minirst: improve layout of field lists
authorMartin Geisler <mg@lazybytes.net>
Sun, 13 Dec 2009 23:49:53 +0100
changeset 10065 a1ae0ed78d1a
parent 10064 6f30c35766d6
child 10066 788e7d04c594
minirst: improve layout of field lists Before, we used the padding following the key to compute where to wrap the text. Long keys would thus give a big indentation. It also required careful alignment of the source text, making it cumbersome to items to the list. We now compute the maximum key length and use that for all items in the list. We also put a cap on the indentation: keys longer than 10 characters are put on their own line. This is similar to how rst2html handles large keys: it uses 14 as the cutoff point, but I felt that 10 was better for monospaced text in the console.
mercurial/minirst.py
tests/test-dispatch.out
tests/test-minirst.py
tests/test-minirst.py.out
--- a/mercurial/minirst.py	Sun Dec 13 22:37:30 2009 +0100
+++ b/mercurial/minirst.py	Sun Dec 13 23:49:53 2009 +0100
@@ -113,7 +113,7 @@
 
 _bulletre = re.compile(r'(-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)) ')
 _optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)?  +)(.*)$')
-_fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):( +)(.*)')
+_fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):[ ]+(.*)')
 _definitionre = re.compile(r'[^ ]')
 
 def splitparagraphs(blocks):
@@ -159,6 +159,33 @@
     return blocks
 
 
+_fieldwidth = 12
+
+def updatefieldlists(blocks):
+    """Find key and maximum key width for field lists."""
+    i = 0
+    while i < len(blocks):
+        if blocks[i]['type'] != 'field':
+            i += 1
+            continue
+
+        keywidth = 0
+        j = i
+        while j < len(blocks) and blocks[j]['type'] == 'field':
+            m = _fieldre.match(blocks[j]['lines'][0])
+            key, rest = m.groups()
+            blocks[j]['lines'][0] = rest
+            blocks[j]['key'] = key
+            keywidth = max(keywidth, len(key))
+            j += 1
+
+        for block in blocks[i:j]:
+            block['keywidth'] = keywidth
+        i = j + 1
+
+    return blocks
+
+
 def findsections(blocks):
     """Finds sections.
 
@@ -228,11 +255,21 @@
         m = _bulletre.match(block['lines'][0])
         subindent = indent + m.end() * ' '
     elif block['type'] == 'field':
-        m = _fieldre.match(block['lines'][0])
-        key, spaces, rest = m.groups()
-        # Turn ":foo: bar" into "foo   bar".
-        block['lines'][0] = '%s  %s%s' % (key, spaces, rest)
-        subindent = indent + (2 + len(key) + len(spaces)) * ' '
+        keywidth = block['keywidth']
+        key = block['key']
+
+        subindent = indent + _fieldwidth * ' '
+        if len(key) + 2 > _fieldwidth:
+            # key too large, use full line width
+            key = key.ljust(width)
+        elif keywidth + 2 < _fieldwidth:
+            # all keys are small, add only two spaces
+            key = key.ljust(keywidth + 2)
+            subindent = indent + (keywidth + 2) * ' '
+        else:
+            # mixed sizes, use fieldwidth for this one
+            key = key.ljust(_fieldwidth)
+        block['lines'][0] = key + block['lines'][0]
     elif block['type'] == 'option':
         m = _optionre.match(block['lines'][0])
         option, arg, rest = m.groups()
@@ -252,6 +289,7 @@
     blocks = findliteralblocks(blocks)
     blocks = inlineliterals(blocks)
     blocks = splitparagraphs(blocks)
+    blocks = updatefieldlists(blocks)
     blocks = findsections(blocks)
     blocks = addmargins(blocks)
     return '\n'.join(formatblock(b, width) for b in blocks)
@@ -272,6 +310,7 @@
     blocks = debug(findliteralblocks, blocks)
     blocks = debug(inlineliterals, blocks)
     blocks = debug(splitparagraphs, blocks)
+    blocks = debug(updatefieldlists, blocks)
     blocks = debug(findsections, blocks)
     blocks = debug(addmargins, blocks)
     print '\n'.join(formatblock(b, 30) for b in blocks)
--- a/tests/test-dispatch.out	Sun Dec 13 22:37:30 2009 +0100
+++ b/tests/test-dispatch.out	Sun Dec 13 23:49:53 2009 +0100
@@ -13,9 +13,9 @@
     a format string. The formatting rules are the same as for the export
     command, with the following additions:
 
-    "%s"   basename of file being printed
-    "%d"   dirname of file being printed, or '.' if in repository root
-    "%p"   root-relative path name of file being printed
+    "%s"  basename of file being printed
+    "%d"  dirname of file being printed, or '.' if in repository root
+    "%p"  root-relative path name of file being printed
 
 options:
 
--- a/tests/test-minirst.py	Sun Dec 13 22:37:30 2009 +0100
+++ b/tests/test-minirst.py	Sun Dec 13 23:49:53 2009 +0100
@@ -134,13 +134,14 @@
 
 
 fields = """
-Field lists give a simple two-column layout:
+:a: First item.
+:ab: Second item. Indentation and wrapping
+     is handled automatically.
 
-:key:         The whitespace following the key is
-  significant for the wrapping of this text.
-:another key: More text.
-    The indentation on the following
-    lines is not significant.
+Next list:
+
+:small: The larger key below triggers full indentation here.
+:much too large: This key is big enough to get its own line.
 """
 
 debugformat('fields', fields, 60)
--- a/tests/test-minirst.py.out	Sun Dec 13 22:37:30 2009 +0100
+++ b/tests/test-minirst.py.out	Sun Dec 13 23:49:53 2009 +0100
@@ -215,29 +215,34 @@
 
 fields formatted to fit within 60 characters:
 ----------------------------------------------------------------------
-Field lists give a simple two-column layout:
+a   First item.
+ab  Second item. Indentation and wrapping is handled
+    automatically.
 
-key           The whitespace following the key is
-              significant for the wrapping of this text.
-another key   More text. The indentation on the following
-              lines is not significant.
+Next list:
+
+small       The larger key below triggers full indentation
+            here.
+much too large
+            This key is big enough to get its own line.
 ----------------------------------------------------------------------
 
 fields formatted to fit within 30 characters:
 ----------------------------------------------------------------------
-Field lists give a simple two-
-column layout:
+a   First item.
+ab  Second item. Indentation
+    and wrapping is handled
+    automatically.
+
+Next list:
 
-key           The whitespace
-              following the
-              key is
-              significant for
-              the wrapping of
-              this text.
-another key   More text. The
-              indentation on
-              the following
-              lines is not
-              significant.
+small       The larger key
+            below triggers
+            full indentation
+            here.
+much too large
+            This key is big
+            enough to get its
+            own line.
 ----------------------------------------------------------------------