Mercurial > hg
annotate mercurial/minirst.py @ 12789:24999db620cd
Merge with mpm
author | Steve Borho <steve@borho.org> |
---|---|
date | Wed, 20 Oct 2010 15:36:42 -0500 |
parents | 3c31c0e42b11 |
children | 5082e2f3f8e0 |
rev | line source |
---|---|
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
1 # minirst.py - minimal reStructuredText parser |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
2 # |
10443
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
3 # Copyright 2009, 2010 Matt Mackall <mpm@selenic.com> and others |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
4 # |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 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 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
17 It only supports a small subset of reStructuredText: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
18 |
9741
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
19 - sections |
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
20 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
21 - paragraphs |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
22 |
9741
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
23 - literal blocks |
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
24 |
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
25 - definition lists |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
26 |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
27 - specific admonitions |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
28 |
9741
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
29 - bullet lists (items must start with '-') |
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
30 |
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
31 - enumerated lists (no autonumbering) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
32 |
9293
e48a48b754d3
minirst: parse field lists
Martin Geisler <mg@lazybytes.net>
parents:
9292
diff
changeset
|
33 - field lists (colons cannot be escaped) |
e48a48b754d3
minirst: parse field lists
Martin Geisler <mg@lazybytes.net>
parents:
9292
diff
changeset
|
34 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
35 - option lists (supports only long options without arguments) |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
36 |
9741
245689e7f869
minirst: update module docstring
Martin Geisler <mg@lazybytes.net>
parents:
9739
diff
changeset
|
37 - inline literals (no other inline markup is not recognized) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
38 """ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
39 |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
40 import re, sys |
11464
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
41 import util, encoding |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
42 from i18n import _ |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
43 |
11464
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
44 |
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
45 def replace(text, substs): |
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
46 utext = text.decode(encoding.encoding) |
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
47 for f, t in substs: |
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
48 utext = utext.replace(f, t) |
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
49 return utext.encode(encoding.encoding) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
50 |
12651
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
51 |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
52 _blockre = re.compile(r"\n(?:\s*\n)+") |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
53 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
54 def findblocks(text): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
55 """Find continuous blocks of lines in text. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
56 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
57 Returns a list of dictionaries representing the blocks. Each block |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
58 has an 'indent' field and a 'lines' field. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
59 """ |
12651
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
60 blocks = [] |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
61 for b in _blockre.split(text.strip()): |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
62 lines = b.splitlines() |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
63 indent = min((len(l) - len(l.lstrip())) for l in lines) |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
64 lines = [l[indent:] for l in lines] |
17f28de168a4
minirst: refactor/simplify findblocks
Martin Geisler <mg@lazybytes.net>
parents:
12620
diff
changeset
|
65 blocks.append(dict(indent=indent, lines=lines)) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
66 return blocks |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
67 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
68 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
69 def findliteralblocks(blocks): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
70 """Finds literal blocks and adds a 'type' field to the blocks. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
71 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
72 Literal blocks are given the type 'literal', all other blocks are |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
73 given type the 'paragraph'. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
74 """ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
75 i = 0 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
76 while i < len(blocks): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
77 # Searching for a block that looks like this: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
78 # |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
79 # +------------------------------+ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
80 # | paragraph | |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
81 # | (ends with "::") | |
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 # +---------------------------+ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
84 # | indented literal block | |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
85 # +---------------------------+ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
86 blocks[i]['type'] = 'paragraph' |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
87 if blocks[i]['lines'][-1].endswith('::') and i + 1 < len(blocks): |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
88 indent = blocks[i]['indent'] |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
89 adjustment = blocks[i + 1]['indent'] - indent |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
90 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
91 if blocks[i]['lines'] == ['::']: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
92 # Expanded form: remove block |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
93 del blocks[i] |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
94 i -= 1 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
95 elif blocks[i]['lines'][-1].endswith(' ::'): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
96 # Partially minimized form: remove space and both |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
97 # colons. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
98 blocks[i]['lines'][-1] = blocks[i]['lines'][-1][:-3] |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
99 else: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
100 # Fully minimized form: remove just one colon. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
101 blocks[i]['lines'][-1] = blocks[i]['lines'][-1][:-1] |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
102 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
103 # List items are formatted with a hanging indent. We must |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
104 # correct for this here while we still have the original |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
105 # information on the indentation of the subsequent literal |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
106 # blocks available. |
9738
f52c4f7a4732
minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents:
9737
diff
changeset
|
107 m = _bulletre.match(blocks[i]['lines'][0]) |
f52c4f7a4732
minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents:
9737
diff
changeset
|
108 if m: |
f52c4f7a4732
minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents:
9737
diff
changeset
|
109 indent += m.end() |
f52c4f7a4732
minirst: prepare for general types of bullet lists
Martin Geisler <mg@lazybytes.net>
parents:
9737
diff
changeset
|
110 adjustment -= m.end() |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
111 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
112 # Mark the following indented blocks. |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
113 while i + 1 < len(blocks) and blocks[i + 1]['indent'] > indent: |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
114 blocks[i + 1]['type'] = 'literal' |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
115 blocks[i + 1]['indent'] -= adjustment |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
116 i += 1 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
117 i += 1 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
118 return blocks |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
119 |
10447
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
120 _bulletre = re.compile(r'(-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)|\|) ') |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
121 _optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)? +)(.*)$') |
10065
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
122 _fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):[ ]+(.*)') |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
123 _definitionre = re.compile(r'[^ ]') |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
124 |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
125 def splitparagraphs(blocks): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
126 """Split paragraphs into lists.""" |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
127 # 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
|
128 # 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
|
129 # come last. |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
130 listtypes = [('bullet', _bulletre, True), |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
131 ('option', _optionre, True), |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
132 ('field', _fieldre, True), |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
133 ('definition', _definitionre, False)] |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
134 |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
135 def match(lines, i, itemre, singleline): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
136 """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
|
137 |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
138 A list item can be followed by an idented line or another list |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
139 item (but only if singleline is True). |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
140 """ |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
141 line1 = lines[i] |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
142 line2 = i + 1 < len(lines) and lines[i + 1] or '' |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
143 if not itemre.match(line1): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
144 return False |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
145 if singleline: |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
146 return line2 == '' or line2[0] == ' ' or itemre.match(line2) |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
147 else: |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
148 return line2.startswith(' ') |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
149 |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
150 i = 0 |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
151 while i < len(blocks): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
152 if blocks[i]['type'] == 'paragraph': |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
153 lines = blocks[i]['lines'] |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
154 for type, itemre, singleline in listtypes: |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
155 if match(lines, 0, itemre, singleline): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
156 items = [] |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
157 for j, line in enumerate(lines): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
158 if match(lines, j, itemre, singleline): |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
159 items.append(dict(type=type, lines=[], |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
160 indent=blocks[i]['indent'])) |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
161 items[-1]['lines'].append(line) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
162 blocks[i:i + 1] = items |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
163 break |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
164 i += 1 |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
165 return blocks |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
166 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
167 |
10065
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
168 _fieldwidth = 12 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
169 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
170 def updatefieldlists(blocks): |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
171 """Find key and maximum key width for field lists.""" |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
172 i = 0 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
173 while i < len(blocks): |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
174 if blocks[i]['type'] != 'field': |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
175 i += 1 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
176 continue |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
177 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
178 keywidth = 0 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
179 j = i |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
180 while j < len(blocks) and blocks[j]['type'] == 'field': |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
181 m = _fieldre.match(blocks[j]['lines'][0]) |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
182 key, rest = m.groups() |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
183 blocks[j]['lines'][0] = rest |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
184 blocks[j]['key'] = key |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
185 keywidth = max(keywidth, len(key)) |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
186 j += 1 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
187 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
188 for block in blocks[i:j]: |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
189 block['keywidth'] = keywidth |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
190 i = j + 1 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
191 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
192 return blocks |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
193 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
194 |
10443
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
195 def prunecontainers(blocks, keep): |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
196 """Prune unwanted containers. |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
197 |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
198 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
|
199 run through findliteralblocks first. |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
200 """ |
10444
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
201 pruned = [] |
10443
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
202 i = 0 |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
203 while i + 1 < len(blocks): |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
204 # Searching for a block that looks like this: |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
205 # |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
206 # +-------+---------------------------+ |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
207 # | ".. container ::" type | |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
208 # +---+ | |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
209 # | blocks | |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
210 # +-------------------------------+ |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
211 if (blocks[i]['type'] == 'paragraph' and |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
212 blocks[i]['lines'][0].startswith('.. container::')): |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
213 indent = blocks[i]['indent'] |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
214 adjustment = blocks[i + 1]['indent'] - indent |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
215 containertype = blocks[i]['lines'][0][15:] |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
216 prune = containertype not in keep |
10444
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
217 if prune: |
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
218 pruned.append(containertype) |
10443
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
219 |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
220 # Always delete "..container:: type" block |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
221 del blocks[i] |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
222 j = i |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
223 while j < len(blocks) and blocks[j]['indent'] > indent: |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
224 if prune: |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
225 del blocks[j] |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
226 i -= 1 # adjust outer index |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
227 else: |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
228 blocks[j]['indent'] -= adjustment |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
229 j += 1 |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
230 i += 1 |
10444
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
231 return blocks, pruned |
10443
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
232 |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
233 |
10984
68b7d2d668ce
minirst: support all recommended title adornments
Martin Geisler <mg@lazybytes.net>
parents:
10983
diff
changeset
|
234 _sectionre = re.compile(r"""^([-=`:.'"~^_*+#])\1+$""") |
68b7d2d668ce
minirst: support all recommended title adornments
Martin Geisler <mg@lazybytes.net>
parents:
10983
diff
changeset
|
235 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
236 def findsections(blocks): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
237 """Finds sections. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
238 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
239 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
|
240 run through findliteralblocks first. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
241 """ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
242 for block in blocks: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
243 # Searching for a block that looks like this: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
244 # |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
245 # +------------------------------+ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
246 # | Section title | |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
247 # | ------------- | |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
248 # +------------------------------+ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
249 if (block['type'] == 'paragraph' and |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
250 len(block['lines']) == 2 and |
10984
68b7d2d668ce
minirst: support all recommended title adornments
Martin Geisler <mg@lazybytes.net>
parents:
10983
diff
changeset
|
251 len(block['lines'][0]) == len(block['lines'][1]) and |
68b7d2d668ce
minirst: support all recommended title adornments
Martin Geisler <mg@lazybytes.net>
parents:
10983
diff
changeset
|
252 _sectionre.match(block['lines'][1])): |
10983
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
253 block['underline'] = block['lines'][1][0] |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
254 block['type'] = 'section' |
10983
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
255 del block['lines'][1] |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
256 return blocks |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
257 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
258 |
9623
32727ce029de
minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents:
9540
diff
changeset
|
259 def inlineliterals(blocks): |
11464
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
260 substs = [('``', '"')] |
9623
32727ce029de
minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents:
9540
diff
changeset
|
261 for b in blocks: |
10983
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
262 if b['type'] in ('paragraph', 'section'): |
11464
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
263 b['lines'] = [replace(l, substs) for l in b['lines']] |
9623
32727ce029de
minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents:
9540
diff
changeset
|
264 return blocks |
32727ce029de
minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents:
9540
diff
changeset
|
265 |
32727ce029de
minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents:
9540
diff
changeset
|
266 |
10972
0a2c6948f5f4
doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents:
10937
diff
changeset
|
267 def hgrole(blocks): |
11464
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
268 substs = [(':hg:`', '"hg '), ('`', '"')] |
10972
0a2c6948f5f4
doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents:
10937
diff
changeset
|
269 for b in blocks: |
10983
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
270 if b['type'] in ('paragraph', 'section'): |
11192
babf9a5f5528
minirst: handle line breaks in hg role
Martin Geisler <mg@aragost.com>
parents:
11189
diff
changeset
|
271 # 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
|
272 # 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
|
273 # 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
|
274 # (run the blocks through inlineliterals first). |
11464
521c8e0c93bf
minirst: use unicode string as intermediate form for replacement
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11297
diff
changeset
|
275 b['lines'] = [replace(l, substs) for l in b['lines']] |
10972
0a2c6948f5f4
doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents:
10937
diff
changeset
|
276 return blocks |
0a2c6948f5f4
doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents:
10937
diff
changeset
|
277 |
0a2c6948f5f4
doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents:
10937
diff
changeset
|
278 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
279 def addmargins(blocks): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
280 """Adds empty blocks for vertical spacing. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
281 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
282 This groups bullets, options, and definitions together with no vertical |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
283 space between them, and adds an empty block between all other blocks. |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
284 """ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
285 i = 1 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
286 while i < len(blocks): |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10264
diff
changeset
|
287 if (blocks[i]['type'] == blocks[i - 1]['type'] and |
10936
2853c891ac41
minirst: add margin around definition items
Martin Geisler <mg@lazybytes.net>
parents:
10447
diff
changeset
|
288 blocks[i]['type'] in ('bullet', 'option', 'field')): |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
289 i += 1 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
290 else: |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
291 blocks.insert(i, dict(lines=[''], indent=0, type='margin')) |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
292 i += 2 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
293 return blocks |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
294 |
12620
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
295 _admonitionre = re.compile(r"\.\. (admonition|attention|caution|danger|" |
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
296 r"error|hint|important|note|tip|warning)::", |
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
297 flags=re.IGNORECASE) |
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
298 |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
299 def findadmonitions(blocks): |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
300 """ |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
301 Makes the type of the block an admonition block if |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
302 the first line is an admonition directive |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
303 """ |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
304 i = 0 |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
305 while i < len(blocks): |
12620
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
306 m = _admonitionre.match(blocks[i]['lines'][0]) |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
307 if m: |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
308 blocks[i]['type'] = 'admonition' |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
309 admonitiontitle = blocks[i]['lines'][0][3:m.end() - 2].lower() |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
310 |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
311 firstline = blocks[i]['lines'][0][m.end() + 1:] |
12620
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
312 if firstline: |
9a9312e84e4e
minirst: small code cleanup
Martin Geisler <mg@lazybytes.net>
parents:
12388
diff
changeset
|
313 blocks[i]['lines'].insert(1, ' ' + firstline) |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
314 |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
315 blocks[i]['admonitiontitle'] = admonitiontitle |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
316 del blocks[i]['lines'][0] |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
317 i = i + 1 |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
318 return blocks |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
319 |
12652
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
320 _admonitiontitles = {'attention': _('Attention:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
321 'caution': _('Caution:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
322 'danger': _('!Danger!') , |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
323 'error': _('Error:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
324 'hint': _('Hint:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
325 'important': _('Important:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
326 'note': _('Note:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
327 'tip': _('Tip:'), |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
328 'warning': _('Warning!')} |
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
329 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
330 def formatblock(block, width): |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
331 """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
|
332 if width <= 0: |
4c3fb45123e5
util, minirst: do not crash with COLUMNS=0
Martin Geisler <mg@lazybytes.net>
parents:
9293
diff
changeset
|
333 width = 78 |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
334 indent = ' ' * block['indent'] |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
335 if block['type'] == 'admonition': |
12652
3c31c0e42b11
minirst: pull admonition titles out formatblock function
Martin Geisler <mg@lazybytes.net>
parents:
12651
diff
changeset
|
336 admonition = _admonitiontitles[block['admonitiontitle']] |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
337 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
338 |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
339 defindent = indent + hang * ' ' |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
340 text = ' '.join(map(str.strip, block['lines'])) |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
341 return '%s\n%s' % (indent + admonition, util.wrap(text, width=width, |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
342 initindent=defindent, |
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
343 hangindent=defindent)) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
344 if block['type'] == 'margin': |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
345 return '' |
9735
97d0d910fa5d
minirst: remove unnecessary "elif:" statements
Martin Geisler <mg@lazybytes.net>
parents:
9623
diff
changeset
|
346 if block['type'] == 'literal': |
9291
cd5b6a11b607
minirst: indent literal blocks with two spaces
Martin Geisler <mg@lazybytes.net>
parents:
9156
diff
changeset
|
347 indent += ' ' |
cd5b6a11b607
minirst: indent literal blocks with two spaces
Martin Geisler <mg@lazybytes.net>
parents:
9156
diff
changeset
|
348 return indent + ('\n' + indent).join(block['lines']) |
9735
97d0d910fa5d
minirst: remove unnecessary "elif:" statements
Martin Geisler <mg@lazybytes.net>
parents:
9623
diff
changeset
|
349 if block['type'] == 'section': |
10983
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
350 underline = len(block['lines'][0]) * block['underline'] |
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
351 return "%s%s\n%s%s" % (indent, block['lines'][0],indent, underline) |
9735
97d0d910fa5d
minirst: remove unnecessary "elif:" statements
Martin Geisler <mg@lazybytes.net>
parents:
9623
diff
changeset
|
352 if block['type'] == 'definition': |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
353 term = indent + block['lines'][0] |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
354 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip()) |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
355 defindent = indent + hang * ' ' |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
356 text = ' '.join(map(str.strip, block['lines'][1:])) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
357 return '%s\n%s' % (term, util.wrap(text, width=width, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
358 initindent=defindent, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
359 hangindent=defindent)) |
10937
a9d5943d2a30
minirst: removed unnecessary initindent variable
Martin Geisler <mg@lazybytes.net>
parents:
10936
diff
changeset
|
360 subindent = indent |
9735
97d0d910fa5d
minirst: remove unnecessary "elif:" statements
Martin Geisler <mg@lazybytes.net>
parents:
9623
diff
changeset
|
361 if block['type'] == 'bullet': |
10447
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
362 if block['lines'][0].startswith('| '): |
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
363 # Remove bullet for line blocks and add no extra |
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
364 # indention. |
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
365 block['lines'][0] = block['lines'][0][2:] |
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
366 else: |
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
367 m = _bulletre.match(block['lines'][0]) |
e957cc7cbd14
minirst: support line blocks
Martin Geisler <mg@lazybytes.net>
parents:
10444
diff
changeset
|
368 subindent = indent + m.end() * ' ' |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
369 elif block['type'] == 'field': |
10065
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
370 keywidth = block['keywidth'] |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
371 key = block['key'] |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
372 |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
373 subindent = indent + _fieldwidth * ' ' |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
374 if len(key) + 2 > _fieldwidth: |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
375 # key too large, use full line width |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
376 key = key.ljust(width) |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
377 elif keywidth + 2 < _fieldwidth: |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
378 # all keys are small, add only two spaces |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
379 key = key.ljust(keywidth + 2) |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
380 subindent = indent + (keywidth + 2) * ' ' |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
381 else: |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
382 # mixed sizes, use fieldwidth for this one |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
383 key = key.ljust(_fieldwidth) |
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
384 block['lines'][0] = key + block['lines'][0] |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
385 elif block['type'] == 'option': |
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
386 m = _optionre.match(block['lines'][0]) |
10064
6f30c35766d6
minirst: don't test regexps twice
Martin Geisler <mg@lazybytes.net>
parents:
10063
diff
changeset
|
387 option, arg, rest = m.groups() |
6f30c35766d6
minirst: don't test regexps twice
Martin Geisler <mg@lazybytes.net>
parents:
10063
diff
changeset
|
388 subindent = indent + (len(option) + len(arg)) * ' ' |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
389 |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
390 text = ' '.join(map(str.strip, block['lines'])) |
11297
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
391 return util.wrap(text, width=width, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
392 initindent=indent, |
d320e70442a5
replace Python standard textwrap by MBCS sensitive one for i18n text
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
11192
diff
changeset
|
393 hangindent=subindent) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
394 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
395 |
10444
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
396 def format(text, width, indent=0, keep=None): |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
397 """Parse and format the text according to width.""" |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
398 blocks = findblocks(text) |
9540
cad36e496640
help: un-indent help topics
Martin Geisler <mg@lazybytes.net>
parents:
9417
diff
changeset
|
399 for b in blocks: |
cad36e496640
help: un-indent help topics
Martin Geisler <mg@lazybytes.net>
parents:
9417
diff
changeset
|
400 b['indent'] += indent |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
401 blocks = findliteralblocks(blocks) |
10444
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
402 blocks, pruned = prunecontainers(blocks, keep or []) |
10983
287a5cdf7743
minirst: correctly format sections containing inline markup
Martin Geisler <mg@lazybytes.net>
parents:
10972
diff
changeset
|
403 blocks = findsections(blocks) |
9623
32727ce029de
minirst: convert ``foo`` into "foo" upon display
Martin Geisler <mg@lazybytes.net>
parents:
9540
diff
changeset
|
404 blocks = inlineliterals(blocks) |
10972
0a2c6948f5f4
doc, minirst: support hg interpreted text role
Martin Geisler <mg@aragost.com>
parents:
10937
diff
changeset
|
405 blocks = hgrole(blocks) |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
406 blocks = splitparagraphs(blocks) |
10065
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
407 blocks = updatefieldlists(blocks) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
408 blocks = addmargins(blocks) |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
409 blocks = findadmonitions(blocks) |
10444
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
410 text = '\n'.join(formatblock(b, width) for b in blocks) |
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
411 if keep is None: |
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
412 return text |
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
413 else: |
e99e0e077bc4
minirst: report pruned container types
Martin Geisler <mg@lazybytes.net>
parents:
10443
diff
changeset
|
414 return text, pruned |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
415 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
416 |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
417 if __name__ == "__main__": |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
418 from pprint import pprint |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
419 |
10443
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
420 def debug(func, *args): |
62d484a81dfe
minirst: support containers
Martin Geisler <mg@lazybytes.net>
parents:
10282
diff
changeset
|
421 blocks = func(*args) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
422 print "*** after %s:" % func.__name__ |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
423 pprint(blocks) |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
424 print |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
425 return blocks |
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 text = open(sys.argv[1]).read() |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
428 blocks = debug(findblocks, text) |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
429 blocks = debug(findliteralblocks, blocks) |
11187
db2897926d14
minirst: fix debug code
Martin Geisler <mg@lazybytes.net>
parents:
10447
diff
changeset
|
430 blocks, pruned = debug(prunecontainers, blocks, sys.argv[2:]) |
10063
a46478b80ea3
minirst: run inlineliterals too in debug mode
Martin Geisler <mg@lazybytes.net>
parents:
9741
diff
changeset
|
431 blocks = debug(inlineliterals, blocks) |
9737
5f101af4a921
minirst: combine list parsing in one function
Martin Geisler <mg@lazybytes.net>
parents:
9735
diff
changeset
|
432 blocks = debug(splitparagraphs, blocks) |
10065
a1ae0ed78d1a
minirst: improve layout of field lists
Martin Geisler <mg@lazybytes.net>
parents:
10064
diff
changeset
|
433 blocks = debug(updatefieldlists, blocks) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
434 blocks = debug(findsections, blocks) |
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
435 blocks = debug(addmargins, blocks) |
12388
75f044d4dbf5
minirst: Support for admonitions
Erik Zielke <ez@aragost.com>
parents:
11464
diff
changeset
|
436 blocks = debug(findadmonitions, blocks) |
9156
c9c7e8cdac9c
minimal reStructuredText parser
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
437 print '\n'.join(formatblock(b, 30) for b in blocks) |