annotate mercurial/minirst.py @ 43845:1ffbd03c8d75

filemerge: drop a default argument to appease pytype The function slices and takes the length of this argument without internally setting it if not provided. There was no bug here because both callers passed the argument. Differential Revision: https://phab.mercurial-scm.org/D7464
author Matt Harbison <matt_harbison@yahoo.com>
date Wed, 20 Nov 2019 13:01:56 -0500
parents 687b865b95ad
children 89a2afe31e82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
1 # minirst.py - minimal reStructuredText parser
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
2 #
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
3 # Copyright 2009, 2010 Matt Mackall <mpm@selenic.com> and others
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
4 #
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
5 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9741
diff changeset
6 # GNU General Public License version 2 or any later version.
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
7
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
8 """simplified reStructuredText parser.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
9
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
10 This parser knows just enough about reStructuredText to parse the
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
11 Mercurial docstrings.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
12
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
13 It cheats in a major way: nested blocks are not really nested. They
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
14 are just indented blocks that look like they are nested. This relies
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
15 on the user to keep the right indentation for the blocks.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
16
26421
4b0fc75f9403 urls: bulk-change primary website URLs
Matt Mackall <mpm@selenic.com>
parents: 26237
diff changeset
17 Remember to update https://mercurial-scm.org/wiki/HelpStyleGuide
12958
8957c39867f6 minirst: link to HelpStyleGuide in docstring
Martin Geisler <mg@aragost.com>
parents: 12867
diff changeset
18 when adding support for new constructs.
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
19 """
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
20
25960
05d97407a8d1 minirst: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25723
diff changeset
21 from __future__ import absolute_import
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
22
25960
05d97407a8d1 minirst: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25723
diff changeset
23 import re
05d97407a8d1 minirst: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25723
diff changeset
24
05d97407a8d1 minirst: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25723
diff changeset
25 from .i18n import _
05d97407a8d1 minirst: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25723
diff changeset
26 from . import (
05d97407a8d1 minirst: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25723
diff changeset
27 encoding,
31318
1c3352d7eaf2 minirst: make encoding.encoding unicodes to pass into encode() and decode()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31317
diff changeset
28 pycompat,
34695
e178fcaa3933 python3: use our bytes-only version of cgi.escape everywhere
Augie Fackler <augie@google.com>
parents: 34131
diff changeset
29 url,
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 34695
diff changeset
30 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
31 from .utils import stringutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
32
18750
c9d923f5d8ae minirst: CGI escape strings prior to embedding it in the HTML
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18749
diff changeset
33
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
34 def section(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
35 return b"%s\n%s\n\n" % (s, b"\"" * encoding.colwidth(s))
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
36
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
37
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
38 def subsection(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
39 return b"%s\n%s\n\n" % (s, b'=' * encoding.colwidth(s))
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
41
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
42 def subsubsection(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
43 return b"%s\n%s\n\n" % (s, b"-" * encoding.colwidth(s))
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
44
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
45
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
46 def subsubsubsection(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
47 return b"%s\n%s\n\n" % (s, b"." * encoding.colwidth(s))
18748
6e676fb6ea44 help: use a full header for topic titles
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 17424
diff changeset
48
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
49
42251
f9cdd732cb58 minirst: support subsubsubsubsections (header level 5) with marker ''''
Sietse Brouwer <sbbrouwer@gmail.com>
parents: 41759
diff changeset
50 def subsubsubsubsection(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
51 return b"%s\n%s\n\n" % (s, b"'" * encoding.colwidth(s))
42251
f9cdd732cb58 minirst: support subsubsubsubsections (header level 5) with marker ''''
Sietse Brouwer <sbbrouwer@gmail.com>
parents: 41759
diff changeset
52
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
53
11464
521c8e0c93bf minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 11297
diff changeset
54 def replace(text, substs):
15393
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
55 '''
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
56 Apply a list of (find, replace) pairs to a text.
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
57
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32526
diff changeset
58 >>> replace(b"foo bar", [(b'f', b'F'), (b'b', b'B')])
15393
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
59 'Foo Bar'
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32526
diff changeset
60 >>> encoding.encoding = b'latin1'
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32526
diff changeset
61 >>> replace(b'\\x81\\\\', [(b'\\\\', b'/')])
15393
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
62 '\\x81/'
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32526
diff changeset
63 >>> encoding.encoding = b'shiftjis'
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 32526
diff changeset
64 >>> replace(b'\\x81\\\\', [(b'\\\\', b'/')])
15393
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
65 '\\x81\\\\'
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
66 '''
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
67
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
68 # some character encodings (cp932 for Japanese, at least) use
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
69 # ASCII characters other than control/alphabet/digit as a part of
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
70 # multi-bytes characters, so direct replacing with such characters
87bb6b7644f6 minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15262
diff changeset
71 # on strings in local encoding causes invalid byte sequences.
31318
1c3352d7eaf2 minirst: make encoding.encoding unicodes to pass into encode() and decode()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31317
diff changeset
72 utext = text.decode(pycompat.sysstr(encoding.encoding))
11464
521c8e0c93bf minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 11297
diff changeset
73 for f, t in substs:
21745
4c62478be2ea minirst: explicitly decode substitutions
Matt Mackall <mpm@selenic.com>
parents: 20682
diff changeset
74 utext = utext.replace(f.decode("ascii"), t.decode("ascii"))
31318
1c3352d7eaf2 minirst: make encoding.encoding unicodes to pass into encode() and decode()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31317
diff changeset
75 return utext.encode(pycompat.sysstr(encoding.encoding))
12651
17f28de168a4 minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents: 12620
diff changeset
76
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
77
31317
0bd32d7c9002 minirst: make regular expressions bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31132
diff changeset
78 _blockre = re.compile(br"\n(?:\s*\n)+")
12651
17f28de168a4 minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents: 12620
diff changeset
79
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
80
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
81 def findblocks(text):
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
82 """Find continuous blocks of lines in text.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
83
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
84 Returns a list of dictionaries representing the blocks. Each block
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
85 has an 'indent' field and a 'lines' field.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
86 """
12651
17f28de168a4 minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents: 12620
diff changeset
87 blocks = []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
88 for b in _blockre.split(text.lstrip(b'\n').rstrip()):
12651
17f28de168a4 minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents: 12620
diff changeset
89 lines = b.splitlines()
15123
9b41ccb2043e minirst: don't choke on empty text
Matt Mackall <mpm@selenic.com>
parents: 15122
diff changeset
90 if lines:
9b41ccb2043e minirst: don't choke on empty text
Matt Mackall <mpm@selenic.com>
parents: 15122
diff changeset
91 indent = min((len(l) - len(l.lstrip())) for l in lines)
9b41ccb2043e minirst: don't choke on empty text
Matt Mackall <mpm@selenic.com>
parents: 15122
diff changeset
92 lines = [l[indent:] for l in lines]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
93 blocks.append({b'indent': indent, b'lines': lines})
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
94 return blocks
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
95
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
96
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
97 def findliteralblocks(blocks):
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
98 """Finds literal blocks and adds a 'type' field to the blocks.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
99
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
100 Literal blocks are given the type 'literal', all other blocks are
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
101 given type the 'paragraph'.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
102 """
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
103 i = 0
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
104 while i < len(blocks):
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
105 # Searching for a block that looks like this:
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
106 #
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
107 # +------------------------------+
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
108 # | paragraph |
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
109 # | (ends with "::") |
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
110 # +------------------------------+
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
111 # +---------------------------+
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
112 # | indented literal block |
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
113 # +---------------------------+
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
114 blocks[i][b'type'] = b'paragraph'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
115 if blocks[i][b'lines'][-1].endswith(b'::') and i + 1 < len(blocks):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
116 indent = blocks[i][b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
117 adjustment = blocks[i + 1][b'indent'] - indent
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
118
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
119 if blocks[i][b'lines'] == [b'::']:
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
120 # Expanded form: remove block
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
121 del blocks[i]
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
122 i -= 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
123 elif blocks[i][b'lines'][-1].endswith(b' ::'):
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
124 # Partially minimized form: remove space and both
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
125 # colons.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
126 blocks[i][b'lines'][-1] = blocks[i][b'lines'][-1][:-3]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
127 elif (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
128 len(blocks[i][b'lines']) == 1
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
129 and blocks[i][b'lines'][0].lstrip(b' ').startswith(b'.. ')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
130 and blocks[i][b'lines'][0].find(b' ', 3) == -1
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
131 ):
20549
2025315cfb0c comments: fix minor spelling issues found with spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 19995
diff changeset
132 # directive on its own line, not a literal block
19992
8ac7b85bd8f9 minirst: do not interpret a directive as a literal block
Simon Heimberg <simohe@besonet.ch>
parents: 18752
diff changeset
133 i += 1
8ac7b85bd8f9 minirst: do not interpret a directive as a literal block
Simon Heimberg <simohe@besonet.ch>
parents: 18752
diff changeset
134 continue
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
135 else:
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
136 # Fully minimized form: remove just one colon.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
137 blocks[i][b'lines'][-1] = blocks[i][b'lines'][-1][:-1]
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
138
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
139 # List items are formatted with a hanging indent. We must
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
140 # correct for this here while we still have the original
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
141 # information on the indentation of the subsequent literal
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
142 # blocks available.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
143 m = _bulletre.match(blocks[i][b'lines'][0])
9738
f52c4f7a4732 minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents: 9737
diff changeset
144 if m:
f52c4f7a4732 minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents: 9737
diff changeset
145 indent += m.end()
f52c4f7a4732 minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents: 9737
diff changeset
146 adjustment -= m.end()
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
147
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
148 # Mark the following indented blocks.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
149 while i + 1 < len(blocks) and blocks[i + 1][b'indent'] > indent:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
150 blocks[i + 1][b'type'] = b'literal'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
151 blocks[i + 1][b'indent'] -= adjustment
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
152 i += 1
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
153 i += 1
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
154 return blocks
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
155
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
156
31317
0bd32d7c9002 minirst: make regular expressions bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31132
diff changeset
157 _bulletre = re.compile(br'(\*|-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)|\|) ')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
158 _optionre = re.compile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
159 br'^(-([a-zA-Z0-9]), )?(--[a-z0-9-]+)' br'((.*) +)(.*)$'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
160 )
31317
0bd32d7c9002 minirst: make regular expressions bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31132
diff changeset
161 _fieldre = re.compile(br':(?![: ])([^:]*)(?<! ):[ ]+(.*)')
0bd32d7c9002 minirst: make regular expressions bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31132
diff changeset
162 _definitionre = re.compile(br'[^ ]')
0bd32d7c9002 minirst: make regular expressions bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31132
diff changeset
163 _tablere = re.compile(br'(=+\s+)*=+')
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
164
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
165
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
166 def splitparagraphs(blocks):
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
167 """Split paragraphs into lists."""
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
168 # Tuples with (list type, item regexp, single line items?). Order
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
169 # matters: definition lists has the least specific regexp and must
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
170 # come last.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
171 listtypes = [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
172 (b'bullet', _bulletre, True),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
173 (b'option', _optionre, True),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
174 (b'field', _fieldre, True),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
175 (b'definition', _definitionre, False),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
176 ]
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
177
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
178 def match(lines, i, itemre, singleline):
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
179 """Does itemre match an item at line i?
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
180
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 16815
diff changeset
181 A list item can be followed by an indented line or another list
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
182 item (but only if singleline is True).
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
183 """
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
184 line1 = lines[i]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
185 line2 = i + 1 < len(lines) and lines[i + 1] or b''
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
186 if not itemre.match(line1):
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
187 return False
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
188 if singleline:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
189 return line2 == b'' or line2[0:1] == b' ' or itemre.match(line2)
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
190 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
191 return line2.startswith(b' ')
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
192
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
193 i = 0
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
194 while i < len(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
195 if blocks[i][b'type'] == b'paragraph':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
196 lines = blocks[i][b'lines']
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
197 for type, itemre, singleline in listtypes:
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
198 if match(lines, 0, itemre, singleline):
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
199 items = []
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
200 for j, line in enumerate(lines):
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
201 if match(lines, j, itemre, singleline):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
202 items.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
203 {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
204 b'type': type,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
205 b'lines': [],
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
206 b'indent': blocks[i][b'indent'],
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
207 }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
208 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
209 items[-1][b'lines'].append(line)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
210 blocks[i : i + 1] = items
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
211 break
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
212 i += 1
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
213 return blocks
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
214
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
215
15861
ee8f5e4ce7b8 minirst: simplify and standardize field list formatting
Olav Reinert <seroton10@gmail.com>
parents: 15393
diff changeset
216 _fieldwidth = 14
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
217
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
218
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
219 def updatefieldlists(blocks):
15861
ee8f5e4ce7b8 minirst: simplify and standardize field list formatting
Olav Reinert <seroton10@gmail.com>
parents: 15393
diff changeset
220 """Find key for field lists."""
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
221 i = 0
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
222 while i < len(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
223 if blocks[i][b'type'] != b'field':
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
224 i += 1
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
225 continue
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
226
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
227 j = i
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
228 while j < len(blocks) and blocks[j][b'type'] == b'field':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
229 m = _fieldre.match(blocks[j][b'lines'][0])
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
230 key, rest = m.groups()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
231 blocks[j][b'lines'][0] = rest
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
232 blocks[j][b'key'] = key
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
233 j += 1
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
234
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
235 i = j + 1
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
236
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
237 return blocks
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
238
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
239
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
240 def updateoptionlists(blocks):
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
241 i = 0
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
242 while i < len(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
243 if blocks[i][b'type'] != b'option':
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
244 i += 1
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
245 continue
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
246
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
247 optstrwidth = 0
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
248 j = i
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
249 while j < len(blocks) and blocks[j][b'type'] == b'option':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
250 m = _optionre.match(blocks[j][b'lines'][0])
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
251
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
252 shortoption = m.group(2)
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
253 group3 = m.group(3)
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
254 longoption = group3[2:].strip()
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
255 desc = m.group(6).strip()
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
256 longoptionarg = m.group(5).strip()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
257 blocks[j][b'lines'][0] = desc
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
258
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
259 noshortop = b''
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
260 if not shortoption:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
261 noshortop = b' '
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
262
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
263 opt = b"%s%s" % (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
264 shortoption and b"-%s " % shortoption or b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
265 b"%s--%s %s" % (noshortop, longoption, longoptionarg),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
266 )
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
267 opt = opt.rstrip()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
268 blocks[j][b'optstr'] = opt
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
269 optstrwidth = max(optstrwidth, encoding.colwidth(opt))
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
270 j += 1
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
271
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
272 for block in blocks[i:j]:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
273 block[b'optstrwidth'] = optstrwidth
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
274 i = j + 1
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
275 return blocks
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
276
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
277
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
278 def prunecontainers(blocks, keep):
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
279 """Prune unwanted containers.
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
280
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
281 The blocks must have a 'type' field, i.e., they should have been
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
282 run through findliteralblocks first.
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
283 """
10444
e99e0e077bc4 minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents: 10443
diff changeset
284 pruned = []
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
285 i = 0
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
286 while i + 1 < len(blocks):
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
287 # Searching for a block that looks like this:
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
288 #
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
289 # +-------+---------------------------+
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
290 # | ".. container ::" type |
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
291 # +---+ |
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
292 # | blocks |
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
293 # +-------------------------------+
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
294 if blocks[i][b'type'] == b'paragraph' and blocks[i][b'lines'][
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
295 0
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
296 ].startswith(b'.. container::'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
297 indent = blocks[i][b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
298 adjustment = blocks[i + 1][b'indent'] - indent
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
299 containertype = blocks[i][b'lines'][0][15:]
22584
19bd8bda6bb2 minirst: allow multiple container types for prune
Matt Mackall <mpm@selenic.com>
parents: 21745
diff changeset
300 prune = True
19bd8bda6bb2 minirst: allow multiple container types for prune
Matt Mackall <mpm@selenic.com>
parents: 21745
diff changeset
301 for c in keep:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
302 if c in containertype.split(b'.'):
22584
19bd8bda6bb2 minirst: allow multiple container types for prune
Matt Mackall <mpm@selenic.com>
parents: 21745
diff changeset
303 prune = False
10444
e99e0e077bc4 minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents: 10443
diff changeset
304 if prune:
e99e0e077bc4 minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents: 10443
diff changeset
305 pruned.append(containertype)
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
306
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
307 # Always delete "..container:: type" block
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
308 del blocks[i]
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
309 j = i
15102
a7e375d087f6 minirst: fix container stripping logic
Matt Mackall <mpm@selenic.com>
parents: 15039
diff changeset
310 i -= 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
311 while j < len(blocks) and blocks[j][b'indent'] > indent:
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
312 if prune:
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
313 del blocks[j]
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
314 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
315 blocks[j][b'indent'] -= adjustment
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
316 j += 1
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
317 i += 1
10444
e99e0e077bc4 minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents: 10443
diff changeset
318 return blocks, pruned
10443
62d484a81dfe minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents: 10282
diff changeset
319
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
320
31317
0bd32d7c9002 minirst: make regular expressions bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31132
diff changeset
321 _sectionre = re.compile(br"""^([-=`:.'"~^_*+#])\1+$""")
10984
68b7d2d668ce minirst: support all recommended title adornments
Martin Geisler <mg@lazybytes.net>
parents: 10983
diff changeset
322
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
323
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
324 def findtables(blocks):
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
325 '''Find simple tables
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
326
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
327 Only simple one-line table elements are supported
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
328 '''
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
329
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
330 for block in blocks:
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
331 # Searching for a block that looks like this:
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
332 #
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
333 # === ==== ===
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
334 # A B C
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
335 # === ==== === <- optional
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
336 # 1 2 3
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
337 # x y z
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
338 # === ==== ===
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
339 if (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
340 block[b'type'] == b'paragraph'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
341 and len(block[b'lines']) > 2
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
342 and _tablere.match(block[b'lines'][0])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
343 and block[b'lines'][0] == block[b'lines'][-1]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
344 ):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
345 block[b'type'] = b'table'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
346 block[b'header'] = False
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
347 div = block[b'lines'][0]
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
348
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
349 # column markers are ASCII so we can calculate column
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
350 # position in bytes
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
351 columns = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
352 x
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
353 for x in pycompat.xrange(len(div))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
354 if div[x : x + 1] == b'=' and (x == 0 or div[x - 1 : x] == b' ')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
355 ]
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
356 rows = []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
357 for l in block[b'lines'][1:-1]:
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
358 if l == div:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
359 block[b'header'] = True
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
360 continue
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
361 row = []
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
362 # we measure columns not in bytes or characters but in
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
363 # colwidth which makes things tricky
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
364 pos = columns[0] # leading whitespace is bytes
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
365 for n, start in enumerate(columns):
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
366 if n + 1 < len(columns):
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
367 width = columns[n + 1] - start
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
368 v = encoding.getcols(l, pos, width) # gather columns
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
369 pos += len(v) # calculate byte position of end
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
370 row.append(v.strip())
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
371 else:
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
372 row.append(l[pos:].strip())
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
373 rows.append(row)
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
374
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
375 block[b'table'] = rows
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
376
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
377 return blocks
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
378
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
379
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
380 def findsections(blocks):
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
381 """Finds sections.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
382
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
383 The blocks must have a 'type' field, i.e., they should have been
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
384 run through findliteralblocks first.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
385 """
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
386 for block in blocks:
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
387 # Searching for a block that looks like this:
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
388 #
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
389 # +------------------------------+
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
390 # | Section title |
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
391 # | ------------- |
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
392 # +------------------------------+
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
393 if (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
394 block[b'type'] == b'paragraph'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
395 and len(block[b'lines']) == 2
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
396 and encoding.colwidth(block[b'lines'][0]) == len(block[b'lines'][1])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
397 and _sectionre.match(block[b'lines'][1])
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
398 ):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
399 block[b'underline'] = block[b'lines'][1][0:1]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
400 block[b'type'] = b'section'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
401 del block[b'lines'][1]
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
402 return blocks
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
403
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
404
9623
32727ce029de minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents: 9540
diff changeset
405 def inlineliterals(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
406 substs = [(b'``', b'"')]
9623
32727ce029de minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents: 9540
diff changeset
407 for b in blocks:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
408 if b[b'type'] in (b'paragraph', b'section'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
409 b[b'lines'] = [replace(l, substs) for l in b[b'lines']]
9623
32727ce029de minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents: 9540
diff changeset
410 return blocks
32727ce029de minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents: 9540
diff changeset
411
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
412
10972
0a2c6948f5f4 doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents: 10937
diff changeset
413 def hgrole(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
414 substs = [(b':hg:`', b"'hg "), (b'`', b"'")]
10972
0a2c6948f5f4 doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents: 10937
diff changeset
415 for b in blocks:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
416 if b[b'type'] in (b'paragraph', b'section'):
11192
babf9a5f5528 minirst: handle line breaks in hg role
Martin Geisler <mg@aragost.com>
parents: 11189
diff changeset
417 # Turn :hg:`command` into "hg command". This also works
babf9a5f5528 minirst: handle line breaks in hg role
Martin Geisler <mg@aragost.com>
parents: 11189
diff changeset
418 # when there is a line break in the command and relies on
babf9a5f5528 minirst: handle line breaks in hg role
Martin Geisler <mg@aragost.com>
parents: 11189
diff changeset
419 # the fact that we have no stray back-quotes in the input
babf9a5f5528 minirst: handle line breaks in hg role
Martin Geisler <mg@aragost.com>
parents: 11189
diff changeset
420 # (run the blocks through inlineliterals first).
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
421 b[b'lines'] = [replace(l, substs) for l in b[b'lines']]
10972
0a2c6948f5f4 doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents: 10937
diff changeset
422 return blocks
0a2c6948f5f4 doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents: 10937
diff changeset
423
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
424
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
425 def addmargins(blocks):
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
426 """Adds empty blocks for vertical spacing.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
427
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
428 This groups bullets, options, and definitions together with no vertical
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
429 space between them, and adds an empty block between all other blocks.
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
430 """
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
431 i = 1
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
432 while i < len(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
433 if blocks[i][b'type'] == blocks[i - 1][b'type'] and blocks[i][
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
434 b'type'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
435 ] in (b'bullet', b'option', b'field',):
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
436 i += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
437 elif not blocks[i - 1][b'lines']:
20549
2025315cfb0c comments: fix minor spelling issues found with spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 19995
diff changeset
438 # no lines in previous block, do not separate
19995
0f6e360b14f2 minirst: do not add a 2nd empty paragraph
Simon Heimberg <simohe@besonet.ch>
parents: 19994
diff changeset
439 i += 1
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
440 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
441 blocks.insert(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
442 i, {b'lines': [b''], b'indent': 0, b'type': b'margin'}
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
443 )
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
444 i += 2
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
445 return blocks
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
446
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
447
12819
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
448 def prunecomments(blocks):
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
449 """Remove comments."""
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
450 i = 0
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
451 while i < len(blocks):
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
452 b = blocks[i]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
453 if b[b'type'] == b'paragraph' and (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
454 b[b'lines'][0].startswith(b'.. ') or b[b'lines'] == [b'..']
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
455 ):
12819
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
456 del blocks[i]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
457 if i < len(blocks) and blocks[i][b'type'] == b'margin':
13003
876a931dd230 minirst: better interaction between comments and margins
Martin Geisler <mg@aragost.com>
parents: 12958
diff changeset
458 del blocks[i]
12819
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
459 else:
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
460 i += 1
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
461 return blocks
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
462
31131
50a49ead4db4 minirst: dynamically compile admonitions regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31130
diff changeset
463
31132
bbdd712e9adb minirst: support passing admonitions into findadmonitions() and parse()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31131
diff changeset
464 def findadmonitions(blocks, admonitions=None):
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
465 """
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
466 Makes the type of the block an admonition block if
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
467 the first line is an admonition directive
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
468 """
31714
bbf7a29dcf9b minirst: remove redundant _admonitions set
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31713
diff changeset
469 admonitions = admonitions or _admonitiontitles.keys()
31132
bbdd712e9adb minirst: support passing admonitions into findadmonitions() and parse()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31131
diff changeset
470
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
471 admonitionre = re.compile(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
472 br'\.\. (%s)::' % b'|'.join(sorted(admonitions)), flags=re.IGNORECASE
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
473 )
31131
50a49ead4db4 minirst: dynamically compile admonitions regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31130
diff changeset
474
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
475 i = 0
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
476 while i < len(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
477 m = admonitionre.match(blocks[i][b'lines'][0])
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
478 if m:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
479 blocks[i][b'type'] = b'admonition'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
480 admonitiontitle = blocks[i][b'lines'][0][3 : m.end() - 2].lower()
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
481
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
482 firstline = blocks[i][b'lines'][0][m.end() + 1 :]
12620
9a9312e84e4e minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents: 12388
diff changeset
483 if firstline:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
484 blocks[i][b'lines'].insert(1, b' ' + firstline)
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
485
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
486 blocks[i][b'admonitiontitle'] = admonitiontitle
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
487 del blocks[i][b'lines'][0]
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
488 i = i + 1
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
489 return blocks
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
490
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
491
31712
b3640334a43a minirst: reindent _admonitiontitles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31340
diff changeset
492 _admonitiontitles = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
493 b'attention': _(b'Attention:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
494 b'caution': _(b'Caution:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
495 b'danger': _(b'!Danger!'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
496 b'error': _(b'Error:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
497 b'hint': _(b'Hint:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
498 b'important': _(b'Important:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
499 b'note': _(b'Note:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
500 b'tip': _(b'Tip:'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
501 b'warning': _(b'Warning!'),
31712
b3640334a43a minirst: reindent _admonitiontitles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31340
diff changeset
502 }
12652
3c31c0e42b11 minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents: 12651
diff changeset
503
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
504
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
505 def formatoption(block, width):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
506 desc = b' '.join(map(bytes.strip, block[b'lines']))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
507 colwidth = encoding.colwidth(block[b'optstr'])
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
508 usablewidth = width - 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
509 hanging = block[b'optstrwidth']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
510 initindent = b'%s%s ' % (block[b'optstr'], b' ' * ((hanging - colwidth)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
511 hangindent = b' ' * (encoding.colwidth(initindent) + 1)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
512 return b' %s\n' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
513 stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
514 desc, usablewidth, initindent=initindent, hangindent=hangindent
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
515 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
516 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
517
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
518
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
519 def formatblock(block, width):
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
520 """Format a block according to width."""
9417
4c3fb45123e5 util, minirst: do not crash with COLUMNS=0
Martin Geisler <mg@lazybytes.net>
parents: 9293
diff changeset
521 if width <= 0:
4c3fb45123e5 util, minirst: do not crash with COLUMNS=0
Martin Geisler <mg@lazybytes.net>
parents: 9293
diff changeset
522 width = 78
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
523 indent = b' ' * block[b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
524 if block[b'type'] == b'admonition':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
525 admonition = _admonitiontitles[block[b'admonitiontitle']]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
526 if not block[b'lines']:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
527 return indent + admonition + b'\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
528 hang = len(block[b'lines'][-1]) - len(block[b'lines'][-1].lstrip())
12388
75f044d4dbf5 minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents: 11464
diff changeset
529
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
530 defindent = indent + hang * b' '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
531 text = b' '.join(map(bytes.strip, block[b'lines']))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
532 return b'%s\n%s\n' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
533 indent + admonition,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
534 stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
535 text, width=width, initindent=defindent, hangindent=defindent
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
536 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
537 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
538 if block[b'type'] == b'margin':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
539 return b'\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
540 if block[b'type'] == b'literal':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
541 indent += b' '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
542 return indent + (b'\n' + indent).join(block[b'lines']) + b'\n'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
543 if block[b'type'] == b'section':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
544 underline = encoding.colwidth(block[b'lines'][0]) * block[b'underline']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
545 return b"%s%s\n%s%s\n" % (indent, block[b'lines'][0], indent, underline)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
546 if block[b'type'] == b'table':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
547 table = block[b'table']
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
548 # compute column widths
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
549 widths = [max([encoding.colwidth(e) for e in c]) for c in zip(*table)]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
550 text = b''
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
551 span = sum(widths) + len(widths) - 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
552 indent = b' ' * block[b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
553 hang = b' ' * (len(indent) + span - widths[-1])
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
554
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
555 for row in table:
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
556 l = []
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
557 for w, v in zip(widths, row):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
558 pad = b' ' * (w - encoding.colwidth(v))
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
559 l.append(v + pad)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
560 l = b' '.join(l)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
561 l = stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
562 l, width=width, initindent=indent, hangindent=hang
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
563 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
564 if not text and block[b'header']:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
565 text = l + b'\n' + indent + b'-' * (min(width, span)) + b'\n'
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
566 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
567 text += l + b"\n"
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
568 return text
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
569 if block[b'type'] == b'definition':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
570 term = indent + block[b'lines'][0]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
571 hang = len(block[b'lines'][-1]) - len(block[b'lines'][-1].lstrip())
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
572 defindent = indent + hang * b' '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
573 text = b' '.join(map(bytes.strip, block[b'lines'][1:]))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
574 return b'%s\n%s\n' % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
575 term,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
576 stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
577 text, width=width, initindent=defindent, hangindent=defindent
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
578 ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
579 )
10937
a9d5943d2a30 minirst: removed unnecessary initindent variable
Martin Geisler <mg@lazybytes.net>
parents: 10936
diff changeset
580 subindent = indent
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
581 if block[b'type'] == b'bullet':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
582 if block[b'lines'][0].startswith(b'| '):
10447
e957cc7cbd14 minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents: 10444
diff changeset
583 # Remove bullet for line blocks and add no extra
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26421
diff changeset
584 # indentation.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
585 block[b'lines'][0] = block[b'lines'][0][2:]
10447
e957cc7cbd14 minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents: 10444
diff changeset
586 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
587 m = _bulletre.match(block[b'lines'][0])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
588 subindent = indent + m.end() * b' '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
589 elif block[b'type'] == b'field':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
590 key = block[b'key']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
591 subindent = indent + _fieldwidth * b' '
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
592 if len(key) + 2 > _fieldwidth:
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
593 # key too large, use full line width
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
594 key = key.ljust(width)
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
595 else:
15861
ee8f5e4ce7b8 minirst: simplify and standardize field list formatting
Olav Reinert <seroton10@gmail.com>
parents: 15393
diff changeset
596 # key fits within field width
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
597 key = key.ljust(_fieldwidth)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
598 block[b'lines'][0] = key + block[b'lines'][0]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
599 elif block[b'type'] == b'option':
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
600 return formatoption(block, width)
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
601
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
602 text = b' '.join(map(bytes.strip, block[b'lines']))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
603 return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
604 stringutil.wrap(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
605 text, width=width, initindent=indent, hangindent=subindent
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
606 )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
607 + b'\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
608 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
609
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
610
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
611 def formathtml(blocks):
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
612 """Format RST blocks as HTML"""
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
613
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
614 out = []
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
615 headernest = b''
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
616 listnest = []
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
617
18750
c9d923f5d8ae minirst: CGI escape strings prior to embedding it in the HTML
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18749
diff changeset
618 def escape(s):
34695
e178fcaa3933 python3: use our bytes-only version of cgi.escape everywhere
Augie Fackler <augie@google.com>
parents: 34131
diff changeset
619 return url.escape(s, True)
18750
c9d923f5d8ae minirst: CGI escape strings prior to embedding it in the HTML
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18749
diff changeset
620
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
621 def openlist(start, level):
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
622 if not listnest or listnest[-1][0] != start:
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
623 listnest.append((start, level))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
624 out.append(b'<%s>\n' % start)
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
625
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
626 blocks = [b for b in blocks if b[b'type'] != b'margin']
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
627
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
628 for pos, b in enumerate(blocks):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
629 btype = b[b'type']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
630 level = b[b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
631 lines = b[b'lines']
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
632
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
633 if btype == b'admonition':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
634 admonition = escape(_admonitiontitles[b[b'admonitiontitle']])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
635 text = escape(b' '.join(map(bytes.strip, lines)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
636 out.append(b'<p>\n<b>%s</b> %s\n</p>\n' % (admonition, text))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
637 elif btype == b'paragraph':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
638 out.append(b'<p>\n%s\n</p>\n' % escape(b'\n'.join(lines)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
639 elif btype == b'margin':
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
640 pass
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
641 elif btype == b'literal':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
642 out.append(b'<pre>\n%s\n</pre>\n' % escape(b'\n'.join(lines)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
643 elif btype == b'section':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
644 i = b[b'underline']
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
645 if i not in headernest:
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
646 headernest += i
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
647 level = headernest.index(i) + 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
648 out.append(b'<h%d>%s</h%d>\n' % (level, escape(lines[0]), level))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
649 elif btype == b'table':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
650 table = b[b'table']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
651 out.append(b'<table>\n')
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
652 for row in table:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
653 out.append(b'<tr>')
18750
c9d923f5d8ae minirst: CGI escape strings prior to embedding it in the HTML
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18749
diff changeset
654 for v in row:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
655 out.append(b'<td>')
18752
fabbaa250977 minirst: optimize HTML table generation a bit
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18751
diff changeset
656 out.append(escape(v))
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
657 out.append(b'</td>')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
658 out.append(b'\n')
18752
fabbaa250977 minirst: optimize HTML table generation a bit
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18751
diff changeset
659 out.pop()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
660 out.append(b'</tr>\n')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
661 out.append(b'</table>\n')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
662 elif btype == b'definition':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
663 openlist(b'dl', level)
18750
c9d923f5d8ae minirst: CGI escape strings prior to embedding it in the HTML
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 18749
diff changeset
664 term = escape(lines[0])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
665 text = escape(b' '.join(map(bytes.strip, lines[1:])))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
666 out.append(b' <dt>%s\n <dd>%s\n' % (term, text))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
667 elif btype == b'bullet':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
668 bullet, head = lines[0].split(b' ', 1)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
669 if bullet in (b'*', b'-'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
670 openlist(b'ul', level)
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
671 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
672 openlist(b'ol', level)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
673 out.append(b' <li> %s\n' % escape(b' '.join([head] + lines[1:])))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
674 elif btype == b'field':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
675 openlist(b'dl', level)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
676 key = escape(b[b'key'])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
677 text = escape(b' '.join(map(bytes.strip, lines)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
678 out.append(b' <dt>%s\n <dd>%s\n' % (key, text))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
679 elif btype == b'option':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
680 openlist(b'dl', level)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
681 opt = escape(b[b'optstr'])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
682 desc = escape(b' '.join(map(bytes.strip, lines)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
683 out.append(b' <dt>%s\n <dd>%s\n' % (opt, desc))
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
684
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
685 # close lists if indent level of next block is lower
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
686 if listnest:
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
687 start, level = listnest[-1]
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
688 if pos == len(blocks) - 1:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
689 out.append(b'</%s>\n' % start)
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
690 listnest.pop()
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
691 else:
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
692 nb = blocks[pos + 1]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
693 ni = nb[b'indent']
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
694 if ni < level or (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
695 ni == level
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
696 and nb[b'type'] not in b'definition bullet field option'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
697 ):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
698 out.append(b'</%s>\n' % start)
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
699 listnest.pop()
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
700
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
701 return b''.join(out)
15261
e2df5b866d22 minirst: add basic HTML formatting support
Matt Mackall <mpm@selenic.com>
parents: 15192
diff changeset
702
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
703
31132
bbdd712e9adb minirst: support passing admonitions into findadmonitions() and parse()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31131
diff changeset
704 def parse(text, indent=0, keep=None, admonitions=None):
15012
ee766af457ed minirst: add parse method to get document structure
Matt Mackall <mpm@selenic.com>
parents: 14433
diff changeset
705 """Parse text into a list of blocks"""
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
706 blocks = findblocks(text)
9540
cad36e496640 help: un-indent help topics
Martin Geisler <mg@lazybytes.net>
parents: 9417
diff changeset
707 for b in blocks:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
708 b[b'indent'] += indent
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
709 blocks = findliteralblocks(blocks)
15037
df47381b41d6 minirst: add simple table support
Matt Mackall <mpm@selenic.com>
parents: 15036
diff changeset
710 blocks = findtables(blocks)
10444
e99e0e077bc4 minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents: 10443
diff changeset
711 blocks, pruned = prunecontainers(blocks, keep or [])
10983
287a5cdf7743 minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents: 10972
diff changeset
712 blocks = findsections(blocks)
9623
32727ce029de minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents: 9540
diff changeset
713 blocks = inlineliterals(blocks)
10972
0a2c6948f5f4 doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents: 10937
diff changeset
714 blocks = hgrole(blocks)
9737
5f101af4a921 minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents: 9735
diff changeset
715 blocks = splitparagraphs(blocks)
10065
a1ae0ed78d1a minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents: 10064
diff changeset
716 blocks = updatefieldlists(blocks)
13011
4936a04b6792 minirst: improved support for option lists.
Erik Zielke <ez@aragost.com>
parents: 13009
diff changeset
717 blocks = updateoptionlists(blocks)
31132
bbdd712e9adb minirst: support passing admonitions into findadmonitions() and parse()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 31131
diff changeset
718 blocks = findadmonitions(blocks, admonitions=admonitions)
13003
876a931dd230 minirst: better interaction between comments and margins
Martin Geisler <mg@aragost.com>
parents: 12958
diff changeset
719 blocks = addmargins(blocks)
12819
5082e2f3f8e0 minirst: ignore comments
Martin Geisler <mg@lazybytes.net>
parents: 12652
diff changeset
720 blocks = prunecomments(blocks)
15012
ee766af457ed minirst: add parse method to get document structure
Matt Mackall <mpm@selenic.com>
parents: 14433
diff changeset
721 return blocks, pruned
ee766af457ed minirst: add parse method to get document structure
Matt Mackall <mpm@selenic.com>
parents: 14433
diff changeset
722
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
723
15013
4a1e3c761ec7 minirst: add formatblocks helper
Matt Mackall <mpm@selenic.com>
parents: 15012
diff changeset
724 def formatblocks(blocks, width):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
725 text = b''.join(formatblock(b, width) for b in blocks)
15013
4a1e3c761ec7 minirst: add formatblocks helper
Matt Mackall <mpm@selenic.com>
parents: 15012
diff changeset
726 return text
4a1e3c761ec7 minirst: add formatblocks helper
Matt Mackall <mpm@selenic.com>
parents: 15012
diff changeset
727
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
728
39307
92e9aa38a578 minirst: extract function that formats parsed blocks as plain text
Yuya Nishihara <yuya@tcha.org>
parents: 39306
diff changeset
729 def formatplain(blocks, width):
92e9aa38a578 minirst: extract function that formats parsed blocks as plain text
Yuya Nishihara <yuya@tcha.org>
parents: 39306
diff changeset
730 """Format parsed blocks as plain text"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
731 return b''.join(formatblock(b, width) for b in blocks)
39307
92e9aa38a578 minirst: extract function that formats parsed blocks as plain text
Yuya Nishihara <yuya@tcha.org>
parents: 39306
diff changeset
732
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
733
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
734 def format(text, width=80, indent=0, keep=None, style=b'plain', section=None):
15012
ee766af457ed minirst: add parse method to get document structure
Matt Mackall <mpm@selenic.com>
parents: 14433
diff changeset
735 """Parse and format the text according to width."""
ee766af457ed minirst: add parse method to get document structure
Matt Mackall <mpm@selenic.com>
parents: 14433
diff changeset
736 blocks, pruned = parse(text, indent, keep or [])
39305
200ad3e85a97 minirst: extract function that filters parsed blocks by section name
Yuya Nishihara <yuya@tcha.org>
parents: 38783
diff changeset
737 if section:
200ad3e85a97 minirst: extract function that filters parsed blocks by section name
Yuya Nishihara <yuya@tcha.org>
parents: 38783
diff changeset
738 blocks = filtersections(blocks, section)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
739 if style == b'html':
39310
a2a5d4ad5276 minirst: make format() simply return a formatted text
Yuya Nishihara <yuya@tcha.org>
parents: 39307
diff changeset
740 return formathtml(blocks)
39305
200ad3e85a97 minirst: extract function that filters parsed blocks by section name
Yuya Nishihara <yuya@tcha.org>
parents: 38783
diff changeset
741 else:
39310
a2a5d4ad5276 minirst: make format() simply return a formatted text
Yuya Nishihara <yuya@tcha.org>
parents: 39307
diff changeset
742 return formatplain(blocks, width=width)
39305
200ad3e85a97 minirst: extract function that filters parsed blocks by section name
Yuya Nishihara <yuya@tcha.org>
parents: 38783
diff changeset
743
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
744
39305
200ad3e85a97 minirst: extract function that filters parsed blocks by section name
Yuya Nishihara <yuya@tcha.org>
parents: 38783
diff changeset
745 def filtersections(blocks, section):
39341
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
746 """Select parsed blocks under the specified section
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
747
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
748 The section name is separated by a dot, and matches the suffix of the
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
749 full section path.
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
750 """
26113
9b70eda7529c help: distinguish sections when multiple match (issue4802)
timeless@mozdev.org
parents: 25960
diff changeset
751 parents = []
39340
b2feccc199c2 minirst: mark getsections() as an internal helper
Yuya Nishihara <yuya@tcha.org>
parents: 39311
diff changeset
752 sections = _getsections(blocks)
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
753 blocks = []
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
754 i = 0
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
755 lastparents = []
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
756 synthetic = []
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
757 collapse = True
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
758 while i < len(sections):
39341
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
759 path, nest, b = sections[i]
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
760 del parents[nest:]
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
761 parents.append(i)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
762 if path == section or path.endswith(b'.' + section):
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
763 if lastparents != parents:
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
764 llen = len(lastparents)
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
765 plen = len(parents)
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
766 if llen and llen != plen:
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
767 collapse = False
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
768 s = []
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
769 for j in pycompat.xrange(3, plen - 1):
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
770 parent = parents[j]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
771 if j >= llen or lastparents[j] != parent:
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
772 s.append(len(blocks))
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
773 sec = sections[parent][2]
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
774 blocks.append(sec[0])
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
775 blocks.append(sec[-1])
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
776 if s:
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
777 synthetic.append(s)
27614
1d7e824ad093 help: include section heading if section depth changes
timeless <timeless@mozdev.org>
parents: 26781
diff changeset
778
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
779 lastparents = parents[:]
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
780 blocks.extend(b)
22770
de9424647fe4 help: show all nested subsections of a section with `hg help foo.section`
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22769
diff changeset
781
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
782 ## Also show all subnested sections
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
783 while i + 1 < len(sections) and sections[i + 1][1] > nest:
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
784 i += 1
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
785 blocks.extend(sections[i][2])
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
786 i += 1
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
787 if collapse:
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
788 synthetic.reverse()
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
789 for s in synthetic:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
790 path = [blocks[syn][b'lines'][0] for syn in s]
39306
9fe97e676250 minirst: unindent "if True" block in filtersections()
Yuya Nishihara <yuya@tcha.org>
parents: 39305
diff changeset
791 real = s[-1] + 2
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
792 realline = blocks[real][b'lines']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
793 realline[0] = b'"%s"' % b'.'.join(path + [realline[0]]).replace(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
794 b'"', b''
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
795 )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
796 del blocks[s[0] : real]
22770
de9424647fe4 help: show all nested subsections of a section with `hg help foo.section`
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22769
diff changeset
797
39305
200ad3e85a97 minirst: extract function that filters parsed blocks by section name
Yuya Nishihara <yuya@tcha.org>
parents: 38783
diff changeset
798 return blocks
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
799
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
800
39340
b2feccc199c2 minirst: mark getsections() as an internal helper
Yuya Nishihara <yuya@tcha.org>
parents: 39311
diff changeset
801 def _getsections(blocks):
39341
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
802 '''return a list of (section path, nesting level, blocks) tuples'''
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
803 nest = b""
39341
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
804 names = ()
15014
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
805 secs = []
22587
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
806
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
807 def getname(b):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
808 if b[b'type'] == b'field':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
809 x = b[b'key']
25723
2a8d8b4097c8 help: support 'hg help template.somekeyword'
Matt Harbison <matt_harbison@yahoo.com>
parents: 22770
diff changeset
810 else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
811 x = b[b'lines'][0]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
812 x = encoding.lower(x).strip(b'"')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
813 if b'(' in x:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
814 x = x.split(b'(')[0]
22587
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
815 return x
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
816
15014
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
817 for b in blocks:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
818 if b[b'type'] == b'section':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
819 i = b[b'underline']
15014
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
820 if i not in nest:
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
821 nest += i
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
822 level = nest.index(i) + 1
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
823 nest = nest[:level]
39341
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
824 names = names[:level] + (getname(b),)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
825 secs.append((b'.'.join(names), level, [b]))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
826 elif b[b'type'] in (b'definition', b'field'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
827 i = b' '
22587
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
828 if i not in nest:
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
829 nest += i
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
830 level = nest.index(i) + 1
c3c3dd31fe1c help: basic support for showing only specified topic sections
Matt Mackall <mpm@selenic.com>
parents: 22584
diff changeset
831 nest = nest[:level]
26237
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
832 for i in range(1, len(secs) + 1):
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
833 sec = secs[-i]
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
834 if sec[1] < level:
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
835 break
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
836 siblings = [a for a in sec[2] if a[b'type'] == b'definition']
26237
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
837 if siblings:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
838 siblingindent = siblings[-1][b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
839 indent = b[b'indent']
26237
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
840 if siblingindent < indent:
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
841 level += 1
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
842 break
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
843 elif siblingindent == indent:
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
844 level = sec[1]
1c6f7cc52da9 minirst: establish leveling for nested definitions
timeless@mozdev.org
parents: 26170
diff changeset
845 break
39341
ca2f4dabf51d minirst: filter blocks by full path to section
Yuya Nishihara <yuya@tcha.org>
parents: 39340
diff changeset
846 names = names[:level] + (getname(b),)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
847 secs.append((b'.'.join(names), level, [b]))
15014
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
848 else:
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
849 if not secs:
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
850 # add an initial empty section
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
851 secs = [(b'', 0, [])]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
852 if b[b'type'] != b'margin':
26157
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
853 pointer = 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
854 bindent = b[b'indent']
26157
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
855 while pointer < len(secs):
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
856 section = secs[-pointer][2][0]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
857 if section[b'type'] != b'margin':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
858 sindent = section[b'indent']
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
859 if len(section[b'lines']) > 1:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
860 sindent += len(section[b'lines'][1]) - len(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
861 section[b'lines'][1].lstrip(b' ')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
862 )
26157
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
863 if bindent >= sindent:
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
864 break
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
865 pointer += 1
65e41f388970 minirst: don't treat top level item as children of last item (issue4803)
timeless@mozdev.org
parents: 26113
diff changeset
866 if pointer > 1:
26170
61124bf8485f minirst: handle edge in hunting for parents
timeless@mozdev.org
parents: 26157
diff changeset
867 blevel = secs[-pointer][1]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
868 if section[b'type'] != b[b'type']:
26170
61124bf8485f minirst: handle edge in hunting for parents
timeless@mozdev.org
parents: 26157
diff changeset
869 blevel += 1
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
870 secs.append((b'', blevel, []))
15014
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
871 secs[-1][2].append(b)
a814e986859f minirst: add getsections helper
Matt Mackall <mpm@selenic.com>
parents: 15013
diff changeset
872 return secs
9156
c9c7e8cdac9c minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff changeset
873
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42251
diff changeset
874
15039
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
875 def maketable(data, indent=0, header=False):
16815
e740746ea557 minirst: generate tables as a list of joined lines
Olav Reinert <seroton10@gmail.com>
parents: 15861
diff changeset
876 '''Generate an RST table for the given table data as a list of lines'''
15039
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
877
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
878 widths = [max(encoding.colwidth(e) for e in c) for c in zip(*data)]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
879 indent = b' ' * indent
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
880 div = indent + b' '.join(b'=' * w for w in widths) + b'\n'
15039
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
881
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
882 out = [div]
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
883 for row in data:
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
884 l = []
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
885 for w, v in zip(widths, row):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
886 if b'\n' in v:
20654
af9d9b778550 minirst: create valid output when table data contains a newline
Simon Heimberg <simohe@besonet.ch>
parents: 20549
diff changeset
887 # only remove line breaks and indentation, long lines are
af9d9b778550 minirst: create valid output when table data contains a newline
Simon Heimberg <simohe@besonet.ch>
parents: 20549
diff changeset
888 # handled by the next tool
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
889 v = b' '.join(e.lstrip() for e in v.split(b'\n'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
890 pad = b' ' * (w - encoding.colwidth(v))
15144
87bb975a1844 minirst: fix column handling for simple tables
Matt Mackall <mpm@selenic.com>
parents: 15125
diff changeset
891 l.append(v + pad)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
892 out.append(indent + b' '.join(l) + b"\n")
15039
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
893 if header and len(data) > 1:
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
894 out.insert(2, div)
c981f4a9ea74 minirst: add a helper function to build an RST table from an array
Matt Mackall <mpm@selenic.com>
parents: 15038
diff changeset
895 out.append(div)
16815
e740746ea557 minirst: generate tables as a list of joined lines
Olav Reinert <seroton10@gmail.com>
parents: 15861
diff changeset
896 return out