2009-07-17 01:25:25 +04:00
|
|
|
# minirst.py - minimal reStructuredText parser
|
|
|
|
#
|
2009-10-05 00:03:41 +04:00
|
|
|
# Copyright 2009, 2010 Matt Mackall <mpm@selenic.com> and others
|
2009-07-17 01:25:25 +04:00
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2009-07-17 01:25:25 +04:00
|
|
|
|
|
|
|
"""simplified reStructuredText parser.
|
|
|
|
|
|
|
|
This parser knows just enough about reStructuredText to parse the
|
|
|
|
Mercurial docstrings.
|
|
|
|
|
|
|
|
It cheats in a major way: nested blocks are not really nested. They
|
|
|
|
are just indented blocks that look like they are nested. This relies
|
|
|
|
on the user to keep the right indentation for the blocks.
|
|
|
|
|
|
|
|
It only supports a small subset of reStructuredText:
|
|
|
|
|
2009-11-07 05:22:03 +03:00
|
|
|
- sections
|
|
|
|
|
2009-07-17 01:25:25 +04:00
|
|
|
- paragraphs
|
|
|
|
|
2009-11-07 05:22:03 +03:00
|
|
|
- literal blocks
|
2009-07-17 01:25:25 +04:00
|
|
|
|
2009-11-07 05:22:03 +03:00
|
|
|
- definition lists
|
2009-07-17 01:25:25 +04:00
|
|
|
|
2010-09-22 17:51:59 +04:00
|
|
|
- specific admonitions
|
|
|
|
|
2009-11-07 05:22:03 +03:00
|
|
|
- bullet lists (items must start with '-')
|
2009-08-03 01:38:07 +04:00
|
|
|
|
2009-11-07 05:22:03 +03:00
|
|
|
- enumerated lists (no autonumbering)
|
|
|
|
|
|
|
|
- field lists (colons cannot be escaped)
|
2009-07-17 01:25:25 +04:00
|
|
|
|
|
|
|
- option lists (supports only long options without arguments)
|
|
|
|
|
2009-11-07 05:22:03 +03:00
|
|
|
- inline literals (no other inline markup is not recognized)
|
2009-07-17 01:25:25 +04:00
|
|
|
"""
|
|
|
|
|
replace Python standard textwrap by MBCS sensitive one for i18n text
Mercurial has problem around text wrapping/filling in MBCS encoding
environment, because standard 'textwrap' module of Python can not
treat it correctly. It splits byte sequence for one character into two
lines.
According to unicode specification, "east asian width" classifies
characters into:
W(ide), N(arrow), F(ull-width), H(alf-width), A(mbiguous)
W/N/F/H can be always recognized as 2/1/2/1 bytes in byte sequence,
but 'A' can not. Size of 'A' depends on language in which it is used.
Unicode specification says:
If the context(= language) cannot be established reliably they
should be treated as narrow characters by default
but many of class 'A' characters are full-width, at least, in Japanese
environment.
So, this patch treats class 'A' characters as full-width always for
safety wrapping.
This patch focuses only on MBCS safe-ness, not on writing/printing
rule strict wrapping for each languages
MBCS sensitive textwrap class is originally implemented
by ITO Nobuaki <daydream.trippers@gmail.com>.
2010-06-06 12:20:10 +04:00
|
|
|
import re, sys
|
2010-06-30 07:44:58 +04:00
|
|
|
import util, encoding
|
2010-09-22 17:51:59 +04:00
|
|
|
from i18n import _
|
|
|
|
|
2010-06-30 07:44:58 +04:00
|
|
|
|
|
|
|
def replace(text, substs):
|
|
|
|
utext = text.decode(encoding.encoding)
|
|
|
|
for f, t in substs:
|
|
|
|
utext = utext.replace(f, t)
|
|
|
|
return utext.encode(encoding.encoding)
|
2009-07-17 01:25:25 +04:00
|
|
|
|
2010-10-09 01:19:26 +04:00
|
|
|
|
|
|
|
_blockre = re.compile(r"\n(?:\s*\n)+")
|
|
|
|
|
2009-07-17 01:25:25 +04:00
|
|
|
def findblocks(text):
|
|
|
|
"""Find continuous blocks of lines in text.
|
|
|
|
|
|
|
|
Returns a list of dictionaries representing the blocks. Each block
|
|
|
|
has an 'indent' field and a 'lines' field.
|
|
|
|
"""
|
2010-10-09 01:19:26 +04:00
|
|
|
blocks = []
|
|
|
|
for b in _blockre.split(text.strip()):
|
|
|
|
lines = b.splitlines()
|
|
|
|
indent = min((len(l) - len(l.lstrip())) for l in lines)
|
|
|
|
lines = [l[indent:] for l in lines]
|
|
|
|
blocks.append(dict(indent=indent, lines=lines))
|
2009-07-17 01:25:25 +04:00
|
|
|
return blocks
|
|
|
|
|
|
|
|
|
|
|
|
def findliteralblocks(blocks):
|
|
|
|
"""Finds literal blocks and adds a 'type' field to the blocks.
|
|
|
|
|
|
|
|
Literal blocks are given the type 'literal', all other blocks are
|
|
|
|
given type the 'paragraph'.
|
|
|
|
"""
|
|
|
|
i = 0
|
|
|
|
while i < len(blocks):
|
|
|
|
# Searching for a block that looks like this:
|
|
|
|
#
|
|
|
|
# +------------------------------+
|
|
|
|
# | paragraph |
|
|
|
|
# | (ends with "::") |
|
|
|
|
# +------------------------------+
|
|
|
|
# +---------------------------+
|
|
|
|
# | indented literal block |
|
|
|
|
# +---------------------------+
|
|
|
|
blocks[i]['type'] = 'paragraph'
|
2010-01-25 09:05:27 +03:00
|
|
|
if blocks[i]['lines'][-1].endswith('::') and i + 1 < len(blocks):
|
2009-07-17 01:25:25 +04:00
|
|
|
indent = blocks[i]['indent']
|
2010-01-25 09:05:27 +03:00
|
|
|
adjustment = blocks[i + 1]['indent'] - indent
|
2009-07-17 01:25:25 +04:00
|
|
|
|
|
|
|
if blocks[i]['lines'] == ['::']:
|
|
|
|
# Expanded form: remove block
|
|
|
|
del blocks[i]
|
|
|
|
i -= 1
|
|
|
|
elif blocks[i]['lines'][-1].endswith(' ::'):
|
|
|
|
# Partially minimized form: remove space and both
|
|
|
|
# colons.
|
|
|
|
blocks[i]['lines'][-1] = blocks[i]['lines'][-1][:-3]
|
|
|
|
else:
|
|
|
|
# Fully minimized form: remove just one colon.
|
|
|
|
blocks[i]['lines'][-1] = blocks[i]['lines'][-1][:-1]
|
|
|
|
|
|
|
|
# List items are formatted with a hanging indent. We must
|
|
|
|
# correct for this here while we still have the original
|
|
|
|
# information on the indentation of the subsequent literal
|
|
|
|
# blocks available.
|
2009-11-07 05:04:46 +03:00
|
|
|
m = _bulletre.match(blocks[i]['lines'][0])
|
|
|
|
if m:
|
|
|
|
indent += m.end()
|
|
|
|
adjustment -= m.end()
|
2009-07-17 01:25:25 +04:00
|
|
|
|
|
|
|
# Mark the following indented blocks.
|
2010-01-25 09:05:27 +03:00
|
|
|
while i + 1 < len(blocks) and blocks[i + 1]['indent'] > indent:
|
|
|
|
blocks[i + 1]['type'] = 'literal'
|
|
|
|
blocks[i + 1]['indent'] -= adjustment
|
2009-07-17 01:25:25 +04:00
|
|
|
i += 1
|
|
|
|
i += 1
|
|
|
|
return blocks
|
|
|
|
|
2010-02-13 23:21:40 +03:00
|
|
|
_bulletre = re.compile(r'(-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)|\|) ')
|
2009-11-06 02:30:35 +03:00
|
|
|
_optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)? +)(.*)$')
|
2009-12-14 01:49:53 +03:00
|
|
|
_fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):[ ]+(.*)')
|
2009-11-06 02:30:35 +03:00
|
|
|
_definitionre = re.compile(r'[^ ]')
|
|
|
|
|
|
|
|
def splitparagraphs(blocks):
|
|
|
|
"""Split paragraphs into lists."""
|
|
|
|
# Tuples with (list type, item regexp, single line items?). Order
|
|
|
|
# matters: definition lists has the least specific regexp and must
|
|
|
|
# come last.
|
|
|
|
listtypes = [('bullet', _bulletre, True),
|
|
|
|
('option', _optionre, True),
|
|
|
|
('field', _fieldre, True),
|
|
|
|
('definition', _definitionre, False)]
|
|
|
|
|
|
|
|
def match(lines, i, itemre, singleline):
|
|
|
|
"""Does itemre match an item at line i?
|
|
|
|
|
|
|
|
A list item can be followed by an idented line or another list
|
|
|
|
item (but only if singleline is True).
|
|
|
|
"""
|
|
|
|
line1 = lines[i]
|
2010-01-25 09:05:27 +03:00
|
|
|
line2 = i + 1 < len(lines) and lines[i + 1] or ''
|
2009-11-06 02:30:35 +03:00
|
|
|
if not itemre.match(line1):
|
|
|
|
return False
|
|
|
|
if singleline:
|
|
|
|
return line2 == '' or line2[0] == ' ' or itemre.match(line2)
|
|
|
|
else:
|
|
|
|
return line2.startswith(' ')
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while i < len(blocks):
|
|
|
|
if blocks[i]['type'] == 'paragraph':
|
|
|
|
lines = blocks[i]['lines']
|
|
|
|
for type, itemre, singleline in listtypes:
|
|
|
|
if match(lines, 0, itemre, singleline):
|
|
|
|
items = []
|
|
|
|
for j, line in enumerate(lines):
|
|
|
|
if match(lines, j, itemre, singleline):
|
|
|
|
items.append(dict(type=type, lines=[],
|
|
|
|
indent=blocks[i]['indent']))
|
|
|
|
items[-1]['lines'].append(line)
|
2010-01-25 09:05:27 +03:00
|
|
|
blocks[i:i + 1] = items
|
2009-11-06 02:30:35 +03:00
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
return blocks
|
|
|
|
|
2009-07-17 01:25:25 +04:00
|
|
|
|
2009-12-14 01:49:53 +03:00
|
|
|
_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
|
|
|
|
|
|
|
|
|
2009-10-05 00:03:41 +04:00
|
|
|
def prunecontainers(blocks, keep):
|
|
|
|
"""Prune unwanted containers.
|
|
|
|
|
|
|
|
The blocks must have a 'type' field, i.e., they should have been
|
|
|
|
run through findliteralblocks first.
|
|
|
|
"""
|
2010-02-13 20:11:08 +03:00
|
|
|
pruned = []
|
2009-10-05 00:03:41 +04:00
|
|
|
i = 0
|
|
|
|
while i + 1 < len(blocks):
|
|
|
|
# Searching for a block that looks like this:
|
|
|
|
#
|
|
|
|
# +-------+---------------------------+
|
|
|
|
# | ".. container ::" type |
|
|
|
|
# +---+ |
|
|
|
|
# | blocks |
|
|
|
|
# +-------------------------------+
|
|
|
|
if (blocks[i]['type'] == 'paragraph' and
|
|
|
|
blocks[i]['lines'][0].startswith('.. container::')):
|
|
|
|
indent = blocks[i]['indent']
|
|
|
|
adjustment = blocks[i + 1]['indent'] - indent
|
|
|
|
containertype = blocks[i]['lines'][0][15:]
|
|
|
|
prune = containertype not in keep
|
2010-02-13 20:11:08 +03:00
|
|
|
if prune:
|
|
|
|
pruned.append(containertype)
|
2009-10-05 00:03:41 +04:00
|
|
|
|
|
|
|
# Always delete "..container:: type" block
|
|
|
|
del blocks[i]
|
|
|
|
j = i
|
|
|
|
while j < len(blocks) and blocks[j]['indent'] > indent:
|
|
|
|
if prune:
|
|
|
|
del blocks[j]
|
|
|
|
i -= 1 # adjust outer index
|
|
|
|
else:
|
|
|
|
blocks[j]['indent'] -= adjustment
|
|
|
|
j += 1
|
|
|
|
i += 1
|
2010-02-13 20:11:08 +03:00
|
|
|
return blocks, pruned
|
2009-10-05 00:03:41 +04:00
|
|
|
|
|
|
|
|
2010-04-25 20:19:54 +04:00
|
|
|
_sectionre = re.compile(r"""^([-=`:.'"~^_*+#])\1+$""")
|
|
|
|
|
2009-07-17 01:25:25 +04:00
|
|
|
def findsections(blocks):
|
|
|
|
"""Finds sections.
|
|
|
|
|
|
|
|
The blocks must have a 'type' field, i.e., they should have been
|
|
|
|
run through findliteralblocks first.
|
|
|
|
"""
|
|
|
|
for block in blocks:
|
|
|
|
# Searching for a block that looks like this:
|
|
|
|
#
|
|
|
|
# +------------------------------+
|
|
|
|
# | Section title |
|
|
|
|
# | ------------- |
|
|
|
|
# +------------------------------+
|
|
|
|
if (block['type'] == 'paragraph' and
|
|
|
|
len(block['lines']) == 2 and
|
2010-10-28 00:35:23 +04:00
|
|
|
encoding.colwidth(block['lines'][0]) == len(block['lines'][1]) and
|
2010-04-25 20:19:54 +04:00
|
|
|
_sectionre.match(block['lines'][1])):
|
2010-04-25 19:48:26 +04:00
|
|
|
block['underline'] = block['lines'][1][0]
|
2009-07-17 01:25:25 +04:00
|
|
|
block['type'] = 'section'
|
2010-04-25 19:48:26 +04:00
|
|
|
del block['lines'][1]
|
2009-07-17 01:25:25 +04:00
|
|
|
return blocks
|
|
|
|
|
|
|
|
|
2009-10-21 00:16:27 +04:00
|
|
|
def inlineliterals(blocks):
|
2010-06-30 07:44:58 +04:00
|
|
|
substs = [('``', '"')]
|
2009-10-21 00:16:27 +04:00
|
|
|
for b in blocks:
|
2010-04-25 19:48:26 +04:00
|
|
|
if b['type'] in ('paragraph', 'section'):
|
2010-06-30 07:44:58 +04:00
|
|
|
b['lines'] = [replace(l, substs) for l in b['lines']]
|
2009-10-21 00:16:27 +04:00
|
|
|
return blocks
|
|
|
|
|
|
|
|
|
2010-04-22 12:04:53 +04:00
|
|
|
def hgrole(blocks):
|
2010-06-30 07:44:58 +04:00
|
|
|
substs = [(':hg:`', '"hg '), ('`', '"')]
|
2010-04-22 12:04:53 +04:00
|
|
|
for b in blocks:
|
2010-04-25 19:48:26 +04:00
|
|
|
if b['type'] in ('paragraph', 'section'):
|
2010-05-18 18:24:14 +04:00
|
|
|
# Turn :hg:`command` into "hg command". This also works
|
|
|
|
# when there is a line break in the command and relies on
|
|
|
|
# the fact that we have no stray back-quotes in the input
|
|
|
|
# (run the blocks through inlineliterals first).
|
2010-06-30 07:44:58 +04:00
|
|
|
b['lines'] = [replace(l, substs) for l in b['lines']]
|
2010-04-22 12:04:53 +04:00
|
|
|
return blocks
|
|
|
|
|
|
|
|
|
2009-07-17 01:25:25 +04:00
|
|
|
def addmargins(blocks):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
i = 1
|
|
|
|
while i < len(blocks):
|
2010-01-25 09:05:27 +03:00
|
|
|
if (blocks[i]['type'] == blocks[i - 1]['type'] and
|
2010-04-18 17:47:49 +04:00
|
|
|
blocks[i]['type'] in ('bullet', 'option', 'field')):
|
2009-07-17 01:25:25 +04:00
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
blocks.insert(i, dict(lines=[''], indent=0, type='margin'))
|
|
|
|
i += 2
|
|
|
|
return blocks
|
|
|
|
|
2010-10-23 19:30:08 +04:00
|
|
|
def prunecomments(blocks):
|
|
|
|
"""Remove comments."""
|
|
|
|
i = 0
|
|
|
|
while i < len(blocks):
|
|
|
|
b = blocks[i]
|
|
|
|
if b['type'] == 'paragraph' and b['lines'][0].startswith('.. '):
|
|
|
|
del blocks[i]
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
return blocks
|
|
|
|
|
2010-10-09 00:42:32 +04:00
|
|
|
_admonitionre = re.compile(r"\.\. (admonition|attention|caution|danger|"
|
|
|
|
r"error|hint|important|note|tip|warning)::",
|
|
|
|
flags=re.IGNORECASE)
|
|
|
|
|
2010-09-22 17:51:59 +04:00
|
|
|
def findadmonitions(blocks):
|
|
|
|
"""
|
|
|
|
Makes the type of the block an admonition block if
|
|
|
|
the first line is an admonition directive
|
|
|
|
"""
|
|
|
|
i = 0
|
|
|
|
while i < len(blocks):
|
2010-10-09 00:42:32 +04:00
|
|
|
m = _admonitionre.match(blocks[i]['lines'][0])
|
2010-09-22 17:51:59 +04:00
|
|
|
if m:
|
|
|
|
blocks[i]['type'] = 'admonition'
|
|
|
|
admonitiontitle = blocks[i]['lines'][0][3:m.end() - 2].lower()
|
|
|
|
|
|
|
|
firstline = blocks[i]['lines'][0][m.end() + 1:]
|
2010-10-09 00:42:32 +04:00
|
|
|
if firstline:
|
|
|
|
blocks[i]['lines'].insert(1, ' ' + firstline)
|
2010-09-22 17:51:59 +04:00
|
|
|
|
|
|
|
blocks[i]['admonitiontitle'] = admonitiontitle
|
|
|
|
del blocks[i]['lines'][0]
|
|
|
|
i = i + 1
|
|
|
|
return blocks
|
2009-07-17 01:25:25 +04:00
|
|
|
|
2010-10-09 01:23:49 +04:00
|
|
|
_admonitiontitles = {'attention': _('Attention:'),
|
|
|
|
'caution': _('Caution:'),
|
|
|
|
'danger': _('!Danger!') ,
|
|
|
|
'error': _('Error:'),
|
|
|
|
'hint': _('Hint:'),
|
|
|
|
'important': _('Important:'),
|
|
|
|
'note': _('Note:'),
|
|
|
|
'tip': _('Tip:'),
|
|
|
|
'warning': _('Warning!')}
|
|
|
|
|
2009-07-17 01:25:25 +04:00
|
|
|
def formatblock(block, width):
|
|
|
|
"""Format a block according to width."""
|
2009-09-03 23:07:06 +04:00
|
|
|
if width <= 0:
|
|
|
|
width = 78
|
2009-07-17 01:25:25 +04:00
|
|
|
indent = ' ' * block['indent']
|
2010-09-22 17:51:59 +04:00
|
|
|
if block['type'] == 'admonition':
|
2010-10-09 01:23:49 +04:00
|
|
|
admonition = _admonitiontitles[block['admonitiontitle']]
|
2010-09-22 17:51:59 +04:00
|
|
|
hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip())
|
|
|
|
|
|
|
|
defindent = indent + hang * ' '
|
|
|
|
text = ' '.join(map(str.strip, block['lines']))
|
|
|
|
return '%s\n%s' % (indent + admonition, util.wrap(text, width=width,
|
|
|
|
initindent=defindent,
|
|
|
|
hangindent=defindent))
|
2009-07-17 01:25:25 +04:00
|
|
|
if block['type'] == 'margin':
|
|
|
|
return ''
|
2009-11-05 23:30:50 +03:00
|
|
|
if block['type'] == 'literal':
|
2009-08-02 19:17:17 +04:00
|
|
|
indent += ' '
|
|
|
|
return indent + ('\n' + indent).join(block['lines'])
|
2009-11-05 23:30:50 +03:00
|
|
|
if block['type'] == 'section':
|
2010-10-28 00:35:23 +04:00
|
|
|
underline = encoding.colwidth(block['lines'][0]) * block['underline']
|
2010-04-25 19:48:26 +04:00
|
|
|
return "%s%s\n%s%s" % (indent, block['lines'][0],indent, underline)
|
2009-11-05 23:30:50 +03:00
|
|
|
if block['type'] == 'definition':
|
2009-07-17 01:25:25 +04:00
|
|
|
term = indent + block['lines'][0]
|
2009-11-06 02:30:35 +03:00
|
|
|
hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip())
|
|
|
|
defindent = indent + hang * ' '
|
2009-07-17 01:25:25 +04:00
|
|
|
text = ' '.join(map(str.strip, block['lines'][1:]))
|
replace Python standard textwrap by MBCS sensitive one for i18n text
Mercurial has problem around text wrapping/filling in MBCS encoding
environment, because standard 'textwrap' module of Python can not
treat it correctly. It splits byte sequence for one character into two
lines.
According to unicode specification, "east asian width" classifies
characters into:
W(ide), N(arrow), F(ull-width), H(alf-width), A(mbiguous)
W/N/F/H can be always recognized as 2/1/2/1 bytes in byte sequence,
but 'A' can not. Size of 'A' depends on language in which it is used.
Unicode specification says:
If the context(= language) cannot be established reliably they
should be treated as narrow characters by default
but many of class 'A' characters are full-width, at least, in Japanese
environment.
So, this patch treats class 'A' characters as full-width always for
safety wrapping.
This patch focuses only on MBCS safe-ness, not on writing/printing
rule strict wrapping for each languages
MBCS sensitive textwrap class is originally implemented
by ITO Nobuaki <daydream.trippers@gmail.com>.
2010-06-06 12:20:10 +04:00
|
|
|
return '%s\n%s' % (term, util.wrap(text, width=width,
|
|
|
|
initindent=defindent,
|
|
|
|
hangindent=defindent))
|
2010-04-18 17:47:49 +04:00
|
|
|
subindent = indent
|
2009-11-05 23:30:50 +03:00
|
|
|
if block['type'] == 'bullet':
|
2010-02-13 23:21:40 +03:00
|
|
|
if block['lines'][0].startswith('| '):
|
|
|
|
# Remove bullet for line blocks and add no extra
|
|
|
|
# indention.
|
|
|
|
block['lines'][0] = block['lines'][0][2:]
|
|
|
|
else:
|
|
|
|
m = _bulletre.match(block['lines'][0])
|
|
|
|
subindent = indent + m.end() * ' '
|
2009-11-06 02:30:35 +03:00
|
|
|
elif block['type'] == 'field':
|
2009-12-14 01:49:53 +03:00
|
|
|
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]
|
2009-11-06 02:30:35 +03:00
|
|
|
elif block['type'] == 'option':
|
|
|
|
m = _optionre.match(block['lines'][0])
|
2009-12-14 00:37:30 +03:00
|
|
|
option, arg, rest = m.groups()
|
|
|
|
subindent = indent + (len(option) + len(arg)) * ' '
|
2009-11-05 23:30:50 +03:00
|
|
|
|
2009-11-06 02:30:35 +03:00
|
|
|
text = ' '.join(map(str.strip, block['lines']))
|
replace Python standard textwrap by MBCS sensitive one for i18n text
Mercurial has problem around text wrapping/filling in MBCS encoding
environment, because standard 'textwrap' module of Python can not
treat it correctly. It splits byte sequence for one character into two
lines.
According to unicode specification, "east asian width" classifies
characters into:
W(ide), N(arrow), F(ull-width), H(alf-width), A(mbiguous)
W/N/F/H can be always recognized as 2/1/2/1 bytes in byte sequence,
but 'A' can not. Size of 'A' depends on language in which it is used.
Unicode specification says:
If the context(= language) cannot be established reliably they
should be treated as narrow characters by default
but many of class 'A' characters are full-width, at least, in Japanese
environment.
So, this patch treats class 'A' characters as full-width always for
safety wrapping.
This patch focuses only on MBCS safe-ness, not on writing/printing
rule strict wrapping for each languages
MBCS sensitive textwrap class is originally implemented
by ITO Nobuaki <daydream.trippers@gmail.com>.
2010-06-06 12:20:10 +04:00
|
|
|
return util.wrap(text, width=width,
|
|
|
|
initindent=indent,
|
|
|
|
hangindent=subindent)
|
2009-07-17 01:25:25 +04:00
|
|
|
|
|
|
|
|
2010-02-13 20:11:08 +03:00
|
|
|
def format(text, width, indent=0, keep=None):
|
2009-07-17 01:25:25 +04:00
|
|
|
"""Parse and format the text according to width."""
|
|
|
|
blocks = findblocks(text)
|
2009-10-04 14:18:43 +04:00
|
|
|
for b in blocks:
|
|
|
|
b['indent'] += indent
|
2009-07-17 01:25:25 +04:00
|
|
|
blocks = findliteralblocks(blocks)
|
2010-02-13 20:11:08 +03:00
|
|
|
blocks, pruned = prunecontainers(blocks, keep or [])
|
2010-04-25 19:48:26 +04:00
|
|
|
blocks = findsections(blocks)
|
2009-10-21 00:16:27 +04:00
|
|
|
blocks = inlineliterals(blocks)
|
2010-04-22 12:04:53 +04:00
|
|
|
blocks = hgrole(blocks)
|
2009-11-06 02:30:35 +03:00
|
|
|
blocks = splitparagraphs(blocks)
|
2009-12-14 01:49:53 +03:00
|
|
|
blocks = updatefieldlists(blocks)
|
2010-10-23 19:30:08 +04:00
|
|
|
blocks = prunecomments(blocks)
|
2009-07-17 01:25:25 +04:00
|
|
|
blocks = addmargins(blocks)
|
2010-09-22 17:51:59 +04:00
|
|
|
blocks = findadmonitions(blocks)
|
2010-02-13 20:11:08 +03:00
|
|
|
text = '\n'.join(formatblock(b, width) for b in blocks)
|
|
|
|
if keep is None:
|
|
|
|
return text
|
|
|
|
else:
|
|
|
|
return text, pruned
|
2009-07-17 01:25:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from pprint import pprint
|
|
|
|
|
2009-10-05 00:03:41 +04:00
|
|
|
def debug(func, *args):
|
|
|
|
blocks = func(*args)
|
2009-07-17 01:25:25 +04:00
|
|
|
print "*** after %s:" % func.__name__
|
|
|
|
pprint(blocks)
|
|
|
|
print
|
|
|
|
return blocks
|
|
|
|
|
|
|
|
text = open(sys.argv[1]).read()
|
|
|
|
blocks = debug(findblocks, text)
|
|
|
|
blocks = debug(findliteralblocks, blocks)
|
2010-05-17 22:42:28 +04:00
|
|
|
blocks, pruned = debug(prunecontainers, blocks, sys.argv[2:])
|
2009-12-13 21:24:24 +03:00
|
|
|
blocks = debug(inlineliterals, blocks)
|
2009-11-06 02:30:35 +03:00
|
|
|
blocks = debug(splitparagraphs, blocks)
|
2009-12-14 01:49:53 +03:00
|
|
|
blocks = debug(updatefieldlists, blocks)
|
2009-07-17 01:25:25 +04:00
|
|
|
blocks = debug(findsections, blocks)
|
2010-10-23 19:30:08 +04:00
|
|
|
blocks = debug(prunecomments, blocks)
|
2009-07-17 01:25:25 +04:00
|
|
|
blocks = debug(addmargins, blocks)
|
2010-09-22 17:51:59 +04:00
|
|
|
blocks = debug(findadmonitions, blocks)
|
2009-07-17 01:25:25 +04:00
|
|
|
print '\n'.join(formatblock(b, 30) for b in blocks)
|