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