"plugins/vscode:/vscode.git/clone" did not exist on "29c09776e0046e90bc5ee953ce8c9830cb68758f"
process-docstring.py 919 Bytes
Newer Older
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
import os
import re
import sys

def replace_code(match):
    lines = match.group(1).split('\n')
    for i, line in enumerate(list(lines)):
        if line.startswith('*'):
            lines[i] = line[1:]
    return f"\n* .. code-block:: c++\n*    {'\n*    '.join(lines)}"


def process_file(file):
    """Edit docstrings in an XML file to remove the Python versions of code examples,
    and put the correct Sphinx directives around the C++ versions."""
    with open(file) as input:
        content = input.read()
    processed = re.sub(r'<c\+\+>((.|\n)*?)</c\+\+>', replace_code, content)
    processed = re.sub(r'<python>((.|\n)*?)</python>', '', processed)
    print(file, processed != content)
    if processed != content:
        with open(file, 'w') as output:
            output.write(processed)


dir = sys.argv[1]
for file in os.listdir(dir):
    process_file(os.path.join(dir, file))