changeset 10983:287a5cdf7743

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.
author Martin Geisler <mg@lazybytes.net>
date Sun, 25 Apr 2010 17:48:26 +0200
parents 0a548640e012
children 68b7d2d668ce
files mercurial/minirst.py tests/test-minirst.py tests/test-minirst.py.out
diffstat 3 files changed, 13 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/minirst.py	Sun Apr 25 17:38:41 2010 +0200
+++ b/mercurial/minirst.py	Sun Apr 25 17:48:26 2010 +0200
@@ -241,13 +241,15 @@
         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 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 @@
         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 @@
         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:
--- a/tests/test-minirst.py	Sun Apr 25 17:38:41 2010 +0200
+++ b/tests/test-minirst.py	Sun Apr 25 17:48:26 2010 +0200
@@ -186,5 +186,8 @@
 sections = """
 A Somewhat Wide Section Header
 ------------------------------
+
+Markup: ``foo`` and :hg:`help`
+------------------------------
 """
 debugformat('sections', sections, 20)
--- a/tests/test-minirst.py.out	Sun Apr 25 17:38:41 2010 +0200
+++ b/tests/test-minirst.py.out	Sun Apr 25 17:48:26 2010 +0200
@@ -307,5 +307,8 @@
 ----------------------------------------------------------------------
 A Somewhat Wide Section Header
 ------------------------------
+
+Markup: "foo" and "hg help"
+---------------------------
 ----------------------------------------------------------------------