minirst.py
900 lines
| 29.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / minirst.py
Martin Geisler
|
r9156 | # minirst.py - minimal reStructuredText parser | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2009, 2010 Olivia Mackall <olivia@selenic.com> and others | ||
Martin Geisler
|
r9156 | # | ||
# This software may be used and distributed according to the terms of the | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Martin Geisler
|
r9156 | |||
"""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. | ||||
Matt Mackall
|
r26421 | Remember to update https://mercurial-scm.org/wiki/HelpStyleGuide | ||
Martin Geisler
|
r12958 | when adding support for new constructs. | ||
Martin Geisler
|
r9156 | """ | ||
Matt Harbison
|
r52756 | from __future__ import annotations | ||
Erik Zielke
|
r12388 | |||
Gregory Szorc
|
r25960 | import re | ||
from .i18n import _ | ||||
from . import ( | ||||
encoding, | ||||
Pulkit Goyal
|
r31318 | pycompat, | ||
Augie Fackler
|
r34696 | url, | ||
Yuya Nishihara
|
r37102 | ) | ||
Augie Fackler
|
r43346 | from .utils import stringutil | ||
Dan Villiom Podlaski Christiansen
|
r18750 | |||
Dan Villiom Podlaski Christiansen
|
r18748 | def section(s): | ||
Augie Fackler
|
r43347 | return b"%s\n%s\n\n" % (s, b"\"" * encoding.colwidth(s)) | ||
Dan Villiom Podlaski Christiansen
|
r18748 | |||
Augie Fackler
|
r43346 | |||
Dan Villiom Podlaski Christiansen
|
r18748 | def subsection(s): | ||
Augie Fackler
|
r43347 | return b"%s\n%s\n\n" % (s, b'=' * encoding.colwidth(s)) | ||
Dan Villiom Podlaski Christiansen
|
r18748 | |||
Augie Fackler
|
r43346 | |||
Dan Villiom Podlaski Christiansen
|
r18748 | def subsubsection(s): | ||
Augie Fackler
|
r43347 | return b"%s\n%s\n\n" % (s, b"-" * encoding.colwidth(s)) | ||
Dan Villiom Podlaski Christiansen
|
r18748 | |||
Augie Fackler
|
r43346 | |||
Dan Villiom Podlaski Christiansen
|
r18748 | def subsubsubsection(s): | ||
Augie Fackler
|
r43347 | return b"%s\n%s\n\n" % (s, b"." * encoding.colwidth(s)) | ||
Dan Villiom Podlaski Christiansen
|
r18748 | |||
Augie Fackler
|
r43346 | |||
Sietse Brouwer
|
r42437 | def subsubsubsubsection(s): | ||
Augie Fackler
|
r43347 | return b"%s\n%s\n\n" % (s, b"'" * encoding.colwidth(s)) | ||
Sietse Brouwer
|
r42437 | |||
Augie Fackler
|
r43346 | |||
FUJIWARA Katsunori
|
r11464 | def replace(text, substs): | ||
Augie Fackler
|
r46554 | """ | ||
FUJIWARA Katsunori
|
r15393 | Apply a list of (find, replace) pairs to a text. | ||
Yuya Nishihara
|
r34133 | >>> replace(b"foo bar", [(b'f', b'F'), (b'b', b'B')]) | ||
FUJIWARA Katsunori
|
r15393 | 'Foo Bar' | ||
Yuya Nishihara
|
r34133 | >>> encoding.encoding = b'latin1' | ||
>>> replace(b'\\x81\\\\', [(b'\\\\', b'/')]) | ||||
FUJIWARA Katsunori
|
r15393 | '\\x81/' | ||
Yuya Nishihara
|
r34133 | >>> encoding.encoding = b'shiftjis' | ||
>>> replace(b'\\x81\\\\', [(b'\\\\', b'/')]) | ||||
FUJIWARA Katsunori
|
r15393 | '\\x81\\\\' | ||
Augie Fackler
|
r46554 | """ | ||
FUJIWARA Katsunori
|
r15393 | |||
# some character encodings (cp932 for Japanese, at least) use | ||||
# ASCII characters other than control/alphabet/digit as a part of | ||||
# multi-bytes characters, so direct replacing with such characters | ||||
# on strings in local encoding causes invalid byte sequences. | ||||
Pulkit Goyal
|
r31318 | utext = text.decode(pycompat.sysstr(encoding.encoding)) | ||
FUJIWARA Katsunori
|
r11464 | for f, t in substs: | ||
Matt Mackall
|
r21745 | utext = utext.replace(f.decode("ascii"), t.decode("ascii")) | ||
Pulkit Goyal
|
r31318 | return utext.encode(pycompat.sysstr(encoding.encoding)) | ||
Martin Geisler
|
r12651 | |||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
r31317 | _blockre = re.compile(br"\n(?:\s*\n)+") | ||
Martin Geisler
|
r12651 | |||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r9156 | 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. | ||||
""" | ||||
Martin Geisler
|
r12651 | blocks = [] | ||
Augie Fackler
|
r43347 | for b in _blockre.split(text.lstrip(b'\n').rstrip()): | ||
Martin Geisler
|
r12651 | lines = b.splitlines() | ||
Matt Mackall
|
r15123 | if lines: | ||
indent = min((len(l) - len(l.lstrip())) for l in lines) | ||||
lines = [l[indent:] for l in lines] | ||||
Augie Fackler
|
r43347 | blocks.append({b'indent': indent, b'lines': lines}) | ||
Martin Geisler
|
r9156 | return blocks | ||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r9156 | 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 | | ||||
# +---------------------------+ | ||||
Augie Fackler
|
r43347 | blocks[i][b'type'] = b'paragraph' | ||
if blocks[i][b'lines'][-1].endswith(b'::') and i + 1 < len(blocks): | ||||
indent = blocks[i][b'indent'] | ||||
adjustment = blocks[i + 1][b'indent'] - indent | ||||
Martin Geisler
|
r9156 | |||
Augie Fackler
|
r43347 | if blocks[i][b'lines'] == [b'::']: | ||
Martin Geisler
|
r9156 | # Expanded form: remove block | ||
del blocks[i] | ||||
i -= 1 | ||||
Augie Fackler
|
r43347 | elif blocks[i][b'lines'][-1].endswith(b' ::'): | ||
Martin Geisler
|
r9156 | # Partially minimized form: remove space and both | ||
# colons. | ||||
Augie Fackler
|
r43347 | blocks[i][b'lines'][-1] = blocks[i][b'lines'][-1][:-3] | ||
Augie Fackler
|
r43346 | elif ( | ||
Augie Fackler
|
r43347 | len(blocks[i][b'lines']) == 1 | ||
and blocks[i][b'lines'][0].lstrip(b' ').startswith(b'.. ') | ||||
and blocks[i][b'lines'][0].find(b' ', 3) == -1 | ||||
Augie Fackler
|
r43346 | ): | ||
Mads Kiilerich
|
r20549 | # directive on its own line, not a literal block | ||
Simon Heimberg
|
r19992 | i += 1 | ||
continue | ||||
Martin Geisler
|
r9156 | else: | ||
# Fully minimized form: remove just one colon. | ||||
Augie Fackler
|
r43347 | blocks[i][b'lines'][-1] = blocks[i][b'lines'][-1][:-1] | ||
Martin Geisler
|
r9156 | |||
# 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. | ||||
Augie Fackler
|
r43347 | m = _bulletre.match(blocks[i][b'lines'][0]) | ||
Martin Geisler
|
r9738 | if m: | ||
indent += m.end() | ||||
adjustment -= m.end() | ||||
Martin Geisler
|
r9156 | |||
# Mark the following indented blocks. | ||||
Augie Fackler
|
r43347 | while i + 1 < len(blocks) and blocks[i + 1][b'indent'] > indent: | ||
blocks[i + 1][b'type'] = b'literal' | ||||
blocks[i + 1][b'indent'] -= adjustment | ||||
Martin Geisler
|
r9156 | i += 1 | ||
i += 1 | ||||
return blocks | ||||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
r31317 | _bulletre = re.compile(br'(\*|-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)|\|) ') | ||
Augie Fackler
|
r43346 | _optionre = re.compile( | ||
br'^(-([a-zA-Z0-9]), )?(--[a-z0-9-]+)' br'((.*) +)(.*)$' | ||||
) | ||||
r47114 | _fieldre = re.compile(br':(?![: ])((?:\:|[^:])*)(?<! ):[ ]+(.*)') | |||
Pulkit Goyal
|
r31317 | _definitionre = re.compile(br'[^ ]') | ||
_tablere = re.compile(br'(=+\s+)*=+') | ||||
Martin Geisler
|
r9737 | |||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r9737 | 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. | ||||
Augie Fackler
|
r43346 | listtypes = [ | ||
Augie Fackler
|
r43347 | (b'bullet', _bulletre, True), | ||
(b'option', _optionre, True), | ||||
(b'field', _fieldre, True), | ||||
(b'definition', _definitionre, False), | ||||
Augie Fackler
|
r43346 | ] | ||
Martin Geisler
|
r9737 | |||
def match(lines, i, itemre, singleline): | ||||
"""Does itemre match an item at line i? | ||||
Mads Kiilerich
|
r17424 | A list item can be followed by an indented line or another list | ||
Martin Geisler
|
r9737 | item (but only if singleline is True). | ||
""" | ||||
line1 = lines[i] | ||||
Augie Fackler
|
r43347 | line2 = i + 1 < len(lines) and lines[i + 1] or b'' | ||
Martin Geisler
|
r9737 | if not itemre.match(line1): | ||
return False | ||||
if singleline: | ||||
Augie Fackler
|
r43347 | return line2 == b'' or line2[0:1] == b' ' or itemre.match(line2) | ||
Martin Geisler
|
r9737 | else: | ||
Augie Fackler
|
r43347 | return line2.startswith(b' ') | ||
Martin Geisler
|
r9737 | |||
i = 0 | ||||
while i < len(blocks): | ||||
Augie Fackler
|
r43347 | if blocks[i][b'type'] == b'paragraph': | ||
lines = blocks[i][b'lines'] | ||||
Martin Geisler
|
r9737 | 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): | ||||
Augie Fackler
|
r43346 | items.append( | ||
{ | ||||
Augie Fackler
|
r43347 | b'type': type, | ||
b'lines': [], | ||||
b'indent': blocks[i][b'indent'], | ||||
Augie Fackler
|
r43346 | } | ||
) | ||||
Augie Fackler
|
r43347 | items[-1][b'lines'].append(line) | ||
Augie Fackler
|
r43346 | blocks[i : i + 1] = items | ||
Martin Geisler
|
r9737 | break | ||
i += 1 | ||||
return blocks | ||||
Augie Fackler
|
r43346 | |||
Olav Reinert
|
r15861 | _fieldwidth = 14 | ||
Martin Geisler
|
r10065 | |||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r10065 | def updatefieldlists(blocks): | ||
Olav Reinert
|
r15861 | """Find key for field lists.""" | ||
Martin Geisler
|
r10065 | i = 0 | ||
while i < len(blocks): | ||||
Augie Fackler
|
r43347 | if blocks[i][b'type'] != b'field': | ||
Martin Geisler
|
r10065 | i += 1 | ||
continue | ||||
j = i | ||||
Augie Fackler
|
r43347 | while j < len(blocks) and blocks[j][b'type'] == b'field': | ||
m = _fieldre.match(blocks[j][b'lines'][0]) | ||||
Martin Geisler
|
r10065 | key, rest = m.groups() | ||
Augie Fackler
|
r43347 | blocks[j][b'lines'][0] = rest | ||
r47114 | blocks[j][b'key'] = key.replace(br'\:', b':') | |||
Martin Geisler
|
r10065 | j += 1 | ||
i = j + 1 | ||||
return blocks | ||||
Augie Fackler
|
r43346 | |||
Erik Zielke
|
r13011 | def updateoptionlists(blocks): | ||
i = 0 | ||||
while i < len(blocks): | ||||
Augie Fackler
|
r43347 | if blocks[i][b'type'] != b'option': | ||
Erik Zielke
|
r13011 | i += 1 | ||
continue | ||||
optstrwidth = 0 | ||||
j = i | ||||
Augie Fackler
|
r43347 | while j < len(blocks) and blocks[j][b'type'] == b'option': | ||
m = _optionre.match(blocks[j][b'lines'][0]) | ||||
Erik Zielke
|
r13011 | |||
shortoption = m.group(2) | ||||
group3 = m.group(3) | ||||
longoption = group3[2:].strip() | ||||
desc = m.group(6).strip() | ||||
longoptionarg = m.group(5).strip() | ||||
Augie Fackler
|
r43347 | blocks[j][b'lines'][0] = desc | ||
Erik Zielke
|
r13011 | |||
Augie Fackler
|
r43347 | noshortop = b'' | ||
Erik Zielke
|
r13011 | if not shortoption: | ||
Augie Fackler
|
r43347 | noshortop = b' ' | ||
Erik Zielke
|
r13011 | |||
Augie Fackler
|
r43347 | opt = b"%s%s" % ( | ||
shortoption and b"-%s " % shortoption or b'', | ||||
b"%s--%s %s" % (noshortop, longoption, longoptionarg), | ||||
Augie Fackler
|
r43346 | ) | ||
Erik Zielke
|
r13011 | opt = opt.rstrip() | ||
Augie Fackler
|
r43347 | blocks[j][b'optstr'] = opt | ||
Erik Zielke
|
r13011 | optstrwidth = max(optstrwidth, encoding.colwidth(opt)) | ||
j += 1 | ||||
for block in blocks[i:j]: | ||||
Augie Fackler
|
r43347 | block[b'optstrwidth'] = optstrwidth | ||
Erik Zielke
|
r13011 | i = j + 1 | ||
return blocks | ||||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r10443 | def prunecontainers(blocks, keep): | ||
"""Prune unwanted containers. | ||||
The blocks must have a 'type' field, i.e., they should have been | ||||
run through findliteralblocks first. | ||||
""" | ||||
Martin Geisler
|
r10444 | pruned = [] | ||
Martin Geisler
|
r10443 | i = 0 | ||
while i + 1 < len(blocks): | ||||
# Searching for a block that looks like this: | ||||
# | ||||
# +-------+---------------------------+ | ||||
# | ".. container ::" type | | ||||
# +---+ | | ||||
# | blocks | | ||||
# +-------------------------------+ | ||||
Augie Fackler
|
r43347 | if blocks[i][b'type'] == b'paragraph' and blocks[i][b'lines'][ | ||
Augie Fackler
|
r43346 | 0 | ||
Augie Fackler
|
r43347 | ].startswith(b'.. container::'): | ||
indent = blocks[i][b'indent'] | ||||
adjustment = blocks[i + 1][b'indent'] - indent | ||||
containertype = blocks[i][b'lines'][0][15:] | ||||
Matt Mackall
|
r22584 | prune = True | ||
for c in keep: | ||||
Augie Fackler
|
r43347 | if c in containertype.split(b'.'): | ||
Matt Mackall
|
r22584 | prune = False | ||
Martin Geisler
|
r10444 | if prune: | ||
pruned.append(containertype) | ||||
Martin Geisler
|
r10443 | |||
# Always delete "..container:: type" block | ||||
del blocks[i] | ||||
j = i | ||||
Matt Mackall
|
r15102 | i -= 1 | ||
Augie Fackler
|
r43347 | while j < len(blocks) and blocks[j][b'indent'] > indent: | ||
Martin Geisler
|
r10443 | if prune: | ||
del blocks[j] | ||||
else: | ||||
Augie Fackler
|
r43347 | blocks[j][b'indent'] -= adjustment | ||
Martin Geisler
|
r10443 | j += 1 | ||
i += 1 | ||||
Martin Geisler
|
r10444 | return blocks, pruned | ||
Martin Geisler
|
r10443 | |||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
r31317 | _sectionre = re.compile(br"""^([-=`:.'"~^_*+#])\1+$""") | ||
Martin Geisler
|
r10984 | |||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r15037 | def findtables(blocks): | ||
Augie Fackler
|
r46554 | """Find simple tables | ||
Matt Mackall
|
r15037 | |||
Augie Fackler
|
r46554 | Only simple one-line table elements are supported | ||
""" | ||||
Matt Mackall
|
r15037 | |||
for block in blocks: | ||||
# Searching for a block that looks like this: | ||||
# | ||||
# === ==== === | ||||
# A B C | ||||
# === ==== === <- optional | ||||
# 1 2 3 | ||||
# x y z | ||||
# === ==== === | ||||
Augie Fackler
|
r43346 | if ( | ||
Augie Fackler
|
r43347 | block[b'type'] == b'paragraph' | ||
and len(block[b'lines']) > 2 | ||||
and _tablere.match(block[b'lines'][0]) | ||||
and block[b'lines'][0] == block[b'lines'][-1] | ||||
Augie Fackler
|
r43346 | ): | ||
Augie Fackler
|
r43347 | block[b'type'] = b'table' | ||
block[b'header'] = False | ||||
div = block[b'lines'][0] | ||||
Matt Mackall
|
r15144 | |||
# column markers are ASCII so we can calculate column | ||||
# position in bytes | ||||
Augie Fackler
|
r43346 | columns = [ | ||
x | ||||
Manuel Jacob
|
r50179 | for x in range(len(div)) | ||
Augie Fackler
|
r43347 | if div[x : x + 1] == b'=' and (x == 0 or div[x - 1 : x] == b' ') | ||
Augie Fackler
|
r43346 | ] | ||
Matt Mackall
|
r15037 | rows = [] | ||
Augie Fackler
|
r43347 | for l in block[b'lines'][1:-1]: | ||
Matt Mackall
|
r15037 | if l == div: | ||
Augie Fackler
|
r43347 | block[b'header'] = True | ||
Matt Mackall
|
r15037 | continue | ||
row = [] | ||||
Matt Mackall
|
r15144 | # we measure columns not in bytes or characters but in | ||
# colwidth which makes things tricky | ||||
Augie Fackler
|
r43346 | pos = columns[0] # leading whitespace is bytes | ||
Matt Mackall
|
r15037 | for n, start in enumerate(columns): | ||
if n + 1 < len(columns): | ||||
Matt Mackall
|
r15144 | width = columns[n + 1] - start | ||
Augie Fackler
|
r43346 | v = encoding.getcols(l, pos, width) # gather columns | ||
pos += len(v) # calculate byte position of end | ||||
Matt Mackall
|
r15144 | row.append(v.strip()) | ||
Matt Mackall
|
r15037 | else: | ||
Matt Mackall
|
r15144 | row.append(l[pos:].strip()) | ||
Matt Mackall
|
r15037 | rows.append(row) | ||
Matt Mackall
|
r15144 | |||
Augie Fackler
|
r43347 | block[b'table'] = rows | ||
Matt Mackall
|
r15037 | |||
return blocks | ||||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r9156 | 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 | | ||||
# | ------------- | | ||||
# +------------------------------+ | ||||
Augie Fackler
|
r43346 | if ( | ||
Augie Fackler
|
r43347 | block[b'type'] == b'paragraph' | ||
and len(block[b'lines']) == 2 | ||||
and encoding.colwidth(block[b'lines'][0]) == len(block[b'lines'][1]) | ||||
and _sectionre.match(block[b'lines'][1]) | ||||
Augie Fackler
|
r43346 | ): | ||
Augie Fackler
|
r43347 | block[b'underline'] = block[b'lines'][1][0:1] | ||
block[b'type'] = b'section' | ||||
del block[b'lines'][1] | ||||
Martin Geisler
|
r9156 | return blocks | ||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r9623 | def inlineliterals(blocks): | ||
Augie Fackler
|
r43347 | substs = [(b'``', b'"')] | ||
Martin Geisler
|
r9623 | for b in blocks: | ||
Augie Fackler
|
r43347 | if b[b'type'] in (b'paragraph', b'section'): | ||
b[b'lines'] = [replace(l, substs) for l in b[b'lines']] | ||||
Martin Geisler
|
r9623 | return blocks | ||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r10972 | def hgrole(blocks): | ||
Augie Fackler
|
r43347 | substs = [(b':hg:`', b"'hg "), (b'`', b"'")] | ||
Martin Geisler
|
r10972 | for b in blocks: | ||
Augie Fackler
|
r43347 | if b[b'type'] in (b'paragraph', b'section'): | ||
Martin Geisler
|
r11192 | # 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). | ||||
Augie Fackler
|
r43347 | b[b'lines'] = [replace(l, substs) for l in b[b'lines']] | ||
Martin Geisler
|
r10972 | return blocks | ||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r9156 | 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): | ||||
Augie Fackler
|
r43347 | if blocks[i][b'type'] == blocks[i - 1][b'type'] and blocks[i][ | ||
b'type' | ||||
Augie Fackler
|
r46554 | ] in ( | ||
b'bullet', | ||||
b'option', | ||||
b'field', | ||||
): | ||||
Martin Geisler
|
r9156 | i += 1 | ||
Augie Fackler
|
r43347 | elif not blocks[i - 1][b'lines']: | ||
Mads Kiilerich
|
r20549 | # no lines in previous block, do not separate | ||
Simon Heimberg
|
r19995 | i += 1 | ||
Martin Geisler
|
r9156 | else: | ||
Augie Fackler
|
r43347 | blocks.insert( | ||
i, {b'lines': [b''], b'indent': 0, b'type': b'margin'} | ||||
) | ||||
Martin Geisler
|
r9156 | i += 2 | ||
return blocks | ||||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r12819 | def prunecomments(blocks): | ||
"""Remove comments.""" | ||||
i = 0 | ||||
while i < len(blocks): | ||||
b = blocks[i] | ||||
Augie Fackler
|
r43347 | if b[b'type'] == b'paragraph' and ( | ||
b[b'lines'][0].startswith(b'.. ') or b[b'lines'] == [b'..'] | ||||
Augie Fackler
|
r43346 | ): | ||
Martin Geisler
|
r12819 | del blocks[i] | ||
Augie Fackler
|
r43347 | if i < len(blocks) and blocks[i][b'type'] == b'margin': | ||
Martin Geisler
|
r13003 | del blocks[i] | ||
Martin Geisler
|
r12819 | else: | ||
i += 1 | ||||
return blocks | ||||
Gregory Szorc
|
r31131 | |||
Gregory Szorc
|
r31132 | def findadmonitions(blocks, admonitions=None): | ||
Erik Zielke
|
r12388 | """ | ||
Makes the type of the block an admonition block if | ||||
the first line is an admonition directive | ||||
""" | ||||
Gregory Szorc
|
r31714 | admonitions = admonitions or _admonitiontitles.keys() | ||
Gregory Szorc
|
r31132 | |||
Augie Fackler
|
r43346 | admonitionre = re.compile( | ||
Augie Fackler
|
r43347 | br'\.\. (%s)::' % b'|'.join(sorted(admonitions)), flags=re.IGNORECASE | ||
Augie Fackler
|
r43346 | ) | ||
Gregory Szorc
|
r31131 | |||
Erik Zielke
|
r12388 | i = 0 | ||
while i < len(blocks): | ||||
Augie Fackler
|
r43347 | m = admonitionre.match(blocks[i][b'lines'][0]) | ||
Erik Zielke
|
r12388 | if m: | ||
Augie Fackler
|
r43347 | blocks[i][b'type'] = b'admonition' | ||
admonitiontitle = blocks[i][b'lines'][0][3 : m.end() - 2].lower() | ||||
Erik Zielke
|
r12388 | |||
Augie Fackler
|
r43347 | firstline = blocks[i][b'lines'][0][m.end() + 1 :] | ||
Martin Geisler
|
r12620 | if firstline: | ||
Augie Fackler
|
r43347 | blocks[i][b'lines'].insert(1, b' ' + firstline) | ||
Erik Zielke
|
r12388 | |||
Augie Fackler
|
r43347 | blocks[i][b'admonitiontitle'] = admonitiontitle | ||
del blocks[i][b'lines'][0] | ||||
Erik Zielke
|
r12388 | i = i + 1 | ||
return blocks | ||||
Martin Geisler
|
r9156 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r31712 | _admonitiontitles = { | ||
Augie Fackler
|
r43347 | b'attention': _(b'Attention:'), | ||
b'caution': _(b'Caution:'), | ||||
b'danger': _(b'!Danger!'), | ||||
b'error': _(b'Error:'), | ||||
b'hint': _(b'Hint:'), | ||||
b'important': _(b'Important:'), | ||||
b'note': _(b'Note:'), | ||||
b'tip': _(b'Tip:'), | ||||
b'warning': _(b'Warning!'), | ||||
Gregory Szorc
|
r31712 | } | ||
Martin Geisler
|
r12652 | |||
Augie Fackler
|
r43346 | |||
Erik Zielke
|
r13011 | def formatoption(block, width): | ||
Augie Fackler
|
r43347 | desc = b' '.join(map(bytes.strip, block[b'lines'])) | ||
colwidth = encoding.colwidth(block[b'optstr']) | ||||
Erik Zielke
|
r13011 | usablewidth = width - 1 | ||
Augie Fackler
|
r43347 | hanging = block[b'optstrwidth'] | ||
initindent = b'%s%s ' % (block[b'optstr'], b' ' * ((hanging - colwidth))) | ||||
hangindent = b' ' * (encoding.colwidth(initindent) + 1) | ||||
return b' %s\n' % ( | ||||
Augie Fackler
|
r43346 | stringutil.wrap( | ||
desc, usablewidth, initindent=initindent, hangindent=hangindent | ||||
) | ||||
) | ||||
Erik Zielke
|
r13011 | |||
Martin Geisler
|
r9156 | def formatblock(block, width): | ||
"""Format a block according to width.""" | ||||
Martin Geisler
|
r9417 | if width <= 0: | ||
width = 78 | ||||
Augie Fackler
|
r43347 | indent = b' ' * block[b'indent'] | ||
if block[b'type'] == b'admonition': | ||||
admonition = _admonitiontitles[block[b'admonitiontitle']] | ||||
if not block[b'lines']: | ||||
return indent + admonition + b'\n' | ||||
hang = len(block[b'lines'][-1]) - len(block[b'lines'][-1].lstrip()) | ||||
Erik Zielke
|
r12388 | |||
Augie Fackler
|
r43347 | defindent = indent + hang * b' ' | ||
text = b' '.join(map(bytes.strip, block[b'lines'])) | ||||
return b'%s\n%s\n' % ( | ||||
Augie Fackler
|
r43346 | indent + admonition, | ||
stringutil.wrap( | ||||
text, width=width, initindent=defindent, hangindent=defindent | ||||
), | ||||
) | ||||
Augie Fackler
|
r43347 | if block[b'type'] == b'margin': | ||
return b'\n' | ||||
if block[b'type'] == b'literal': | ||||
indent += b' ' | ||||
return indent + (b'\n' + indent).join(block[b'lines']) + b'\n' | ||||
if block[b'type'] == b'section': | ||||
underline = encoding.colwidth(block[b'lines'][0]) * block[b'underline'] | ||||
return b"%s%s\n%s%s\n" % (indent, block[b'lines'][0], indent, underline) | ||||
if block[b'type'] == b'table': | ||||
table = block[b'table'] | ||||
Matt Mackall
|
r15037 | # compute column widths | ||
widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)] | ||||
Augie Fackler
|
r43347 | text = b'' | ||
Matt Mackall
|
r15037 | span = sum(widths) + len(widths) - 1 | ||
Augie Fackler
|
r43347 | indent = b' ' * block[b'indent'] | ||
hang = b' ' * (len(indent) + span - widths[-1]) | ||||
Matt Mackall
|
r15037 | |||
for row in table: | ||||
Matt Mackall
|
r15144 | l = [] | ||
for w, v in zip(widths, row): | ||||
Augie Fackler
|
r43347 | pad = b' ' * (w - encoding.colwidth(v)) | ||
Matt Mackall
|
r15144 | l.append(v + pad) | ||
Augie Fackler
|
r43347 | l = b' '.join(l) | ||
Augie Fackler
|
r43346 | l = stringutil.wrap( | ||
l, width=width, initindent=indent, hangindent=hang | ||||
) | ||||
Augie Fackler
|
r43347 | if not text and block[b'header']: | ||
text = l + b'\n' + indent + b'-' * (min(width, span)) + b'\n' | ||||
Matt Mackall
|
r15037 | else: | ||
Augie Fackler
|
r43347 | text += l + b"\n" | ||
Matt Mackall
|
r15037 | return text | ||
Augie Fackler
|
r43347 | if block[b'type'] == b'definition': | ||
term = indent + block[b'lines'][0] | ||||
hang = len(block[b'lines'][-1]) - len(block[b'lines'][-1].lstrip()) | ||||
defindent = indent + hang * b' ' | ||||
text = b' '.join(map(bytes.strip, block[b'lines'][1:])) | ||||
return b'%s\n%s\n' % ( | ||||
Augie Fackler
|
r43346 | term, | ||
stringutil.wrap( | ||||
text, width=width, initindent=defindent, hangindent=defindent | ||||
), | ||||
) | ||||
Martin Geisler
|
r10937 | subindent = indent | ||
Augie Fackler
|
r43347 | if block[b'type'] == b'bullet': | ||
if block[b'lines'][0].startswith(b'| '): | ||||
Martin Geisler
|
r10447 | # Remove bullet for line blocks and add no extra | ||
Mads Kiilerich
|
r26781 | # indentation. | ||
Augie Fackler
|
r43347 | block[b'lines'][0] = block[b'lines'][0][2:] | ||
Martin Geisler
|
r10447 | else: | ||
Augie Fackler
|
r43347 | m = _bulletre.match(block[b'lines'][0]) | ||
subindent = indent + m.end() * b' ' | ||||
elif block[b'type'] == b'field': | ||||
key = block[b'key'] | ||||
subindent = indent + _fieldwidth * b' ' | ||||
Martin Geisler
|
r10065 | if len(key) + 2 > _fieldwidth: | ||
# key too large, use full line width | ||||
key = key.ljust(width) | ||||
else: | ||||
Olav Reinert
|
r15861 | # key fits within field width | ||
Martin Geisler
|
r10065 | key = key.ljust(_fieldwidth) | ||
Augie Fackler
|
r43347 | block[b'lines'][0] = key + block[b'lines'][0] | ||
elif block[b'type'] == b'option': | ||||
Erik Zielke
|
r13011 | return formatoption(block, width) | ||
Martin Geisler
|
r9156 | |||
Augie Fackler
|
r43347 | text = b' '.join(map(bytes.strip, block[b'lines'])) | ||
Augie Fackler
|
r43346 | return ( | ||
stringutil.wrap( | ||||
text, width=width, initindent=indent, hangindent=subindent | ||||
) | ||||
Augie Fackler
|
r43347 | + b'\n' | ||
Augie Fackler
|
r43346 | ) | ||
Martin Geisler
|
r9156 | |||
Matt Mackall
|
r15261 | def formathtml(blocks): | ||
"""Format RST blocks as HTML""" | ||||
out = [] | ||||
Augie Fackler
|
r43347 | headernest = b'' | ||
Matt Mackall
|
r15261 | listnest = [] | ||
Dan Villiom Podlaski Christiansen
|
r18750 | def escape(s): | ||
Augie Fackler
|
r34696 | return url.escape(s, True) | ||
Dan Villiom Podlaski Christiansen
|
r18750 | |||
Matt Mackall
|
r15261 | def openlist(start, level): | ||
if not listnest or listnest[-1][0] != start: | ||||
listnest.append((start, level)) | ||||
Augie Fackler
|
r43347 | out.append(b'<%s>\n' % start) | ||
Matt Mackall
|
r15261 | |||
Augie Fackler
|
r43347 | blocks = [b for b in blocks if b[b'type'] != b'margin'] | ||
Matt Mackall
|
r15261 | |||
for pos, b in enumerate(blocks): | ||||
Augie Fackler
|
r43347 | btype = b[b'type'] | ||
level = b[b'indent'] | ||||
lines = b[b'lines'] | ||||
Matt Mackall
|
r15261 | |||
Augie Fackler
|
r43347 | if btype == b'admonition': | ||
admonition = escape(_admonitiontitles[b[b'admonitiontitle']]) | ||||
text = escape(b' '.join(map(bytes.strip, lines))) | ||||
out.append(b'<p>\n<b>%s</b> %s\n</p>\n' % (admonition, text)) | ||||
elif btype == b'paragraph': | ||||
out.append(b'<p>\n%s\n</p>\n' % escape(b'\n'.join(lines))) | ||||
elif btype == b'margin': | ||||
Matt Mackall
|
r15261 | pass | ||
Augie Fackler
|
r43347 | elif btype == b'literal': | ||
out.append(b'<pre>\n%s\n</pre>\n' % escape(b'\n'.join(lines))) | ||||
elif btype == b'section': | ||||
i = b[b'underline'] | ||||
Matt Mackall
|
r15261 | if i not in headernest: | ||
headernest += i | ||||
level = headernest.index(i) + 1 | ||||
Augie Fackler
|
r43347 | out.append(b'<h%d>%s</h%d>\n' % (level, escape(lines[0]), level)) | ||
elif btype == b'table': | ||||
table = b[b'table'] | ||||
out.append(b'<table>\n') | ||||
Matt Mackall
|
r15261 | for row in table: | ||
Augie Fackler
|
r43347 | out.append(b'<tr>') | ||
Dan Villiom Podlaski Christiansen
|
r18750 | for v in row: | ||
Augie Fackler
|
r43347 | out.append(b'<td>') | ||
Dan Villiom Podlaski Christiansen
|
r18752 | out.append(escape(v)) | ||
Augie Fackler
|
r43347 | out.append(b'</td>') | ||
out.append(b'\n') | ||||
Dan Villiom Podlaski Christiansen
|
r18752 | out.pop() | ||
Augie Fackler
|
r43347 | out.append(b'</tr>\n') | ||
out.append(b'</table>\n') | ||||
elif btype == b'definition': | ||||
openlist(b'dl', level) | ||||
Dan Villiom Podlaski Christiansen
|
r18750 | term = escape(lines[0]) | ||
Augie Fackler
|
r43347 | text = escape(b' '.join(map(bytes.strip, lines[1:]))) | ||
out.append(b' <dt>%s\n <dd>%s\n' % (term, text)) | ||||
elif btype == b'bullet': | ||||
bullet, head = lines[0].split(b' ', 1) | ||||
if bullet in (b'*', b'-'): | ||||
openlist(b'ul', level) | ||||
Matt Mackall
|
r15261 | else: | ||
Augie Fackler
|
r43347 | openlist(b'ol', level) | ||
out.append(b' <li> %s\n' % escape(b' '.join([head] + lines[1:]))) | ||||
elif btype == b'field': | ||||
openlist(b'dl', level) | ||||
key = escape(b[b'key']) | ||||
text = escape(b' '.join(map(bytes.strip, lines))) | ||||
out.append(b' <dt>%s\n <dd>%s\n' % (key, text)) | ||||
elif btype == b'option': | ||||
openlist(b'dl', level) | ||||
opt = escape(b[b'optstr']) | ||||
desc = escape(b' '.join(map(bytes.strip, lines))) | ||||
out.append(b' <dt>%s\n <dd>%s\n' % (opt, desc)) | ||||
Matt Mackall
|
r15261 | |||
# close lists if indent level of next block is lower | ||||
if listnest: | ||||
start, level = listnest[-1] | ||||
if pos == len(blocks) - 1: | ||||
Augie Fackler
|
r43347 | out.append(b'</%s>\n' % start) | ||
Matt Mackall
|
r15261 | listnest.pop() | ||
else: | ||||
nb = blocks[pos + 1] | ||||
Augie Fackler
|
r43347 | ni = nb[b'indent'] | ||
Augie Fackler
|
r43346 | if ni < level or ( | ||
ni == level | ||||
Augie Fackler
|
r43347 | and nb[b'type'] not in b'definition bullet field option' | ||
Augie Fackler
|
r43346 | ): | ||
Augie Fackler
|
r43347 | out.append(b'</%s>\n' % start) | ||
Matt Mackall
|
r15261 | listnest.pop() | ||
Augie Fackler
|
r43347 | return b''.join(out) | ||
Matt Mackall
|
r15261 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r31132 | def parse(text, indent=0, keep=None, admonitions=None): | ||
Matt Mackall
|
r15012 | """Parse text into a list of blocks""" | ||
Martin Geisler
|
r9156 | blocks = findblocks(text) | ||
Martin Geisler
|
r9540 | for b in blocks: | ||
Augie Fackler
|
r43347 | b[b'indent'] += indent | ||
Martin Geisler
|
r9156 | blocks = findliteralblocks(blocks) | ||
Matt Mackall
|
r15037 | blocks = findtables(blocks) | ||
Martin Geisler
|
r10444 | blocks, pruned = prunecontainers(blocks, keep or []) | ||
Martin Geisler
|
r10983 | blocks = findsections(blocks) | ||
Martin Geisler
|
r9623 | blocks = inlineliterals(blocks) | ||
Martin Geisler
|
r10972 | blocks = hgrole(blocks) | ||
Martin Geisler
|
r9737 | blocks = splitparagraphs(blocks) | ||
Martin Geisler
|
r10065 | blocks = updatefieldlists(blocks) | ||
Erik Zielke
|
r13011 | blocks = updateoptionlists(blocks) | ||
Gregory Szorc
|
r31132 | blocks = findadmonitions(blocks, admonitions=admonitions) | ||
Martin Geisler
|
r13003 | blocks = addmargins(blocks) | ||
Martin Geisler
|
r12819 | blocks = prunecomments(blocks) | ||
Matt Mackall
|
r15012 | return blocks, pruned | ||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r15013 | def formatblocks(blocks, width): | ||
Augie Fackler
|
r43347 | text = b''.join(formatblock(b, width) for b in blocks) | ||
Matt Mackall
|
r15013 | return text | ||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r39343 | def formatplain(blocks, width): | ||
"""Format parsed blocks as plain text""" | ||||
Augie Fackler
|
r43347 | return b''.join(formatblock(b, width) for b in blocks) | ||
Yuya Nishihara
|
r39343 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | def format(text, width=80, indent=0, keep=None, style=b'plain', section=None): | ||
Matt Mackall
|
r15012 | """Parse and format the text according to width.""" | ||
blocks, pruned = parse(text, indent, keep or []) | ||||
Yuya Nishihara
|
r39341 | if section: | ||
blocks = filtersections(blocks, section) | ||||
Augie Fackler
|
r43347 | if style == b'html': | ||
Yuya Nishihara
|
r39346 | return formathtml(blocks) | ||
Yuya Nishihara
|
r39341 | else: | ||
Yuya Nishihara
|
r39346 | return formatplain(blocks, width=width) | ||
Yuya Nishihara
|
r39341 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r39341 | def filtersections(blocks, section): | ||
Yuya Nishihara
|
r39377 | """Select parsed blocks under the specified section | ||
The section name is separated by a dot, and matches the suffix of the | ||||
full section path. | ||||
""" | ||||
timeless@mozdev.org
|
r26113 | parents = [] | ||
Yuya Nishihara
|
r39376 | sections = _getsections(blocks) | ||
Yuya Nishihara
|
r39342 | blocks = [] | ||
i = 0 | ||||
lastparents = [] | ||||
synthetic = [] | ||||
collapse = True | ||||
while i < len(sections): | ||||
Yuya Nishihara
|
r39377 | path, nest, b = sections[i] | ||
Yuya Nishihara
|
r39342 | del parents[nest:] | ||
parents.append(i) | ||||
Augie Fackler
|
r43347 | if path == section or path.endswith(b'.' + section): | ||
Yuya Nishihara
|
r39342 | if lastparents != parents: | ||
llen = len(lastparents) | ||||
plen = len(parents) | ||||
if llen and llen != plen: | ||||
collapse = False | ||||
s = [] | ||||
Manuel Jacob
|
r50179 | for j in range(3, plen - 1): | ||
Yuya Nishihara
|
r39342 | parent = parents[j] | ||
Augie Fackler
|
r43346 | if j >= llen or lastparents[j] != parent: | ||
Yuya Nishihara
|
r39342 | s.append(len(blocks)) | ||
sec = sections[parent][2] | ||||
blocks.append(sec[0]) | ||||
blocks.append(sec[-1]) | ||||
if s: | ||||
synthetic.append(s) | ||||
timeless
|
r27614 | |||
Yuya Nishihara
|
r39342 | lastparents = parents[:] | ||
blocks.extend(b) | ||||
Jordi Gutiérrez Hermoso
|
r22770 | |||
Yuya Nishihara
|
r39342 | ## Also show all subnested sections | ||
while i + 1 < len(sections) and sections[i + 1][1] > nest: | ||||
i += 1 | ||||
blocks.extend(sections[i][2]) | ||||
i += 1 | ||||
if collapse: | ||||
synthetic.reverse() | ||||
for s in synthetic: | ||||
Augie Fackler
|
r43347 | path = [blocks[syn][b'lines'][0] for syn in s] | ||
Yuya Nishihara
|
r39342 | real = s[-1] + 2 | ||
Augie Fackler
|
r43347 | realline = blocks[real][b'lines'] | ||
realline[0] = b'"%s"' % b'.'.join(path + [realline[0]]).replace( | ||||
b'"', b'' | ||||
Augie Fackler
|
r43346 | ) | ||
del blocks[s[0] : real] | ||||
Jordi Gutiérrez Hermoso
|
r22770 | |||
Yuya Nishihara
|
r39341 | return blocks | ||
Martin Geisler
|
r9156 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r39376 | def _getsections(blocks): | ||
Yuya Nishihara
|
r39377 | '''return a list of (section path, nesting level, blocks) tuples''' | ||
Augie Fackler
|
r43347 | nest = b"" | ||
Yuya Nishihara
|
r39377 | names = () | ||
Matt Mackall
|
r15014 | secs = [] | ||
Matt Mackall
|
r22587 | |||
def getname(b): | ||||
Augie Fackler
|
r43347 | if b[b'type'] == b'field': | ||
x = b[b'key'] | ||||
Matt Harbison
|
r25723 | else: | ||
Augie Fackler
|
r43347 | x = b[b'lines'][0] | ||
x = encoding.lower(x).strip(b'"') | ||||
if b'(' in x: | ||||
x = x.split(b'(')[0] | ||||
Matt Mackall
|
r22587 | return x | ||
Matt Mackall
|
r15014 | for b in blocks: | ||
Augie Fackler
|
r43347 | if b[b'type'] == b'section': | ||
i = b[b'underline'] | ||||
Matt Mackall
|
r15014 | if i not in nest: | ||
nest += i | ||||
level = nest.index(i) + 1 | ||||
nest = nest[:level] | ||||
Yuya Nishihara
|
r39377 | names = names[:level] + (getname(b),) | ||
Augie Fackler
|
r43347 | secs.append((b'.'.join(names), level, [b])) | ||
elif b[b'type'] in (b'definition', b'field'): | ||||
i = b' ' | ||||
Matt Mackall
|
r22587 | if i not in nest: | ||
nest += i | ||||
level = nest.index(i) + 1 | ||||
nest = nest[:level] | ||||
timeless@mozdev.org
|
r26237 | for i in range(1, len(secs) + 1): | ||
sec = secs[-i] | ||||
if sec[1] < level: | ||||
break | ||||
Augie Fackler
|
r43347 | siblings = [a for a in sec[2] if a[b'type'] == b'definition'] | ||
timeless@mozdev.org
|
r26237 | if siblings: | ||
Augie Fackler
|
r43347 | siblingindent = siblings[-1][b'indent'] | ||
indent = b[b'indent'] | ||||
timeless@mozdev.org
|
r26237 | if siblingindent < indent: | ||
level += 1 | ||||
break | ||||
elif siblingindent == indent: | ||||
level = sec[1] | ||||
break | ||||
Yuya Nishihara
|
r39377 | names = names[:level] + (getname(b),) | ||
Augie Fackler
|
r43347 | secs.append((b'.'.join(names), level, [b])) | ||
Matt Mackall
|
r15014 | else: | ||
if not secs: | ||||
# add an initial empty section | ||||
Augie Fackler
|
r43347 | secs = [(b'', 0, [])] | ||
if b[b'type'] != b'margin': | ||||
timeless@mozdev.org
|
r26157 | pointer = 1 | ||
Augie Fackler
|
r43347 | bindent = b[b'indent'] | ||
timeless@mozdev.org
|
r26157 | while pointer < len(secs): | ||
section = secs[-pointer][2][0] | ||||
Augie Fackler
|
r43347 | if section[b'type'] != b'margin': | ||
sindent = section[b'indent'] | ||||
if len(section[b'lines']) > 1: | ||||
sindent += len(section[b'lines'][1]) - len( | ||||
section[b'lines'][1].lstrip(b' ') | ||||
Augie Fackler
|
r43346 | ) | ||
timeless@mozdev.org
|
r26157 | if bindent >= sindent: | ||
break | ||||
pointer += 1 | ||||
if pointer > 1: | ||||
timeless@mozdev.org
|
r26170 | blevel = secs[-pointer][1] | ||
Augie Fackler
|
r43347 | if section[b'type'] != b[b'type']: | ||
timeless@mozdev.org
|
r26170 | blevel += 1 | ||
Augie Fackler
|
r43347 | secs.append((b'', blevel, [])) | ||
Matt Mackall
|
r15014 | secs[-1][2].append(b) | ||
return secs | ||||
Martin Geisler
|
r9156 | |||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r15039 | def maketable(data, indent=0, header=False): | ||
Olav Reinert
|
r16815 | '''Generate an RST table for the given table data as a list of lines''' | ||
Matt Mackall
|
r15039 | |||
widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)] | ||||
Augie Fackler
|
r43347 | indent = b' ' * indent | ||
div = indent + b' '.join(b'=' * w for w in widths) + b'\n' | ||||
Matt Mackall
|
r15039 | |||
out = [div] | ||||
for row in data: | ||||
Matt Mackall
|
r15144 | l = [] | ||
for w, v in zip(widths, row): | ||||
Augie Fackler
|
r43347 | if b'\n' in v: | ||
Simon Heimberg
|
r20654 | # only remove line breaks and indentation, long lines are | ||
# handled by the next tool | ||||
Augie Fackler
|
r43347 | v = b' '.join(e.lstrip() for e in v.split(b'\n')) | ||
pad = b' ' * (w - encoding.colwidth(v)) | ||||
Matt Mackall
|
r15144 | l.append(v + pad) | ||
Augie Fackler
|
r43347 | out.append(indent + b' '.join(l) + b"\n") | ||
Matt Mackall
|
r15039 | if header and len(data) > 1: | ||
out.insert(2, div) | ||||
out.append(div) | ||||
Olav Reinert
|
r16815 | return out | ||