Mercurial > evolve
annotate docs/test2rst.py @ 5758:552efcc2cc30 mercurial-5.5
test-compat: merge mercurial-5.6 into mercurial-5.5
# no-check-commit
author | Anton Shestakov <av6@dwimlabs.net> |
---|---|
date | Sat, 30 Jan 2021 20:32:38 +0800 |
parents | 1d80cda7fe93 |
children |
rev | line source |
---|---|
5660 | 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 | 18 newlines = [] |
19 | |
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 | 22 |
23 for line in orig.splitlines(): | |
24 | |
5664
1d80cda7fe93
test2rst: minor improvements
Anton Shestakov <av6@dwimlabs.net>
parents:
5660
diff
changeset
|
25 # Empty lines doesn't change output |
2825 | 26 if not line: |
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 | 38 continue |
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 | 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 | 53 if code_block_mode is False: |
54 newlines.extend(['::', '']) | |
55 | |
56 code_block_mode = True | |
57 | |
58 newlines.append(line) | |
59 | |
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() |