Source

pyorg / pyorg / parser.py

Full commit
Gabriele Lanaro 2b5c265 




























"""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