File: regen_readme.py

package info (click to toggle)
coq 8.16.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 40,596 kB
  • sloc: ml: 219,376; sh: 3,545; python: 3,231; ansic: 2,529; makefile: 767; lisp: 279; javascript: 63; xml: 24; sed: 2
file content (65 lines) | stat: -rwxr-xr-x 2,325 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""Rebuild sphinx/README.rst from sphinx/README.template.rst."""

import re
from os import sys, path

SCRIPT_DIR = path.dirname(path.abspath(__file__))
if __name__ == "__main__" and __package__ is None:
    sys.path.append(path.dirname(SCRIPT_DIR))

SPHINX_DIR = path.join(SCRIPT_DIR, "../../sphinx/")
README_TEMPLATE_PATH = path.join(SPHINX_DIR, "README.template.rst")

if len(sys.argv) == 1:
    README_PATH = path.join(SPHINX_DIR, "README.rst")
elif len(sys.argv) == 2:
    README_PATH = sys.argv[1]
else:
    print ("usage: {} [FILE]".format(sys.argv[0]))
    sys.exit(1)

import sphinx
from coqrst import coqdomain

README_ROLES_MARKER = "[ROLES]"
README_OBJECTS_MARKER = "[OBJECTS]"
README_DIRECTIVES_MARKER = "[DIRECTIVES]"

FIRST_LINE_BLANKS = re.compile("^(.*)\n *\n")
def format_docstring(template, obj, *strs):
    docstring = obj.__doc__.strip()
    strs = strs + (FIRST_LINE_BLANKS.sub(r"\1\n", docstring),)
    return template.format(*strs)

def notation_symbol(d):
    return " :black_nib:" if issubclass(d, coqdomain.NotationObject) else ""

def regen_readme():
    objects_docs = [format_docstring("``.. {}::``{} {}", obj, objname, notation_symbol(obj))
                    for objname, obj in sorted(coqdomain.CoqDomain.directives.items())]

    roles = ([(name, cls)
              for name, cls in sorted(coqdomain.CoqDomain.roles.items())
              if not isinstance(cls, (sphinx.roles.XRefRole, coqdomain.IndexXRefRole))] +
             [(fn.role_name, fn)
              for fn in coqdomain.COQ_ADDITIONAL_ROLES])
    roles_docs = [format_docstring("``:{}:`` {}", role, name)
                  for (name, role) in roles]

    directives_docs = [format_docstring("``.. {}::`` {}", d, d.directive_name)
                       for d in coqdomain.COQ_ADDITIONAL_DIRECTIVES]

    with open(README_TEMPLATE_PATH, encoding="utf-8") as readme:
        contents = readme.read()

    with open(README_PATH, mode="w", encoding="utf-8") as readme:
        readme.write(contents
                     .replace(README_ROLES_MARKER, "\n\n".join(roles_docs))
                     .replace(README_OBJECTS_MARKER, "\n\n".join(objects_docs))
                     .replace(README_DIRECTIVES_MARKER, "\n\n".join(directives_docs)))

if __name__ == '__main__':
    regen_readme()