File: extract-rst-targets.py

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (54 lines) | stat: -rwxr-xr-x 1,989 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>

import os
import re
from typing import Dict, Iterator

tgt_pat = re.compile(r'^.. _(\S+?):$', re.MULTILINE)
title_pat = re.compile('^(.+)\n[-=^#*]{5,}$', re.MULTILINE)


def find_explicit_targets(text: str) -> Iterator[str]:
    for m in tgt_pat.finditer(text):
        yield m.group(1)


def find_page_title(text: str) -> str:
    for m in title_pat.finditer(text):
        return m.group(1)
    return ''


def main() -> Dict[str, Dict[str, str]]:
    refs = {}
    docs = {}
    base = os.path.dirname(os.path.abspath(__file__))
    for dirpath, dirnames, filenames in os.walk(base):
        if 'generated' in dirnames:
            dirnames.remove('generated')
        for f in filenames:
            if f.endswith('.rst'):
                with open(os.path.join(dirpath, f)) as stream:
                    raw = stream.read()
                href = os.path.relpath(stream.name, base).replace(os.sep, '/')
                href = href.rpartition('.')[0] + '/'
                docs[href.rstrip('/')] = find_page_title(raw)
                first_line = raw.lstrip('\n').partition('\n')[0]
                first_target_added = False
                for explicit_target in find_explicit_targets(raw):
                    # Shorten the reference link to the top of the page.
                    # Note that anchor links should still be used in HTML docs
                    # to allow jumping within the same page.
                    if not first_target_added:
                        first_target_added = True
                        if first_line.startswith(f'.. _{explicit_target}:'):
                            refs[explicit_target] = href
                            continue
                    refs[explicit_target] = href + f'#{explicit_target.replace("_", "-")}'
    return {'ref': refs, 'doc': docs}


if __name__ == '__main__':
    import json
    print(json.dumps(main(), indent=2))