annotate docs/test2rst.py @ 6049:441e7e773d6c

changelog: add more entries from default branch to 10.4.0
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 07 Oct 2021 21:50:29 +0300
parents 1d80cda7fe93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5660
9d6c3e227455 test2rst: use python3
Anton Shestakov <av6@dwimlabs.net>
parents: 5659
diff changeset
1 #!/usr/bin/env python3
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
2
5664
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
3 import argparse
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
4 import os
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
5 import re
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
6
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
7
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
8 ignored_patterns = [
4801
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
9 re.compile(r'^#if'),
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
10 re.compile(r'^#else'),
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
11 re.compile(r'^#endif'),
16c1398b0063 python3: prefix all regex to work with python 2 and 3
Raphaël Gomès <rgomes@octobus.net>
parents: 2960
diff changeset
12 re.compile(r'#rest-ignore$'),
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
13 ]
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
14
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
15
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
16 def rstify(orig):
5664
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
17 """Take contents of a .t file and produce reStructuredText"""
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
18 newlines = []
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
19
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
20 code_block_mode = False
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
21 sphinx_directive_mode = False
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
22
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
23 for line in orig.splitlines():
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
24
5664
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
25 # Empty lines doesn't change output
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
26 if not line:
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
27 newlines.append(line)
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
28 code_block_mode = False
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
29 sphinx_directive_mode = False
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
30 continue
2958
1cb715257130 doc: add a special flag for content to ignore in the rst
Boris Feld <boris.feld@octobus.net>
parents: 2951
diff changeset
31
2960
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
32 ignored = False
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
33 for pattern in ignored_patterns:
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
34 if pattern.search(line):
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
35 ignored = True
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
36 break
1a4f26eec0af test2rst: ignore the #if/#endif line in the output
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2959
diff changeset
37 if ignored:
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
38 continue
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
39
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
40 # Sphinx directives mode
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
41 if line.startswith(' .. '):
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
42
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
43 # Insert a empty line to makes sphinx happy
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
44 newlines.append("")
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
45
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
46 # And unindent the directive
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
47 line = line[2:]
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
48 sphinx_directive_mode = True
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
49
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
50 # Code mode
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
51 codeline = line.startswith(' ')
2959
ef361938dfa1 doc: integrate graphviz graphs in tutorials
Boris Feld <boris.feld@octobus.net>
parents: 2958
diff changeset
52 if codeline and not sphinx_directive_mode:
2825
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
53 if code_block_mode is False:
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
54 newlines.extend(['::', ''])
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
55
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
56 code_block_mode = True
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
57
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
58 newlines.append(line)
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
59
7608f1e04205 doc: fix test2rst
Boris Feld <boris.feld@octobus.net>
parents: 2035
diff changeset
60 return "\n".join(newlines)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
61
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
62
5664
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
63 def main():
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
64 ap = argparse.ArgumentParser()
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
65 ap.add_argument('testfile', help='.t file to transform')
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
66
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
67 opts = ap.parse_args()
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
68
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
69 with open(opts.testfile) as f:
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
70 content = f.read()
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
71 rst = rstify(content)
5664
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
72 target = os.path.splitext(opts.testfile)[0] + '.rst'
5656
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
73 with open(target, 'w') as f:
60ce376919c5 test2rst: make one_file() useful
Anton Shestakov <av6@dwimlabs.net>
parents: 4801
diff changeset
74 f.write(rst)
235
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
75
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
76
8469ccb9550f [doc] add `.t` to `.rst` converteur
Pierre-Yves.David@ens-lyon.org
parents:
diff changeset
77 if __name__ == '__main__':
5664
1d80cda7fe93 test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents: 5660
diff changeset
78 main()