diff 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
line wrap: on
line diff
--- a/docs/test2rst.py	Thu Jul 27 11:39:51 2017 +0200
+++ b/docs/test2rst.py	Thu Jul 27 17:16:02 2017 +0200
@@ -2,14 +2,8 @@
 
 import os
 import os.path as op
-import re
 import sys
 
-# line starts with two chars one of which is not a space (and both are not
-# newlines obviously) and ends with one or more newlines followed by two spaces
-# on a next line (indented text)
-CODEBLOCK = re.compile(r'()\n(([^ \n][^\n]|[^\n][^ \n])[^\n]*)\n+  ')
-
 INDEX = '''
 Mercurial tests
 ===============
@@ -20,10 +14,29 @@
 
 
 def rstify(orig, name):
-    header = '%s\n%s\n\n' % (name, '=' * len(name))
-    content = header + orig
-    content = CODEBLOCK.sub(r'\n\1\n\n::\n\n  ', content)
-    return content
+    newlines = []
+
+    code_block_mode = False
+
+    for line in orig.splitlines():
+
+        # Emtpy lines doesn't change output
+        if not line:
+            newlines.append(line)
+            continue
+
+        codeline = line.startswith('  ')
+        if codeline:
+            if code_block_mode is False:
+                newlines.extend(['::', ''])
+
+            code_block_mode = True
+        else:
+            code_block_mode = False
+
+        newlines.append(line)
+
+    return "\n".join(newlines)
 
 
 def main(base):