##// END OF EJS Templates
hg-cpython: use ancestor iterator impls from vcsgraph...
hg-cpython: use ancestor iterator impls from vcsgraph Differential Revision: https://phab.mercurial-scm.org/D11947

File last commit:

r47575:d4ba4d51 default
r49350:35ebe6f8 default
Show More
minirst.py
900 lines | 29.1 KiB | text/x-python | PythonLexer
Martin Geisler
minimal reStructuredText parser
r9156 # minirst.py - minimal reStructuredText parser
#
Raphaël Gomès
contributor: change mentions of mpm to olivia...
r47575 # Copyright 2009, 2010 Olivia Mackall <olivia@selenic.com> and others
Martin Geisler
minimal reStructuredText parser
r9156 #
# This software may be used and distributed according to the terms of the
Matt Mackall
Update license to GPLv2+
r10263 # GNU General Public License version 2 or any later version.
Martin Geisler
minimal reStructuredText parser
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
urls: bulk-change primary website URLs
r26421 Remember to update https://mercurial-scm.org/wiki/HelpStyleGuide
Martin Geisler
minirst: link to HelpStyleGuide in docstring
r12958 when adding support for new constructs.
Martin Geisler
minimal reStructuredText parser
r9156 """
Gregory Szorc
minirst: use absolute_import
r25960 from __future__ import absolute_import
Erik Zielke
minirst: Support for admonitions...
r12388
Gregory Szorc
minirst: use absolute_import
r25960 import re
from .i18n import _
from . import (
encoding,
Pulkit Goyal
minirst: make encoding.encoding unicodes to pass into encode() and decode()
r31318 pycompat,
Augie Fackler
python3: use our bytes-only version of cgi.escape everywhere...
r34696 url,
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 )
Augie Fackler
formatting: blacken the codebase...
r43346 from .utils import stringutil
Dan Villiom Podlaski Christiansen
minirst: CGI escape strings prior to embedding it in the HTML
r18750
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748 def section(s):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b"%s\n%s\n\n" % (s, b"\"" * encoding.colwidth(s))
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748
Augie Fackler
formatting: blacken the codebase...
r43346
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748 def subsection(s):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b"%s\n%s\n\n" % (s, b'=' * encoding.colwidth(s))
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748
Augie Fackler
formatting: blacken the codebase...
r43346
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748 def subsubsection(s):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b"%s\n%s\n\n" % (s, b"-" * encoding.colwidth(s))
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748
Augie Fackler
formatting: blacken the codebase...
r43346
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748 def subsubsubsection(s):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b"%s\n%s\n\n" % (s, b"." * encoding.colwidth(s))
Dan Villiom Podlaski Christiansen
help: use a full header for topic titles...
r18748
Augie Fackler
formatting: blacken the codebase...
r43346
Sietse Brouwer
minirst: support subsubsubsubsections (header level 5) with marker ''''...
r42437 def subsubsubsubsection(s):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b"%s\n%s\n\n" % (s, b"'" * encoding.colwidth(s))
Sietse Brouwer
minirst: support subsubsubsubsections (header level 5) with marker ''''...
r42437
Augie Fackler
formatting: blacken the codebase...
r43346
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
r11464 def replace(text, substs):
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
r15393 Apply a list of (find, replace) pairs to a text.
Yuya Nishihara
doctest: bulk-replace string literals with b'' for Python 3...
r34133 >>> replace(b"foo bar", [(b'f', b'F'), (b'b', b'B')])
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
r15393 'Foo Bar'
Yuya Nishihara
doctest: bulk-replace string literals with b'' for Python 3...
r34133 >>> encoding.encoding = b'latin1'
>>> replace(b'\\x81\\\\', [(b'\\\\', b'/')])
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
r15393 '\\x81/'
Yuya Nishihara
doctest: bulk-replace string literals with b'' for Python 3...
r34133 >>> encoding.encoding = b'shiftjis'
>>> replace(b'\\x81\\\\', [(b'\\\\', b'/')])
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
r15393 '\\x81\\\\'
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
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
minirst: make encoding.encoding unicodes to pass into encode() and decode()
r31318 utext = text.decode(pycompat.sysstr(encoding.encoding))
FUJIWARA Katsunori
minirst: use unicode string as intermediate form for replacement...
r11464 for f, t in substs:
Matt Mackall
minirst: explicitly decode substitutions...
r21745 utext = utext.replace(f.decode("ascii"), t.decode("ascii"))
Pulkit Goyal
minirst: make encoding.encoding unicodes to pass into encode() and decode()
r31318 return utext.encode(pycompat.sysstr(encoding.encoding))
Martin Geisler
minirst: refactor/simplify findblocks
r12651
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
minirst: make regular expressions bytes
r31317 _blockre = re.compile(br"\n(?:\s*\n)+")
Martin Geisler
minirst: refactor/simplify findblocks
r12651
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minimal reStructuredText parser
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
minirst: refactor/simplify findblocks
r12651 blocks = []
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 for b in _blockre.split(text.lstrip(b'\n').rstrip()):
Martin Geisler
minirst: refactor/simplify findblocks
r12651 lines = b.splitlines()
Matt Mackall
minirst: don't choke on empty text
r15123 if lines:
indent = min((len(l) - len(l.lstrip())) for l in lines)
lines = [l[indent:] for l in lines]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks.append({b'indent': indent, b'lines': lines})
Martin Geisler
minimal reStructuredText parser
r9156 return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minimal reStructuredText parser
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
formatting: byteify all mercurial/ and hgext/ string literals...
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
minimal reStructuredText parser
r9156
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if blocks[i][b'lines'] == [b'::']:
Martin Geisler
minimal reStructuredText parser
r9156 # Expanded form: remove block
del blocks[i]
i -= 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif blocks[i][b'lines'][-1].endswith(b' ::'):
Martin Geisler
minimal reStructuredText parser
r9156 # Partially minimized form: remove space and both
# colons.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[i][b'lines'][-1] = blocks[i][b'lines'][-1][:-3]
Augie Fackler
formatting: blacken the codebase...
r43346 elif (
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
formatting: blacken the codebase...
r43346 ):
Mads Kiilerich
comments: fix minor spelling issues found with spell checker
r20549 # directive on its own line, not a literal block
Simon Heimberg
minirst: do not interpret a directive as a literal block...
r19992 i += 1
continue
Martin Geisler
minimal reStructuredText parser
r9156 else:
# Fully minimized form: remove just one colon.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[i][b'lines'][-1] = blocks[i][b'lines'][-1][:-1]
Martin Geisler
minimal reStructuredText parser
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
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 m = _bulletre.match(blocks[i][b'lines'][0])
Martin Geisler
minirst: prepare for general types of bullet lists...
r9738 if m:
indent += m.end()
adjustment -= m.end()
Martin Geisler
minimal reStructuredText parser
r9156
# Mark the following indented blocks.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minimal reStructuredText parser
r9156 i += 1
i += 1
return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
minirst: make regular expressions bytes
r31317 _bulletre = re.compile(br'(\*|-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)|\|) ')
Augie Fackler
formatting: blacken the codebase...
r43346 _optionre = re.compile(
br'^(-([a-zA-Z0-9]), )?(--[a-z0-9-]+)' br'((.*) +)(.*)$'
)
minirst: respect escaping in definition list key...
r47114 _fieldre = re.compile(br':(?![: ])((?:\:|[^:])*)(?<! ):[ ]+(.*)')
Pulkit Goyal
minirst: make regular expressions bytes
r31317 _definitionre = re.compile(br'[^ ]')
_tablere = re.compile(br'(=+\s+)*=+')
Martin Geisler
minirst: combine list parsing in one function...
r9737
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minirst: combine list parsing in one function...
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
formatting: blacken the codebase...
r43346 listtypes = [
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 (b'bullet', _bulletre, True),
(b'option', _optionre, True),
(b'field', _fieldre, True),
(b'definition', _definitionre, False),
Augie Fackler
formatting: blacken the codebase...
r43346 ]
Martin Geisler
minirst: combine list parsing in one function...
r9737
def match(lines, i, itemre, singleline):
"""Does itemre match an item at line i?
Mads Kiilerich
fix trivial spelling errors
r17424 A list item can be followed by an indented line or another list
Martin Geisler
minirst: combine list parsing in one function...
r9737 item (but only if singleline is True).
"""
line1 = lines[i]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 line2 = i + 1 < len(lines) and lines[i + 1] or b''
Martin Geisler
minirst: combine list parsing in one function...
r9737 if not itemre.match(line1):
return False
if singleline:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return line2 == b'' or line2[0:1] == b' ' or itemre.match(line2)
Martin Geisler
minirst: combine list parsing in one function...
r9737 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return line2.startswith(b' ')
Martin Geisler
minirst: combine list parsing in one function...
r9737
i = 0
while i < len(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if blocks[i][b'type'] == b'paragraph':
lines = blocks[i][b'lines']
Martin Geisler
minirst: combine list parsing in one function...
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
formatting: blacken the codebase...
r43346 items.append(
{
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'type': type,
b'lines': [],
b'indent': blocks[i][b'indent'],
Augie Fackler
formatting: blacken the codebase...
r43346 }
)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 items[-1][b'lines'].append(line)
Augie Fackler
formatting: blacken the codebase...
r43346 blocks[i : i + 1] = items
Martin Geisler
minirst: combine list parsing in one function...
r9737 break
i += 1
return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Olav Reinert
minirst: simplify and standardize field list formatting...
r15861 _fieldwidth = 14
Martin Geisler
minirst: improve layout of field lists...
r10065
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minirst: improve layout of field lists...
r10065 def updatefieldlists(blocks):
Olav Reinert
minirst: simplify and standardize field list formatting...
r15861 """Find key for field lists."""
Martin Geisler
minirst: improve layout of field lists...
r10065 i = 0
while i < len(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if blocks[i][b'type'] != b'field':
Martin Geisler
minirst: improve layout of field lists...
r10065 i += 1
continue
j = i
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 while j < len(blocks) and blocks[j][b'type'] == b'field':
m = _fieldre.match(blocks[j][b'lines'][0])
Martin Geisler
minirst: improve layout of field lists...
r10065 key, rest = m.groups()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[j][b'lines'][0] = rest
minirst: respect escaping in definition list key...
r47114 blocks[j][b'key'] = key.replace(br'\:', b':')
Martin Geisler
minirst: improve layout of field lists...
r10065 j += 1
i = j + 1
return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Erik Zielke
minirst: improved support for option lists....
r13011 def updateoptionlists(blocks):
i = 0
while i < len(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if blocks[i][b'type'] != b'option':
Erik Zielke
minirst: improved support for option lists....
r13011 i += 1
continue
optstrwidth = 0
j = i
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 while j < len(blocks) and blocks[j][b'type'] == b'option':
m = _optionre.match(blocks[j][b'lines'][0])
Erik Zielke
minirst: improved support for option lists....
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
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[j][b'lines'][0] = desc
Erik Zielke
minirst: improved support for option lists....
r13011
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 noshortop = b''
Erik Zielke
minirst: improved support for option lists....
r13011 if not shortoption:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 noshortop = b' '
Erik Zielke
minirst: improved support for option lists....
r13011
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 opt = b"%s%s" % (
shortoption and b"-%s " % shortoption or b'',
b"%s--%s %s" % (noshortop, longoption, longoptionarg),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Erik Zielke
minirst: improved support for option lists....
r13011 opt = opt.rstrip()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[j][b'optstr'] = opt
Erik Zielke
minirst: improved support for option lists....
r13011 optstrwidth = max(optstrwidth, encoding.colwidth(opt))
j += 1
for block in blocks[i:j]:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'optstrwidth'] = optstrwidth
Erik Zielke
minirst: improved support for option lists....
r13011 i = j + 1
return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minirst: support containers...
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
minirst: report pruned container types
r10444 pruned = []
Martin Geisler
minirst: support containers...
r10443 i = 0
while i + 1 < len(blocks):
# Searching for a block that looks like this:
#
# +-------+---------------------------+
# | ".. container ::" type |
# +---+ |
# | blocks |
# +-------------------------------+
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if blocks[i][b'type'] == b'paragraph' and blocks[i][b'lines'][
Augie Fackler
formatting: blacken the codebase...
r43346 0
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: allow multiple container types for prune...
r22584 prune = True
for c in keep:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if c in containertype.split(b'.'):
Matt Mackall
minirst: allow multiple container types for prune...
r22584 prune = False
Martin Geisler
minirst: report pruned container types
r10444 if prune:
pruned.append(containertype)
Martin Geisler
minirst: support containers...
r10443
# Always delete "..container:: type" block
del blocks[i]
j = i
Matt Mackall
minirst: fix container stripping logic
r15102 i -= 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 while j < len(blocks) and blocks[j][b'indent'] > indent:
Martin Geisler
minirst: support containers...
r10443 if prune:
del blocks[j]
else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[j][b'indent'] -= adjustment
Martin Geisler
minirst: support containers...
r10443 j += 1
i += 1
Martin Geisler
minirst: report pruned container types
r10444 return blocks, pruned
Martin Geisler
minirst: support containers...
r10443
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
minirst: make regular expressions bytes
r31317 _sectionre = re.compile(br"""^([-=`:.'"~^_*+#])\1+$""")
Martin Geisler
minirst: support all recommended title adornments
r10984
Augie Fackler
formatting: blacken the codebase...
r43346
Matt Mackall
minirst: add simple table support...
r15037 def findtables(blocks):
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 """Find simple tables
Matt Mackall
minirst: add simple table support...
r15037
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 Only simple one-line table elements are supported
"""
Matt Mackall
minirst: add simple table support...
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
formatting: blacken the codebase...
r43346 if (
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
formatting: blacken the codebase...
r43346 ):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'type'] = b'table'
block[b'header'] = False
div = block[b'lines'][0]
Matt Mackall
minirst: fix column handling for simple tables
r15144
# column markers are ASCII so we can calculate column
# position in bytes
Augie Fackler
formatting: blacken the codebase...
r43346 columns = [
x
for x in pycompat.xrange(len(div))
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if div[x : x + 1] == b'=' and (x == 0 or div[x - 1 : x] == b' ')
Augie Fackler
formatting: blacken the codebase...
r43346 ]
Matt Mackall
minirst: add simple table support...
r15037 rows = []
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 for l in block[b'lines'][1:-1]:
Matt Mackall
minirst: add simple table support...
r15037 if l == div:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'header'] = True
Matt Mackall
minirst: add simple table support...
r15037 continue
row = []
Matt Mackall
minirst: fix column handling for simple tables
r15144 # we measure columns not in bytes or characters but in
# colwidth which makes things tricky
Augie Fackler
formatting: blacken the codebase...
r43346 pos = columns[0] # leading whitespace is bytes
Matt Mackall
minirst: add simple table support...
r15037 for n, start in enumerate(columns):
if n + 1 < len(columns):
Matt Mackall
minirst: fix column handling for simple tables
r15144 width = columns[n + 1] - start
Augie Fackler
formatting: blacken the codebase...
r43346 v = encoding.getcols(l, pos, width) # gather columns
pos += len(v) # calculate byte position of end
Matt Mackall
minirst: fix column handling for simple tables
r15144 row.append(v.strip())
Matt Mackall
minirst: add simple table support...
r15037 else:
Matt Mackall
minirst: fix column handling for simple tables
r15144 row.append(l[pos:].strip())
Matt Mackall
minirst: add simple table support...
r15037 rows.append(row)
Matt Mackall
minirst: fix column handling for simple tables
r15144
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'table'] = rows
Matt Mackall
minirst: add simple table support...
r15037
return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minimal reStructuredText parser
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
formatting: blacken the codebase...
r43346 if (
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
formatting: blacken the codebase...
r43346 ):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'underline'] = block[b'lines'][1][0:1]
block[b'type'] = b'section'
del block[b'lines'][1]
Martin Geisler
minimal reStructuredText parser
r9156 return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minirst: convert ``foo`` into "foo" upon display...
r9623 def inlineliterals(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 substs = [(b'``', b'"')]
Martin Geisler
minirst: convert ``foo`` into "foo" upon display...
r9623 for b in blocks:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b[b'type'] in (b'paragraph', b'section'):
b[b'lines'] = [replace(l, substs) for l in b[b'lines']]
Martin Geisler
minirst: convert ``foo`` into "foo" upon display...
r9623 return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
doc, minirst: support hg interpreted text role
r10972 def hgrole(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 substs = [(b':hg:`', b"'hg "), (b'`', b"'")]
Martin Geisler
doc, minirst: support hg interpreted text role
r10972 for b in blocks:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b[b'type'] in (b'paragraph', b'section'):
Martin Geisler
minirst: handle line breaks in hg role
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
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b[b'lines'] = [replace(l, substs) for l in b[b'lines']]
Martin Geisler
doc, minirst: support hg interpreted text role
r10972 return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minimal reStructuredText parser
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
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if blocks[i][b'type'] == blocks[i - 1][b'type'] and blocks[i][
b'type'
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 ] in (
b'bullet',
b'option',
b'field',
):
Martin Geisler
minimal reStructuredText parser
r9156 i += 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif not blocks[i - 1][b'lines']:
Mads Kiilerich
comments: fix minor spelling issues found with spell checker
r20549 # no lines in previous block, do not separate
Simon Heimberg
minirst: do not add a 2nd empty paragraph...
r19995 i += 1
Martin Geisler
minimal reStructuredText parser
r9156 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks.insert(
i, {b'lines': [b''], b'indent': 0, b'type': b'margin'}
)
Martin Geisler
minimal reStructuredText parser
r9156 i += 2
return blocks
Augie Fackler
formatting: blacken the codebase...
r43346
Martin Geisler
minirst: ignore comments
r12819 def prunecomments(blocks):
"""Remove comments."""
i = 0
while i < len(blocks):
b = blocks[i]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b[b'type'] == b'paragraph' and (
b[b'lines'][0].startswith(b'.. ') or b[b'lines'] == [b'..']
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Martin Geisler
minirst: ignore comments
r12819 del blocks[i]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if i < len(blocks) and blocks[i][b'type'] == b'margin':
Martin Geisler
minirst: better interaction between comments and margins...
r13003 del blocks[i]
Martin Geisler
minirst: ignore comments
r12819 else:
i += 1
return blocks
Gregory Szorc
minirst: dynamically compile admonitions regexp...
r31131
Gregory Szorc
minirst: support passing admonitions into findadmonitions() and parse()...
r31132 def findadmonitions(blocks, admonitions=None):
Erik Zielke
minirst: Support for admonitions...
r12388 """
Makes the type of the block an admonition block if
the first line is an admonition directive
"""
Gregory Szorc
minirst: remove redundant _admonitions set...
r31714 admonitions = admonitions or _admonitiontitles.keys()
Gregory Szorc
minirst: support passing admonitions into findadmonitions() and parse()...
r31132
Augie Fackler
formatting: blacken the codebase...
r43346 admonitionre = re.compile(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 br'\.\. (%s)::' % b'|'.join(sorted(admonitions)), flags=re.IGNORECASE
Augie Fackler
formatting: blacken the codebase...
r43346 )
Gregory Szorc
minirst: dynamically compile admonitions regexp...
r31131
Erik Zielke
minirst: Support for admonitions...
r12388 i = 0
while i < len(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 m = admonitionre.match(blocks[i][b'lines'][0])
Erik Zielke
minirst: Support for admonitions...
r12388 if m:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[i][b'type'] = b'admonition'
admonitiontitle = blocks[i][b'lines'][0][3 : m.end() - 2].lower()
Erik Zielke
minirst: Support for admonitions...
r12388
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 firstline = blocks[i][b'lines'][0][m.end() + 1 :]
Martin Geisler
minirst: small code cleanup
r12620 if firstline:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[i][b'lines'].insert(1, b' ' + firstline)
Erik Zielke
minirst: Support for admonitions...
r12388
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks[i][b'admonitiontitle'] = admonitiontitle
del blocks[i][b'lines'][0]
Erik Zielke
minirst: Support for admonitions...
r12388 i = i + 1
return blocks
Martin Geisler
minimal reStructuredText parser
r9156
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
minirst: reindent _admonitiontitles...
r31712 _admonitiontitles = {
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: reindent _admonitiontitles...
r31712 }
Martin Geisler
minirst: pull admonition titles out formatblock function
r12652
Augie Fackler
formatting: blacken the codebase...
r43346
Erik Zielke
minirst: improved support for option lists....
r13011 def formatoption(block, width):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 desc = b' '.join(map(bytes.strip, block[b'lines']))
colwidth = encoding.colwidth(block[b'optstr'])
Erik Zielke
minirst: improved support for option lists....
r13011 usablewidth = width - 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
formatting: blacken the codebase...
r43346 stringutil.wrap(
desc, usablewidth, initindent=initindent, hangindent=hangindent
)
)
Erik Zielke
minirst: improved support for option lists....
r13011
Martin Geisler
minimal reStructuredText parser
r9156 def formatblock(block, width):
"""Format a block according to width."""
Martin Geisler
util, minirst: do not crash with COLUMNS=0
r9417 if width <= 0:
width = 78
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: Support for admonitions...
r12388
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 defindent = indent + hang * b' '
text = b' '.join(map(bytes.strip, block[b'lines']))
return b'%s\n%s\n' % (
Augie Fackler
formatting: blacken the codebase...
r43346 indent + admonition,
stringutil.wrap(
text, width=width, initindent=defindent, hangindent=defindent
),
)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: add simple table support...
r15037 # compute column widths
widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 text = b''
Matt Mackall
minirst: add simple table support...
r15037 span = sum(widths) + len(widths) - 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 indent = b' ' * block[b'indent']
hang = b' ' * (len(indent) + span - widths[-1])
Matt Mackall
minirst: add simple table support...
r15037
for row in table:
Matt Mackall
minirst: fix column handling for simple tables
r15144 l = []
for w, v in zip(widths, row):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 pad = b' ' * (w - encoding.colwidth(v))
Matt Mackall
minirst: fix column handling for simple tables
r15144 l.append(v + pad)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 l = b' '.join(l)
Augie Fackler
formatting: blacken the codebase...
r43346 l = stringutil.wrap(
l, width=width, initindent=indent, hangindent=hang
)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if not text and block[b'header']:
text = l + b'\n' + indent + b'-' * (min(width, span)) + b'\n'
Matt Mackall
minirst: add simple table support...
r15037 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 text += l + b"\n"
Matt Mackall
minirst: add simple table support...
r15037 return text
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
formatting: blacken the codebase...
r43346 term,
stringutil.wrap(
text, width=width, initindent=defindent, hangindent=defindent
),
)
Martin Geisler
minirst: removed unnecessary initindent variable
r10937 subindent = indent
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if block[b'type'] == b'bullet':
if block[b'lines'][0].startswith(b'| '):
Martin Geisler
minirst: support line blocks
r10447 # Remove bullet for line blocks and add no extra
Mads Kiilerich
spelling: trivial spell checking
r26781 # indentation.
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'lines'][0] = block[b'lines'][0][2:]
Martin Geisler
minirst: support line blocks
r10447 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: improve layout of field lists...
r10065 if len(key) + 2 > _fieldwidth:
# key too large, use full line width
key = key.ljust(width)
else:
Olav Reinert
minirst: simplify and standardize field list formatting...
r15861 # key fits within field width
Martin Geisler
minirst: improve layout of field lists...
r10065 key = key.ljust(_fieldwidth)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 block[b'lines'][0] = key + block[b'lines'][0]
elif block[b'type'] == b'option':
Erik Zielke
minirst: improved support for option lists....
r13011 return formatoption(block, width)
Martin Geisler
minimal reStructuredText parser
r9156
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 text = b' '.join(map(bytes.strip, block[b'lines']))
Augie Fackler
formatting: blacken the codebase...
r43346 return (
stringutil.wrap(
text, width=width, initindent=indent, hangindent=subindent
)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 + b'\n'
Augie Fackler
formatting: blacken the codebase...
r43346 )
Martin Geisler
minimal reStructuredText parser
r9156
Matt Mackall
minirst: add basic HTML formatting support
r15261 def formathtml(blocks):
"""Format RST blocks as HTML"""
out = []
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 headernest = b''
Matt Mackall
minirst: add basic HTML formatting support
r15261 listnest = []
Dan Villiom Podlaski Christiansen
minirst: CGI escape strings prior to embedding it in the HTML
r18750 def escape(s):
Augie Fackler
python3: use our bytes-only version of cgi.escape everywhere...
r34696 return url.escape(s, True)
Dan Villiom Podlaski Christiansen
minirst: CGI escape strings prior to embedding it in the HTML
r18750
Matt Mackall
minirst: add basic HTML formatting support
r15261 def openlist(start, level):
if not listnest or listnest[-1][0] != start:
listnest.append((start, level))
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'<%s>\n' % start)
Matt Mackall
minirst: add basic HTML formatting support
r15261
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 blocks = [b for b in blocks if b[b'type'] != b'margin']
Matt Mackall
minirst: add basic HTML formatting support
r15261
for pos, b in enumerate(blocks):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 btype = b[b'type']
level = b[b'indent']
lines = b[b'lines']
Matt Mackall
minirst: add basic HTML formatting support
r15261
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: add basic HTML formatting support
r15261 pass
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: add basic HTML formatting support
r15261 if i not in headernest:
headernest += i
level = headernest.index(i) + 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: add basic HTML formatting support
r15261 for row in table:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'<tr>')
Dan Villiom Podlaski Christiansen
minirst: CGI escape strings prior to embedding it in the HTML
r18750 for v in row:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'<td>')
Dan Villiom Podlaski Christiansen
minirst: optimize HTML table generation a bit...
r18752 out.append(escape(v))
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'</td>')
out.append(b'\n')
Dan Villiom Podlaski Christiansen
minirst: optimize HTML table generation a bit...
r18752 out.pop()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'</tr>\n')
out.append(b'</table>\n')
elif btype == b'definition':
openlist(b'dl', level)
Dan Villiom Podlaski Christiansen
minirst: CGI escape strings prior to embedding it in the HTML
r18750 term = escape(lines[0])
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: add basic HTML formatting support
r15261 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
minirst: add basic HTML formatting support
r15261
# close lists if indent level of next block is lower
if listnest:
start, level = listnest[-1]
if pos == len(blocks) - 1:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'</%s>\n' % start)
Matt Mackall
minirst: add basic HTML formatting support
r15261 listnest.pop()
else:
nb = blocks[pos + 1]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 ni = nb[b'indent']
Augie Fackler
formatting: blacken the codebase...
r43346 if ni < level or (
ni == level
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 and nb[b'type'] not in b'definition bullet field option'
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(b'</%s>\n' % start)
Matt Mackall
minirst: add basic HTML formatting support
r15261 listnest.pop()
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b''.join(out)
Matt Mackall
minirst: add basic HTML formatting support
r15261
Augie Fackler
formatting: blacken the codebase...
r43346
Gregory Szorc
minirst: support passing admonitions into findadmonitions() and parse()...
r31132 def parse(text, indent=0, keep=None, admonitions=None):
Matt Mackall
minirst: add parse method to get document structure
r15012 """Parse text into a list of blocks"""
Martin Geisler
minimal reStructuredText parser
r9156 blocks = findblocks(text)
Martin Geisler
help: un-indent help topics...
r9540 for b in blocks:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b[b'indent'] += indent
Martin Geisler
minimal reStructuredText parser
r9156 blocks = findliteralblocks(blocks)
Matt Mackall
minirst: add simple table support...
r15037 blocks = findtables(blocks)
Martin Geisler
minirst: report pruned container types
r10444 blocks, pruned = prunecontainers(blocks, keep or [])
Martin Geisler
minirst: correctly format sections containing inline markup...
r10983 blocks = findsections(blocks)
Martin Geisler
minirst: convert ``foo`` into "foo" upon display...
r9623 blocks = inlineliterals(blocks)
Martin Geisler
doc, minirst: support hg interpreted text role
r10972 blocks = hgrole(blocks)
Martin Geisler
minirst: combine list parsing in one function...
r9737 blocks = splitparagraphs(blocks)
Martin Geisler
minirst: improve layout of field lists...
r10065 blocks = updatefieldlists(blocks)
Erik Zielke
minirst: improved support for option lists....
r13011 blocks = updateoptionlists(blocks)
Gregory Szorc
minirst: support passing admonitions into findadmonitions() and parse()...
r31132 blocks = findadmonitions(blocks, admonitions=admonitions)
Martin Geisler
minirst: better interaction between comments and margins...
r13003 blocks = addmargins(blocks)
Martin Geisler
minirst: ignore comments
r12819 blocks = prunecomments(blocks)
Matt Mackall
minirst: add parse method to get document structure
r15012 return blocks, pruned
Augie Fackler
formatting: blacken the codebase...
r43346
Matt Mackall
minirst: add formatblocks helper
r15013 def formatblocks(blocks, width):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 text = b''.join(formatblock(b, width) for b in blocks)
Matt Mackall
minirst: add formatblocks helper
r15013 return text
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
minirst: extract function that formats parsed blocks as plain text
r39343 def formatplain(blocks, width):
"""Format parsed blocks as plain text"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return b''.join(formatblock(b, width) for b in blocks)
Yuya Nishihara
minirst: extract function that formats parsed blocks as plain text
r39343
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def format(text, width=80, indent=0, keep=None, style=b'plain', section=None):
Matt Mackall
minirst: add parse method to get document structure
r15012 """Parse and format the text according to width."""
blocks, pruned = parse(text, indent, keep or [])
Yuya Nishihara
minirst: extract function that filters parsed blocks by section name...
r39341 if section:
blocks = filtersections(blocks, section)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if style == b'html':
Yuya Nishihara
minirst: make format() simply return a formatted text...
r39346 return formathtml(blocks)
Yuya Nishihara
minirst: extract function that filters parsed blocks by section name...
r39341 else:
Yuya Nishihara
minirst: make format() simply return a formatted text...
r39346 return formatplain(blocks, width=width)
Yuya Nishihara
minirst: extract function that filters parsed blocks by section name...
r39341
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
minirst: extract function that filters parsed blocks by section name...
r39341 def filtersections(blocks, section):
Yuya Nishihara
minirst: filter blocks by full path to section
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
help: distinguish sections when multiple match (issue4802)
r26113 parents = []
Yuya Nishihara
minirst: mark getsections() as an internal helper
r39376 sections = _getsections(blocks)
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
r39342 blocks = []
i = 0
lastparents = []
synthetic = []
collapse = True
while i < len(sections):
Yuya Nishihara
minirst: filter blocks by full path to section
r39377 path, nest, b = sections[i]
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
r39342 del parents[nest:]
parents.append(i)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if path == section or path.endswith(b'.' + section):
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
r39342 if lastparents != parents:
llen = len(lastparents)
plen = len(parents)
if llen and llen != plen:
collapse = False
s = []
for j in pycompat.xrange(3, plen - 1):
parent = parents[j]
Augie Fackler
formatting: blacken the codebase...
r43346 if j >= llen or lastparents[j] != parent:
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
r39342 s.append(len(blocks))
sec = sections[parent][2]
blocks.append(sec[0])
blocks.append(sec[-1])
if s:
synthetic.append(s)
timeless
help: include section heading if section depth changes...
r27614
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
r39342 lastparents = parents[:]
blocks.extend(b)
Jordi Gutiérrez Hermoso
help: show all nested subsections of a section with `hg help foo.section`...
r22770
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
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
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 path = [blocks[syn][b'lines'][0] for syn in s]
Yuya Nishihara
minirst: unindent "if True" block in filtersections()
r39342 real = s[-1] + 2
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 realline = blocks[real][b'lines']
realline[0] = b'"%s"' % b'.'.join(path + [realline[0]]).replace(
b'"', b''
Augie Fackler
formatting: blacken the codebase...
r43346 )
del blocks[s[0] : real]
Jordi Gutiérrez Hermoso
help: show all nested subsections of a section with `hg help foo.section`...
r22770
Yuya Nishihara
minirst: extract function that filters parsed blocks by section name...
r39341 return blocks
Martin Geisler
minimal reStructuredText parser
r9156
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
minirst: mark getsections() as an internal helper
r39376 def _getsections(blocks):
Yuya Nishihara
minirst: filter blocks by full path to section
r39377 '''return a list of (section path, nesting level, blocks) tuples'''
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 nest = b""
Yuya Nishihara
minirst: filter blocks by full path to section
r39377 names = ()
Matt Mackall
minirst: add getsections helper
r15014 secs = []
Matt Mackall
help: basic support for showing only specified topic sections...
r22587
def getname(b):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b[b'type'] == b'field':
x = b[b'key']
Matt Harbison
help: support 'hg help template.somekeyword'...
r25723 else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 x = b[b'lines'][0]
x = encoding.lower(x).strip(b'"')
if b'(' in x:
x = x.split(b'(')[0]
Matt Mackall
help: basic support for showing only specified topic sections...
r22587 return x
Matt Mackall
minirst: add getsections helper
r15014 for b in blocks:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b[b'type'] == b'section':
i = b[b'underline']
Matt Mackall
minirst: add getsections helper
r15014 if i not in nest:
nest += i
level = nest.index(i) + 1
nest = nest[:level]
Yuya Nishihara
minirst: filter blocks by full path to section
r39377 names = names[:level] + (getname(b),)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 secs.append((b'.'.join(names), level, [b]))
elif b[b'type'] in (b'definition', b'field'):
i = b' '
Matt Mackall
help: basic support for showing only specified topic sections...
r22587 if i not in nest:
nest += i
level = nest.index(i) + 1
nest = nest[:level]
timeless@mozdev.org
minirst: establish leveling for nested definitions
r26237 for i in range(1, len(secs) + 1):
sec = secs[-i]
if sec[1] < level:
break
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 siblings = [a for a in sec[2] if a[b'type'] == b'definition']
timeless@mozdev.org
minirst: establish leveling for nested definitions
r26237 if siblings:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 siblingindent = siblings[-1][b'indent']
indent = b[b'indent']
timeless@mozdev.org
minirst: establish leveling for nested definitions
r26237 if siblingindent < indent:
level += 1
break
elif siblingindent == indent:
level = sec[1]
break
Yuya Nishihara
minirst: filter blocks by full path to section
r39377 names = names[:level] + (getname(b),)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 secs.append((b'.'.join(names), level, [b]))
Matt Mackall
minirst: add getsections helper
r15014 else:
if not secs:
# add an initial empty section
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 secs = [(b'', 0, [])]
if b[b'type'] != b'margin':
timeless@mozdev.org
minirst: don't treat top level item as children of last item (issue4803)...
r26157 pointer = 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 bindent = b[b'indent']
timeless@mozdev.org
minirst: don't treat top level item as children of last item (issue4803)...
r26157 while pointer < len(secs):
section = secs[-pointer][2][0]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
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
formatting: blacken the codebase...
r43346 )
timeless@mozdev.org
minirst: don't treat top level item as children of last item (issue4803)...
r26157 if bindent >= sindent:
break
pointer += 1
if pointer > 1:
timeless@mozdev.org
minirst: handle edge in hunting for parents...
r26170 blevel = secs[-pointer][1]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if section[b'type'] != b[b'type']:
timeless@mozdev.org
minirst: handle edge in hunting for parents...
r26170 blevel += 1
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 secs.append((b'', blevel, []))
Matt Mackall
minirst: add getsections helper
r15014 secs[-1][2].append(b)
return secs
Martin Geisler
minimal reStructuredText parser
r9156
Augie Fackler
formatting: blacken the codebase...
r43346
Matt Mackall
minirst: add a helper function to build an RST table from an array
r15039 def maketable(data, indent=0, header=False):
Olav Reinert
minirst: generate tables as a list of joined lines
r16815 '''Generate an RST table for the given table data as a list of lines'''
Matt Mackall
minirst: add a helper function to build an RST table from an array
r15039
widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)]
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 indent = b' ' * indent
div = indent + b' '.join(b'=' * w for w in widths) + b'\n'
Matt Mackall
minirst: add a helper function to build an RST table from an array
r15039
out = [div]
for row in data:
Matt Mackall
minirst: fix column handling for simple tables
r15144 l = []
for w, v in zip(widths, row):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if b'\n' in v:
Simon Heimberg
minirst: create valid output when table data contains a newline...
r20654 # only remove line breaks and indentation, long lines are
# handled by the next tool
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 v = b' '.join(e.lstrip() for e in v.split(b'\n'))
pad = b' ' * (w - encoding.colwidth(v))
Matt Mackall
minirst: fix column handling for simple tables
r15144 l.append(v + pad)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 out.append(indent + b' '.join(l) + b"\n")
Matt Mackall
minirst: add a helper function to build an RST table from an array
r15039 if header and len(data) > 1:
out.insert(2, div)
out.append(div)
Olav Reinert
minirst: generate tables as a list of joined lines
r16815 return out