# HG changeset patch # User Martin Geisler # Date 2010-04-25 15:48:26 # Node ID 287a5cdf7743b9e1a1b3123ee84444c09888f1d3 # Parent 0a548640e012c103ff0a0300b8f8c1deab487075 minirst: correctly format sections containing inline markup Before, a section like ``foo`` ------- would be formatted as "foo" ------- We now recompute the length of the underline when formatting the section. diff --git a/mercurial/minirst.py b/mercurial/minirst.py --- a/mercurial/minirst.py +++ b/mercurial/minirst.py @@ -241,13 +241,15 @@ def findsections(blocks): if (block['type'] == 'paragraph' and len(block['lines']) == 2 and block['lines'][1] == '-' * len(block['lines'][0])): + block['underline'] = block['lines'][1][0] block['type'] = 'section' + del block['lines'][1] return blocks def inlineliterals(blocks): for b in blocks: - if b['type'] == 'paragraph': + if b['type'] in ('paragraph', 'section'): b['lines'] = [l.replace('``', '"') for l in b['lines']] return blocks @@ -256,7 +258,7 @@ def inlineliterals(blocks): def hgrole(blocks): for b in blocks: - if b['type'] == 'paragraph': + if b['type'] in ('paragraph', 'section'): b['lines'] = [_hgrolere.sub(r'"hg \1"', l) for l in b['lines']] return blocks @@ -289,7 +291,8 @@ def formatblock(block, width): indent += ' ' return indent + ('\n' + indent).join(block['lines']) if block['type'] == 'section': - return indent + ('\n' + indent).join(block['lines']) + underline = len(block['lines'][0]) * block['underline'] + return "%s%s\n%s%s" % (indent, block['lines'][0],indent, underline) if block['type'] == 'definition': term = indent + block['lines'][0] hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) @@ -341,11 +344,11 @@ def format(text, width, indent=0, keep=N b['indent'] += indent blocks = findliteralblocks(blocks) blocks, pruned = prunecontainers(blocks, keep or []) + blocks = findsections(blocks) blocks = inlineliterals(blocks) blocks = hgrole(blocks) blocks = splitparagraphs(blocks) blocks = updatefieldlists(blocks) - blocks = findsections(blocks) blocks = addmargins(blocks) text = '\n'.join(formatblock(b, width) for b in blocks) if keep is None: diff --git a/tests/test-minirst.py b/tests/test-minirst.py --- a/tests/test-minirst.py +++ b/tests/test-minirst.py @@ -186,5 +186,8 @@ debugformat('roles', roles, 60) sections = """ A Somewhat Wide Section Header ------------------------------ + +Markup: ``foo`` and :hg:`help` +------------------------------ """ debugformat('sections', sections, 20) diff --git a/tests/test-minirst.py.out b/tests/test-minirst.py.out --- a/tests/test-minirst.py.out +++ b/tests/test-minirst.py.out @@ -307,5 +307,8 @@ sections formatted to fit within 20 char ---------------------------------------------------------------------- A Somewhat Wide Section Header ------------------------------ + +Markup: "foo" and "hg help" +--------------------------- ----------------------------------------------------------------------