Mercurial > public > mercurial-scm > evolve
comparison docs/test2rst.py @ 5664:1d80cda7fe93 stable
test2rst: minor improvements
author | Anton Shestakov <av6@dwimlabs.net> |
---|---|
date | Mon, 23 Nov 2020 13:43:29 +0800 |
parents | 9d6c3e227455 |
children |
comparison
equal
deleted
inserted
replaced
5663:5affbb44f135 | 5664:1d80cda7fe93 |
---|---|
1 #!/usr/bin/env python3 | 1 #!/usr/bin/env python3 |
2 | 2 |
3 import argparse | |
3 import os | 4 import os |
4 import re | 5 import re |
5 import sys | |
6 | 6 |
7 | 7 |
8 ignored_patterns = [ | 8 ignored_patterns = [ |
9 re.compile(r'^#if'), | 9 re.compile(r'^#if'), |
10 re.compile(r'^#else'), | 10 re.compile(r'^#else'), |
12 re.compile(r'#rest-ignore$'), | 12 re.compile(r'#rest-ignore$'), |
13 ] | 13 ] |
14 | 14 |
15 | 15 |
16 def rstify(orig): | 16 def rstify(orig): |
17 """Take contents of a .t file and produce reStructuredText""" | |
17 newlines = [] | 18 newlines = [] |
18 | 19 |
19 code_block_mode = False | 20 code_block_mode = False |
20 sphinx_directive_mode = False | 21 sphinx_directive_mode = False |
21 | 22 |
22 for line in orig.splitlines(): | 23 for line in orig.splitlines(): |
23 | 24 |
24 # Emtpy lines doesn't change output | 25 # Empty lines doesn't change output |
25 if not line: | 26 if not line: |
26 newlines.append(line) | 27 newlines.append(line) |
27 code_block_mode = False | 28 code_block_mode = False |
28 sphinx_directive_mode = False | 29 sphinx_directive_mode = False |
29 continue | 30 continue |
57 newlines.append(line) | 58 newlines.append(line) |
58 | 59 |
59 return "\n".join(newlines) | 60 return "\n".join(newlines) |
60 | 61 |
61 | 62 |
62 def main(path): | 63 def main(): |
63 with open(path) as f: | 64 ap = argparse.ArgumentParser() |
65 ap.add_argument('testfile', help='.t file to transform') | |
66 | |
67 opts = ap.parse_args() | |
68 | |
69 with open(opts.testfile) as f: | |
64 content = f.read() | 70 content = f.read() |
65 rst = rstify(content) | 71 rst = rstify(content) |
66 target = os.path.splitext(path)[0] + '.rst' | 72 target = os.path.splitext(opts.testfile)[0] + '.rst' |
67 with open(target, 'w') as f: | 73 with open(target, 'w') as f: |
68 f.write(rst) | 74 f.write(rst) |
69 | 75 |
70 | 76 |
71 if __name__ == '__main__': | 77 if __name__ == '__main__': |
72 if len(sys.argv) != 2: | 78 main() |
73 print('Please supply a path to tests dir as parameter') | |
74 sys.exit() | |
75 main(sys.argv[1]) |