Mercurial > hg
changeset 15014:a814e986859f
minirst: add getsections helper
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Tue, 02 Aug 2011 17:43:18 -0500 |
parents | 4a1e3c761ec7 |
children | ee6988aea74e |
files | mercurial/minirst.py |
diffstat | 1 files changed, 19 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/minirst.py Tue Aug 02 17:41:45 2011 -0500 +++ b/mercurial/minirst.py Tue Aug 02 17:43:18 2011 -0500 @@ -465,6 +465,25 @@ 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