comparison docs/test2rst.py @ 2825:7608f1e04205 stable

doc: fix test2rst Previous versions used to break on multi-lines texts and miss entirely single lines of texts. Use a simplest version without regex.
author Boris Feld <boris.feld@octobus.net>
date Thu, 27 Jul 2017 17:16:02 +0200
parents 94fe2cc9cd41
children 1b4c92621e23
comparison
equal deleted inserted replaced
2824:d0e3a8e0b62c 2825:7608f1e04205
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import os 3 import os
4 import os.path as op 4 import os.path as op
5 import re
6 import sys 5 import sys
7
8 # line starts with two chars one of which is not a space (and both are not
9 # newlines obviously) and ends with one or more newlines followed by two spaces
10 # on a next line (indented text)
11 CODEBLOCK = re.compile(r'()\n(([^ \n][^\n]|[^\n][^ \n])[^\n]*)\n+ ')
12 6
13 INDEX = ''' 7 INDEX = '''
14 Mercurial tests 8 Mercurial tests
15 =============== 9 ===============
16 10
18 :maxdepth: 1 12 :maxdepth: 1
19 ''' 13 '''
20 14
21 15
22 def rstify(orig, name): 16 def rstify(orig, name):
23 header = '%s\n%s\n\n' % (name, '=' * len(name)) 17 newlines = []
24 content = header + orig 18
25 content = CODEBLOCK.sub(r'\n\1\n\n::\n\n ', content) 19 code_block_mode = False
26 return content 20
21 for line in orig.splitlines():
22
23 # Emtpy lines doesn't change output
24 if not line:
25 newlines.append(line)
26 continue
27
28 codeline = line.startswith(' ')
29 if codeline:
30 if code_block_mode is False:
31 newlines.extend(['::', ''])
32
33 code_block_mode = True
34 else:
35 code_block_mode = False
36
37 newlines.append(line)
38
39 return "\n".join(newlines)
27 40
28 41
29 def main(base): 42 def main(base):
30 if os.path.isdir(base): 43 if os.path.isdir(base):
31 one_dir(base) 44 one_dir(base)