"...src/ssh:/git@developer.sourcefind.cn:2222/tsoc/openmm.git" did not exist on "a57facb4d1f8c9a51bd429f2f4a1df9fcf137c91"
process-docstring.py 4.76 KB
Newer Older
Robert McGibbon's avatar
Robert McGibbon committed
1
2
import re

3
4
5
6
7
8
9
10
11
def count_leading_whitespace(s):
    count = 0
    for c in s:
        if c.isspace():
            count += 1
        else:
            break
    return count

Robert McGibbon's avatar
Robert McGibbon committed
12
def process_docstring(app, what, name, obj, options, lines):
13
14
15
    """This hook edits the docstrings to replace "<tt><pre>" html tags,
    Breathe-style verbatim embed blocks, and deprecated markers with
    sphinx directives.
Robert McGibbon's avatar
Robert McGibbon committed
16
17
18
19
20
21
22
    """
    def repl(m):
        s = m.group(1)
        if not s.startswith(linesep):
            s = linesep + s
        newline = '.. code-block:: c++' + linesep
        return newline + '    ' + s.replace(linesep, linesep + '    ')
23
24
25
26
    def repl2(m):
        s = m.group(1)
        if not s.startswith(linesep):
            s = linesep + s
27
        newline = '|LINEBREAK|.. admonition::|LINEBREAK|   Deprecated' + linesep
28
        return newline + '    ' + s.replace(linesep, linesep + '    ')
John Chodera's avatar
John Chodera committed
29
30
31
    def repl3(m):
        s = m.group(1)
        return '*' + s + '*'
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    def repl4(m):
        s = m.group(1)
        if s.startswith("embed:rst"):
            lines = s.split(linesep)[1:]

            # Match behaviour of Breathe
            if s.startswith("embed:rst:leading-asterisk"):
                lines = [l.replace("*", " ", 1) for l in lines]
            elif s.startswith("embed:rst:leading-slashes"):
                lines = [l.replace("///", "   ", 1) for l in lines]

            # Strip leading whitespace to match first line
            to_strip = count_leading_whitespace(lines[0])
            lines = [l[to_strip:] for l in lines]

            return linesep.join(lines)
        else:
            s = m.group(1)
            if not s.startswith(linesep):
                s = linesep + s
            newline = '.. verbatim::' + linesep
            return newline + '    ' + s.replace(linesep, linesep + '    ')
54
55
56
57
58
59
    def replace_subscript(m):
        """ Replace subscript tags. """
        return r'\ :sub:`{}`\ '.format(m.group(1))
    def replace_superscript(m):
        """ Replace superscript tags. """
        return r'\ :sup:`{}`\ '.format(m.group(1))
Robert McGibbon's avatar
Robert McGibbon committed
60
61
62
63
64
65

    linesep = '|LINEBREAK|'
    joined = linesep.join(lines)

    joined = re.sub(r'<tt><pre>((|LINEBREAK|)?.*?)</pre></tt>', repl, joined)
    joined = re.sub(r'<tt>(.*?)</tt>', repl, joined)
66
    joined = re.sub(r'@deprecated(.*?\|LINEBREAK\|)', repl2, joined, flags=re.IGNORECASE)
John Chodera's avatar
John Chodera committed
67
    joined = re.sub(r'<i>(.*?)</i>', repl3, joined)
68
    joined = re.sub(r'<verbatim>(.*?)</verbatim>', repl4, joined)
69
70
71
    joined = re.sub(r'<sub>(.*?)</sub>', replace_subscript, joined)
    joined = re.sub(r'<sup>(.*?)</sup>', replace_superscript, joined)

Robert McGibbon's avatar
Robert McGibbon committed
72
73
74
    lines[:] = [(l if not l.isspace() else '') for l in joined.split(linesep)]


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
substitutions = {'double':'float', 'long long':'int', 'string':'str',
                 'pairii':'tuple[int, int]',
                 'vectord':'tuple[float, ...]',
                 'vectorvectorvectord':'tuple[tuple[tuple[float, ...], ...], ...]',
                 'vectori':'tuple[int, ...]',
                 'vectorvectori':'tuple[tuple[int, ...], ...]',
                 'vectorpairii':'tuple[tuple[int, int], ...]',
                 'vectorstring':'tuple[str, ...]',
                 'mapstringstring':'Mapping[str, str]',
                 'mapstringdouble':'Mapping[str, float]',
                 'mapii':'Mapping[int, int]',
                 'seti':'set[int]'
                }

def convert_type(type):
    if type in substitutions:
        type = substitutions[type]
    if '<' in type:
        match = re.match('vector<(.*?),.*>', type)
        if match is not None:
            type = f'tuple[{convert_type(match[1])}]'
        match = re.match('map<(.*?),(.*?),.*>', type)
        if match is not None:
            type = f'Mapping[{convert_type(match[1])}, {convert_type(match[2])}]'
    return type

def process_signature(app, what, name, obj, options, signature, return_annotation):
    if return_annotation is not None:
        # Convert C++ types to Python types
        if return_annotation.startswith('std::'):
            return_annotation = return_annotation[5:]
        if return_annotation.endswith(' const &'):
            return_annotation = return_annotation[:-8]
        return_annotation = convert_type(return_annotation)
    return (signature, return_annotation)


Robert McGibbon's avatar
Robert McGibbon committed
112
113
def setup(app):
    app.connect('autodoc-process-docstring', process_docstring)
114
    app.connect('autodoc-process-signature', process_signature)
Robert McGibbon's avatar
Robert McGibbon committed
115
116
117


def test():
118
119
    lines    = ['Hello World', '<tt><pre>', 'contents', '</pre></tt>', '', '<tt>contents2</tt>', 'r<sub>1</sub>', 'r<sup>1</sup>']
    linesRef = ['Hello World', '.. code-block:: c++', '', '    contents', '', '', '.. code-block:: c++', '', '    contents2', 'r\\ :sub:`1`\\ ', 'r\\ :sup:`1`\\ ']
Robert McGibbon's avatar
Robert McGibbon committed
120
    process_docstring(None, None, None, None, None, lines)
121
    assert lines == linesRef, (lines, linesRef)
Robert McGibbon's avatar
Robert McGibbon committed
122
123
124

if __name__ == '__main__':
    test()