-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc_to_rst.py
More file actions
executable file
·96 lines (69 loc) · 2.86 KB
/
Copy pathsrc_to_rst.py
File metadata and controls
executable file
·96 lines (69 loc) · 2.86 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
#
import argparse
from pathlib import Path
template = '''
.. _{}:
{}
{}
.. code-block:: cpp
'''
dir_template = '''
.. _{link_name}:
{caption_name}
{title}
.. toctree::
:maxdepth: 1
'''
def main(root_source_directory, source_subdirectories, target_directory):
paths = []
for d in source_subdirectories:
path = Path(root_source_directory, d)
paths += list(Path(path).rglob("*.h"))
paths += list(Path(path).rglob("*.cpp"))
for path in paths:
rel_path = path.relative_to(root_source_directory)
if rel_path.parent == '..':
raise ValueError('root_source_directory must contain all files')
rst_parts = [target_directory]
for part in rel_path.parts:
rst_parts.append(part)
rst_parts[-1] += '.rst'
rst_path = Path(*rst_parts)
rst_parent = rst_path.parent
if not rst_parent.exists():
rst_parent.mkdir(parents=True)
src_name = rel_path
name = src_name.name
with open(rst_path, 'w') as wfp:
header = '=' * (4 + len(src_name.as_posix()))
wfp.write(template.format(src_name, name, header))
with open(path, 'r') as rfp:
for line in rfp:
line = line.replace('\t', (' ' * 8))
wfp.write(' ' + line)
for path in (p for p in Path(target_directory).rglob('*') if p.is_dir()):
link_name = path.relative_to(target_directory).as_posix()
caption_name = path.stem
rst_file = path / 'dir.rst'
dirs = list(Path(p) for p in path.glob('*') if p.is_dir())
files = list(Path(p) for p in path.glob('*.h.rst'))
files += list(Path(p) for p in path.glob('*.cpp.rst'))
with open(rst_file, 'w') as wfp:
wfp.write(dir_template.format(caption_name=caption_name,
link_name=link_name,
title=('=' * (len(caption_name) + 4))))
for dir in sorted(dirs):
dirname = dir.relative_to(path).as_posix()
wfp.write(' {}/ <{}>\n'.format(dirname, dirname + '/dir'))
for file in sorted(files):
wfp.write(' ' + file.stem + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--root-source-directory', type=str, default='.',
help='Root source subdirectory')
parser.add_argument('-s', '--source-subdirectories', type=str, nargs='+', default='.',
help='Source subdirectories')
parser.add_argument('-t', '--target-directory', type=str, default='.',
help='Target directory')
main(**vars(parser.parse_args()))