Debian Codegraph @

tests/test-minirst.py

raw
1 from mercurial import minirst
2 from mercurial.utils import stringutil
3
4
5 def debugformat(text, form, **kwargs):
6 blocks, pruned = minirst.parse(text, **kwargs)
7 if form == b'html':
8 print("html format:")
9 out = minirst.format(text, style=form, **kwargs)
10 else:
11 print("%d column format:" % form)
12 out = minirst.format(text, width=form, **kwargs)
13
14 print("-" * 70)
15 print(out[:-1].decode('utf8'))
16 if kwargs.get('keep'):
17 print("-" * 70)
18 print(stringutil.pprint(pruned).decode('utf8'))
19 print("-" * 70)
20 print()
21
22
23 def debugformats(title, text, **kwargs):
24 print("== %s ==" % title)
25 debugformat(text, 60, **kwargs)
26 debugformat(text, 30, **kwargs)
27 debugformat(text, b'html', **kwargs)
28
29
30 paragraphs = b"""
31 This is some text in the first paragraph.
32
33 A small indented paragraph.
34 It is followed by some lines
35 containing random whitespace.
36 \n \n \nThe third and final paragraph.
37 """
38
39 debugformats('paragraphs', paragraphs)
40
41 definitions = b"""
42 A Term
43 Definition. The indented
44 lines make up the definition.
45 Another Term
46 Another definition. The final line in the
47 definition determines the indentation, so
48 this will be indented with four spaces.
49
50 A Nested/Indented Term
51 Definition.
52 """
53
54 debugformats('definitions', definitions)
55
56 literals = br"""
57 The fully minimized form is the most
58 convenient form::
59
60 Hello
61 literal
62 world
63
64 In the partially minimized form a paragraph
65 simply ends with space-double-colon. ::
66
67 ////////////////////////////////////////
68 long un-wrapped line in a literal block
69 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
70
71 ::
72
73 This literal block is started with '::',
74 the so-called expanded form. The paragraph
75 with '::' disappears in the final output.
76 """
77
78 debugformats('literals', literals)
79
80 lists = b"""
81 - This is the first list item.
82
83 Second paragraph in the first list item.
84
85 - List items need not be separated
86 by a blank line.
87 - And will be rendered without
88 one in any case.
89
90 We can have indented lists:
91
92 - This is an indented list item
93
94 - Another indented list item::
95
96 - A literal block in the middle
97 of an indented list.
98
99 (The above is not a list item since we are in the literal block.)
100
101 ::
102
103 Literal block with no indentation (apart from
104 the two spaces added to all literal blocks).
105
106 1. This is an enumerated list (first item).
107 2. Continuing with the second item.
108
109 (1) foo
110 (2) bar
111
112 1) Another
113 2) List
114
115 Line blocks are also a form of list:
116
117 | This is the first line.
118 The line continues here.
119 | This is the second line.
120
121 Bullet lists are also detected:
122
123 * This is the first bullet
124 * This is the second bullet
125 It has 2 lines
126 * This is the third bullet
127 """
128
129 debugformats('lists', lists)
130
131 options = b"""
132 There is support for simple option lists,
133 but only with long options:
134
135 -X, --exclude filter an option with a short and long option with an argument
136 -I, --include an option with both a short option and a long option
137 --all Output all.
138 --both Output both (this description is
139 quite long).
140 --long Output all day long.
141
142 --par This option has two paragraphs in its description.
143 This is the first.
144
145 This is the second. Blank lines may be omitted between
146 options (as above) or left in (as here).
147
148
149 The next paragraph looks like an option list, but lacks the two-space
150 marker after the option. It is treated as a normal paragraph:
151
152 --foo bar baz
153 """
154
155 debugformats('options', options)
156
157 fields = br"""
158 :a: First item.
159 :ab: Second item. Indentation and wrapping
160 is handled automatically.
161 :c\:d: a key with colon
162 :efg\:\:hh: a key with many colon
163
164 Next list:
165
166 :small: The larger key below triggers full indentation here.
167 :much too large: This key is big enough to get its own line.
168 """
169
170 debugformats('fields', fields)
171
172 containers = b"""
173 Normal output.
174
175 .. container:: debug
176
177 Initial debug output.
178
179 .. container:: verbose
180
181 Verbose output.
182
183 .. container:: debug
184
185 Debug output.
186 """
187
188 debugformats('containers (normal)', containers)
189 debugformats('containers (verbose)', containers, keep=[b'verbose'])
190 debugformats('containers (debug)', containers, keep=[b'debug'])
191 debugformats(
192 'containers (verbose debug)', containers, keep=[b'verbose', b'debug']
193 )
194
195 roles = b"""Please see :hg:`add`."""
196 debugformats('roles', roles)
197
198
199 sections = b"""
200 Title
201 =====
202
203 Section
204 -------
205
206 Subsection
207 ''''''''''
208
209 Markup: ``foo`` and :hg:`help`
210 ------------------------------
211 """
212 debugformats('sections', sections)
213
214
215 admonitions = b"""
216 .. note::
217
218 This is a note
219
220 - Bullet 1
221 - Bullet 2
222
223 .. warning:: This is a warning Second
224 input line of warning
225
226 .. danger::
227 This is danger
228 """
229
230 debugformats('admonitions', admonitions)
231
232 comments = b"""
233 Some text.
234
235 .. A comment
236
237 .. An indented comment
238
239 Some indented text.
240
241 ..
242
243 Empty comment above
244 """
245
246 debugformats('comments', comments)
247
248
249 data = [
250 [b'a', b'b', b'c'],
251 [b'1', b'2', b'3'],
252 [b'foo', b'bar', b'baz this list is very very very long man'],
253 ]
254
255 rst = minirst.maketable(data, 2, True)
256 table = b''.join(rst)
257
258 print(table.decode('utf8'))
259
260 debugformats('table', table)
261
262 data = [
263 [b's', b'long', b'line\ngoes on here'],
264 [b'', b'xy', b'tried to fix here\n by indenting'],
265 ]
266
267 rst = minirst.maketable(data, 1, False)
268 table = b''.join(rst)
269
270 print(table.decode('utf8'))
271
272 debugformats('table+nl', table)