Add support for Markdown.
[docutils.git] / docutils / test / test_parsers / test_recommonmark / test_inline_markup.py
blobac6b6de37fad3300411d65449135bdffba06b909
1 #!/usr/bin/env python3
2 # -*- coding: utf8 -*-
3 # :Copyright: © 2020 Günter Milde.
4 # :License: Released under the terms of the `2-Clause BSD license`_, in short:
6 # Copying and distribution of this file, with or without modification,
7 # are permitted in any medium without royalty provided the copyright
8 # notice and this notice are preserved.
9 # This file is offered as-is, without any warranty.
11 # .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
12 """
13 Tests for inline markup in CommonMark parsers
14 Cf. the `CommonMark Specification <https://spec.commonmark.org/>`__
15 """
17 from __future__ import absolute_import
19 if __name__ == '__main__':
20 import __init__
21 from test_parsers import DocutilsTestSupport
24 def suite():
25 s = DocutilsTestSupport.RecommonmarkParserTestSuite()
26 s.generateTests(totest)
27 return s
29 totest = {}
31 totest['emphasis'] = [
32 ["""\
33 *emphasis*
34 _also emphasis_
35 """,
36 """\
37 <document source="test data">
38 <paragraph>
39 <emphasis>
40 emphasis
41 <emphasis>
42 also emphasis
43 """],
44 [u"""\
45 Partially*emphasised*word.
46 """,
47 u"""\
48 <document source="test data">
49 <paragraph>
50 Partially
51 <emphasis>
52 emphasised
53 word.
54 """],
55 ["""\
56 *emphasized sentence
57 across lines*
58 """,
59 """\
60 <document source="test data">
61 <paragraph>
62 <emphasis>
63 emphasized sentence
64 across lines
65 """],
66 ["""\
67 *no emphasis without closing asterisk
68 """,
69 """\
70 <document source="test data">
71 <paragraph>
73 no emphasis without closing asterisk
74 """],
75 [r"""
76 No markup when \*escaped or unbalanced *.
78 What about *this**?
79 Unbalanced _markup__ is kept as-is without warning.
80 """,
81 """\
82 <document source="test data">
83 <paragraph>
84 No markup when \n\
86 escaped or unbalanced \n\
89 <paragraph>
90 What about \n\
91 <emphasis>
92 this
95 Unbalanced \n\
96 <emphasis>
97 markup
99 is kept as-is without warning.
100 """],
101 [r"""
102 Emphasized asterisk: *\**
104 Emphasized double asterisk: *\*\**
105 """,
106 """\
107 <document source="test data">
108 <paragraph>
109 Emphasized asterisk: \n\
110 <emphasis>
112 <paragraph>
113 Emphasized double asterisk: \n\
114 <emphasis>
117 """],
120 totest['strong'] = [
121 ["""\
122 **strong**
123 __also strong__
124 """,
125 """\
126 <document source="test data">
127 <paragraph>
128 <strong>
129 strong
130 <strong>
131 also strong
132 """],
133 ["""\
134 Strong asterisk must be escaped **\\***
136 Strong double asterisk: **\\*\\***
137 """,
138 """\
139 <document source="test data">
140 <paragraph>
141 Strong asterisk must be escaped \n\
142 <strong>
144 <paragraph>
145 Strong double asterisk: \n\
146 <strong>
149 """],
150 ["""\
151 **not strong without closing asterisks
152 """,
153 """\
154 <document source="test data">
155 <paragraph>
157 not strong without closing asterisks
158 """],
161 totest['literal'] = [
162 ["""\
163 Inline `literals` are called `code spans` in CommonMark.
164 """,
165 """\
166 <document source="test data">
167 <paragraph>
168 Inline \n\
169 <literal classes="code">
170 literals
171 are called \n\
172 <literal classes="code">
173 code spans
174 in CommonMark.
175 """],
176 [r"""
177 `\*literal`
178 """,
179 """\
180 <document source="test data">
181 <paragraph>
182 <literal classes="code">
183 \\*literal
184 """],
185 [r"""
186 ``lite\ral``
187 """,
188 """\
189 <document source="test data">
190 <paragraph>
191 <literal classes="code">
192 lite\\ral
193 """],
194 [r"""
195 ``literal\``
196 """,
197 """\
198 <document source="test data">
199 <paragraph>
200 <literal classes="code">
201 literal\\
202 """],
203 [u"""\
204 l'``literal`` and l\u2019``literal`` with apostrophe
205 """,
206 u"""\
207 <document source="test data">
208 <paragraph>
210 <literal classes="code">
211 literal
212 and l\u2019
213 <literal classes="code">
214 literal
215 with apostrophe
216 """],
217 [u"""\
218 quoted '``literal``', quoted "``literal``",
219 quoted \u2018``literal``\u2019, quoted \u201c``literal``\u201d,
220 quoted \xab``literal``\xbb
221 """,
222 u"""\
223 <document source="test data">
224 <paragraph>
225 quoted '
226 <literal classes="code">
227 literal
228 ', quoted "
229 <literal classes="code">
230 literal
232 quoted \u2018
233 <literal classes="code">
234 literal
235 \u2019, quoted \u201c
236 <literal classes="code">
237 literal
238 \u201d,
239 quoted \xab
240 <literal classes="code">
241 literal
242 \xbb
243 """],
244 [u"""\
245 ``'literal'`` with quotes, ``"literal"`` with quotes,
246 ``\u2018literal\u2019`` with quotes, ``\u201cliteral\u201d`` with quotes,
247 ``\xabliteral\xbb`` with quotes
248 """,
249 u"""\
250 <document source="test data">
251 <paragraph>
252 <literal classes="code">
253 'literal'
254 with quotes, \n\
255 <literal classes="code">
256 "literal"
257 with quotes,
258 <literal classes="code">
259 \u2018literal\u2019
260 with quotes, \n\
261 <literal classes="code">
262 \u201cliteral\u201d
263 with quotes,
264 <literal classes="code">
265 \xabliteral\xbb
266 with quotes
267 """],
268 [r"""
269 ``literal ``no literal
271 No warning for `standalone TeX quotes' or other *unbalanced markup**.
272 """,
273 """\
274 <document source="test data">
275 <paragraph>
276 <literal classes="code">
277 literal
278 no literal
279 <paragraph>
280 No warning for \n\
282 standalone TeX quotes\' or other \n\
283 <emphasis>
284 unbalanced markup
287 """],
288 ["""\
289 ``not literal without closing backquotes
290 """,
291 """\
292 <document source="test data">
293 <paragraph>
295 not literal without closing backquotes
296 """],
297 [r"""
298 Python ``list``s use square bracket syntax.
299 """,
300 """\
301 <document source="test data">
302 <paragraph>
303 Python \n\
304 <literal classes="code">
305 list
306 s use square bracket syntax.
307 """],
308 [r"""
309 Blank after opening `` not allowed.
310 """,
311 """\
312 <document source="test data">
313 <paragraph>
314 Blank after opening \n\
316 not allowed.
317 """],
318 [r"""
319 no blank ``after closing``still ends a literal.
320 """,
321 """\
322 <document source="test data">
323 <paragraph>
324 no blank \n\
325 <literal classes="code">
326 after closing
327 still ends a literal.
328 """],
331 totest['references'] = [
332 ["""\
333 [ref]
335 [ref]: /uri
336 """,
337 """\
338 <document source="test data">
339 <paragraph>
340 <reference name="ref" refuri="/uri">
342 """],
343 ["""\
344 Inline image ![foo *bar*][foobar]
345 in a paragraph.
347 [FOOBAR]: train.jpg "train & tracks"
348 """,
349 """\
350 <document source="test data">
351 <paragraph>
352 Inline image \n\
353 <image alt="foo " title="train & tracks" uri="train.jpg">
354 in a paragraph.
355 """],
356 ["""\
357 [phrase reference]
359 [phrase reference]: /uri
360 """,
361 """\
362 <document source="test data">
363 <paragraph>
364 <reference name="phrase reference" refuri="/uri">
365 phrase reference
366 """],
367 [u"""\
368 No whitespace required around a[phrase reference].
370 [phrase reference]: /uri
371 """,
372 u"""\
373 <document source="test data">
374 <paragraph>
375 No whitespace required around a
376 <reference name="phrase reference" refuri="/uri">
377 phrase reference
379 """],
380 ["""\
381 [phrase reference
382 across lines]
384 [phrase reference across lines]: /uri
385 """,
386 """\
387 <document source="test data">
388 <paragraph>
389 <reference name="phrase referenceacross lines" refuri="/uri">
390 phrase reference
391 across lines
392 """],
395 totest['appended_URIs'] = [
396 ["""\
397 [anonymous reference](http://example.com)
398 """,
399 """\
400 <document source="test data">
401 <paragraph>
402 <reference name="anonymous reference" refuri="http://example.com">
403 anonymous reference
404 """],
405 ["""\
406 Inline image ![a train](train.jpg) more text.
407 """,
408 """\
409 <document source="test data">
410 <paragraph>
411 Inline image \n\
412 <image alt="a train" uri="train.jpg">
413 more text.
414 """],
415 ["""\
416 Inline image ![foo](/url "title") more text.
417 """,
418 """\
419 <document source="test data">
420 <paragraph>
421 Inline image \n\
422 <image alt="foo" title="title" uri="/url">
423 more text.
424 """],
425 ["""\
426 [URI must follow immediately]
427 (http://example.com)
428 """,
429 """\
430 <document source="test data">
431 <paragraph>
433 URI must follow immediately
435 (http://example.com)
436 """],
437 ["""\
438 Relative URIs' reference text can't be omitted:
440 [reference](reference)
441 """,
442 """\
443 <document source="test data">
444 <paragraph>
445 Relative URIs' reference text can't be omitted:
446 <paragraph>
447 <reference name="reference" refuri="reference">
448 reference
449 """],
452 totest['standalone hyperlink'] = [
453 ["""\
454 CommonMark calls standalone hyperlinks
455 like <http://example.com> "autolinks".
456 """,
457 """\
458 <document source="test data">
459 <paragraph>
460 CommonMark calls standalone hyperlinks
461 like \n\
462 <reference name="http://example.com" refuri="http://example.com">
463 http://example.com
464 "autolinks".
465 """],
468 totest['raw HTML'] = [
469 ["""\
470 foo <a href="uri"> bar
471 """,
472 """\
473 <document source="test data">
474 <paragraph>
475 foo \n\
476 <raw format="html" xml:space="preserve">
477 <a href="uri">
479 """],
480 ["""\
481 foo <br /> bar
482 and <!-- this is an inline
483 comment - with hyphen -->
484 """,
485 """\
486 <document source="test data">
487 <paragraph>
488 foo \n\
489 <raw format="html" xml:space="preserve">
490 <br />
492 and \n\
493 <raw format="html" xml:space="preserve">
494 <!-- this is an inline
495 comment - with hyphen -->
496 """],
497 ["""\
498 Hard line breaks are not supported by Docutils.
499 Not the soft line break preceded by two or more spaces, \n\
500 nor the more visible alternative,\\
501 a backslash before the line ending.
502 """,
503 """\
504 <document source="test data">
505 <paragraph>
506 Hard line breaks are not supported by Docutils.
507 Not the soft line break preceded by two or more spaces,
508 nor the more visible alternative,
509 a backslash before the line ending.
510 """],
513 totest['markup recognition rules'] = [
514 [r"""
515 Character-level m*a***r**`k`_u_p
516 works except for underline.
517 """,
518 """\
519 <document source="test data">
520 <paragraph>
521 Character-level m
522 <emphasis>
524 <strong>
526 <literal classes="code">
532 works except for underline.
533 """],
537 if __name__ == '__main__':
538 import unittest
539 unittest.main(defaultTest='suite')