Commits

Gabriele Lanaro committed 2b5c265

passing tests, the parser can split headings now

  • Participants
  • Parent commits 4243507

Comments (0)

Files changed (1)

+"""Parser related stuff
+"""
+import re
+
+def maxhead(text):
+    """tell which is the topmost level
+    """
+    levels = []
+    for line in text.splitlines():
+        match = re.match('^(\*+)\s(.*?)\s*$', line)
+        if match:
+            levels.append(len(match.groups()[0]))
+    return min(levels)
+
+def splitheads(text):
+    """Split up the topmost headers returning a list of the headers
+    with the associated text.
+    """
+    
+    indexes = [m.start() for m in 
+               re.finditer('^(\*{%i})\s(.*?)\s*$'%maxhead(text), text, flags = re.MULTILINE)]
+    ret = []
+    for i in range(len(indexes)-1):
+        start = indexes[i]
+        end = indexes[i+1]
+        ret.append(text[start:end])
+    
+    return ret
+            
+