[PATCH 1 of 2 STABLE] Options for controlling how minirst formats the output
Olav Reinert
seroton10 at gmail.com
Mon Jan 9 10:31:29 CST 2012
# HG changeset patch
# User Olav Reinert <olav at blackbox.ruvalo.net>
# Date 1326126529 -3600
# Node ID c9f8671b5081abd8891dd8d78ded241ae1c491db
# Parent f15c646bffc7187c357f3dcc12761513c1ed6609
Options for controlling how minirst formats the output
diff -r f15c646bffc7 -r c9f8671b5081 mercurial/minirst.py
--- a/mercurial/minirst.py Thu Jan 05 07:26:22 2012 -0800
+++ b/mercurial/minirst.py Mon Jan 09 17:28:49 2012 +0100
@@ -354,16 +354,19 @@
b['lines'] = [replace(l, substs) for l in b['lines']]
return blocks
-def addmargins(blocks):
+def addmargins(blocks, **options):
"""Adds empty blocks for vertical spacing.
This groups bullets, options, and definitions together with no vertical
space between them, and adds an empty block between all other blocks.
"""
+ nospacetypes = ['bullet', 'option', 'field']
+ if not options.get('definitionmargin', True):
+ nospacetypes.append('definition')
i = 1
while i < len(blocks):
if (blocks[i]['type'] == blocks[i - 1]['type'] and
- blocks[i]['type'] in ('bullet', 'option', 'field')):
+ blocks[i]['type'] in nospacetypes):
i += 1
else:
blocks.insert(i, dict(lines=[''], indent=0, type='margin'))
@@ -430,7 +433,7 @@
initindent=initindent,
hangindent=hangindent))
-def formatblock(block, width):
+def formatblock(block, width, **options):
"""Format a block according to width."""
if width <= 0:
width = 78
@@ -492,20 +495,24 @@
m = _bulletre.match(block['lines'][0])
subindent = indent + m.end() * ' '
elif block['type'] == 'field':
- keywidth = block['keywidth']
+ fieldwidth = options.get('fieldwidth', _fieldwidth)
+ if options.get('fieldshrink', True):
+ keywidth = block['keywidth']
+ else:
+ keywidth = fieldwidth
key = block['key']
- subindent = indent + _fieldwidth * ' '
- if len(key) + 2 > _fieldwidth:
+ subindent = indent + fieldwidth * ' '
+ if len(key) + 2 > fieldwidth:
# key too large, use full line width
key = key.ljust(width)
- elif keywidth + 2 < _fieldwidth:
+ 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)
+ key = key.ljust(fieldwidth)
block['lines'][0] = key + block['lines'][0]
elif block['type'] == 'option':
return formatoption(block, width)
@@ -602,7 +609,7 @@
return ''.join(out)
-def parse(text, indent=0, keep=None):
+def parse(text, indent=0, keep=None, **options):
"""Parse text into a list of blocks"""
pruned = []
blocks = findblocks(text)
@@ -617,7 +624,7 @@
blocks = splitparagraphs(blocks)
blocks = updatefieldlists(blocks)
blocks = updateoptionlists(blocks)
- blocks = addmargins(blocks)
+ blocks = addmargins(blocks, **options)
blocks = prunecomments(blocks)
blocks = findadmonitions(blocks)
return blocks, pruned
@@ -626,13 +633,13 @@
text = ''.join(formatblock(b, width) for b in blocks)
return text
-def format(text, width=80, indent=0, keep=None, style='plain'):
+def format(text, width=80, indent=0, keep=None, style='plain', **options):
"""Parse and format the text according to width."""
- blocks, pruned = parse(text, indent, keep or [])
+ blocks, pruned = parse(text, indent, keep or [], **options)
if style == 'html':
text = formathtml(blocks)
else:
- text = ''.join(formatblock(b, width) for b in blocks)
+ text = ''.join(formatblock(b, width, options) for b in blocks)
if keep is None:
return text
else:
More information about the Mercurial-devel
mailing list