Mercurial > public > mercurial-scm > hg
diff mercurial/minirst.py @ 10065:a1ae0ed78d1a
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.
author | Martin Geisler <mg@lazybytes.net> |
---|---|
date | Sun, 13 Dec 2009 23:49:53 +0100 |
parents | 6f30c35766d6 |
children | d6512b3e9ac0 |
line wrap: on
line diff
--- 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)