# HG changeset patch # User Matt Mackall # Date 2011-08-02 22:43:18 # Node ID a814e986859f85acb637f7feb8227740ac637d69 # Parent 4a1e3c761ec78affd19a0a399df2fe64064f8ba7 minirst: add getsections helper diff --git a/mercurial/minirst.py b/mercurial/minirst.py --- a/mercurial/minirst.py +++ b/mercurial/minirst.py @@ -465,6 +465,25 @@ def format(text, width, indent=0, keep=N else: return text, pruned +def getsections(blocks): + '''return a list of (section name, nesting level, blocks) tuples''' + nest = "" + level = 0 + secs = [] + for b in blocks: + if b['type'] == 'section': + i = b['underline'] + if i not in nest: + nest += i + level = nest.index(i) + 1 + nest = nest[:level] + secs.append((b['lines'][0], level, [b])) + else: + if not secs: + # add an initial empty section + secs = [('', 0, [])] + secs[-1][2].append(b) + return secs if __name__ == "__main__": from pprint import pprint