summaryrefslogtreecommitdiff
path: root/docutils/test/test_parsers/test_recommonmark/test_inline_markup.py
blob: ee891b36d4c7c24c500ae4846e54fda457732f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env python3
# :Copyright: © 2020 Günter Milde.
# :License: Released under the terms of the `2-Clause BSD license`_, in short:
#
#    Copying and distribution of this file, with or without modification,
#    are permitted in any medium without royalty provided the copyright
#    notice and this notice are preserved.
#    This file is offered as-is, without any warranty.
#
# .. _2-Clause BSD license: https://opensource.org/licenses/BSD-2-Clause
"""
Tests for inline markup in CommonMark parsers
Cf. the `CommonMark Specification <https://spec.commonmark.org/>`__
"""

from pathlib import Path
import sys
import unittest

if __name__ == '__main__':
    # prepend the "docutils root" to the Python library path
    # so we import the local `docutils` package.
    sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

from docutils.frontend import get_default_settings
from docutils.parsers.recommonmark_wrapper import Parser
from docutils.utils import new_document


class RecommonmarkParserTestCase(unittest.TestCase):
    def test_parser(self):
        parser = Parser()
        settings = get_default_settings(Parser)
        for name, cases in totest.items():
            for casenum, (case_input, case_expected) in enumerate(cases):
                with self.subTest(id=f'totest[{name!r}][{casenum}]'):
                    document = new_document('test data', settings.copy())
                    parser.parse(case_input, document)
                    output = document.pformat()
                    self.assertEqual(output, case_expected)


totest = {}

totest['emphasis'] = [
["""\
*emphasis*
_also emphasis_
""",
"""\
<document source="test data">
    <paragraph>
        <emphasis>
            emphasis
        \n\
        <emphasis>
            also emphasis
"""],
["""\
Partially*emphasised*word.
""",
"""\
<document source="test data">
    <paragraph>
        Partially
        <emphasis>
            emphasised
        word.
"""],
["""\
*emphasized sentence
across lines*
""",
"""\
<document source="test data">
    <paragraph>
        <emphasis>
            emphasized sentence
            across lines
"""],
["""\
*no emphasis without closing asterisk
""",
"""\
<document source="test data">
    <paragraph>
        *no emphasis without closing asterisk
"""],
[r"""
No markup when \*escaped or unbalanced *.

What about *this**?
Unbalanced _markup__ is kept as-is without warning.
""",
"""\
<document source="test data">
    <paragraph>
        No markup when *escaped or unbalanced *.
    <paragraph>
        What about \n\
        <emphasis>
            this
        *?
        Unbalanced \n\
        <emphasis>
            markup
        _ is kept as-is without warning.
"""],
[r"""
Emphasized asterisk: *\**

Emphasized double asterisk: *\*\**
""",
"""\
<document source="test data">
    <paragraph>
        Emphasized asterisk: \n\
        <emphasis>
            *
    <paragraph>
        Emphasized double asterisk: \n\
        <emphasis>
            **
"""],
]

totest['strong'] = [
["""\
**strong**
__also strong__
""",
"""\
<document source="test data">
    <paragraph>
        <strong>
            strong
        \n\
        <strong>
            also strong
"""],
["""\
Strong asterisk must be escaped **\\***

Strong double asterisk: **\\*\\***
""",
"""\
<document source="test data">
    <paragraph>
        Strong asterisk must be escaped \n\
        <strong>
            *
    <paragraph>
        Strong double asterisk: \n\
        <strong>
            **
"""],
["""\
**not strong without closing asterisks
""",
"""\
<document source="test data">
    <paragraph>
        **not strong without closing asterisks
"""],
]

totest['literal'] = [
["""\
Inline `literals` are called `code spans` in CommonMark.
""",
"""\
<document source="test data">
    <paragraph>
        Inline \n\
        <literal classes="code">
            literals
         are called \n\
        <literal classes="code">
            code spans
         in CommonMark.
"""],
[r"""
`\*literal`
""",
"""\
<document source="test data">
    <paragraph>
        <literal classes="code">
            \\*literal
"""],
[r"""
``lite\ral``
""",
"""\
<document source="test data">
    <paragraph>
        <literal classes="code">
            lite\\ral
"""],
[r"""
``literal\``
""",
"""\
<document source="test data">
    <paragraph>
        <literal classes="code">
            literal\\
"""],
["""\
l'``literal`` and l\u2019``literal`` with apostrophe
""",
"""\
<document source="test data">
    <paragraph>
        l'
        <literal classes="code">
            literal
         and l\u2019
        <literal classes="code">
            literal
         with apostrophe
"""],
["""\
quoted '``literal``', quoted "``literal``",
quoted \u2018``literal``\u2019, quoted \u201c``literal``\u201d,
quoted \xab``literal``\xbb
""",
"""\
<document source="test data">
    <paragraph>
        quoted '
        <literal classes="code">
            literal
        ', quoted "
        <literal classes="code">
            literal
        ",
        quoted \u2018
        <literal classes="code">
            literal
        \u2019, quoted \u201c
        <literal classes="code">
            literal
        \u201d,
        quoted \xab
        <literal classes="code">
            literal
        \xbb
"""],
["""\
``'literal'`` with quotes, ``"literal"`` with quotes,
``\u2018literal\u2019`` with quotes, ``\u201cliteral\u201d`` with quotes,
``\xabliteral\xbb`` with quotes
""",
"""\
<document source="test data">
    <paragraph>
        <literal classes="code">
            'literal'
         with quotes, \n\
        <literal classes="code">
            "literal"
         with quotes,
        <literal classes="code">
            \u2018literal\u2019
         with quotes, \n\
        <literal classes="code">
            \u201cliteral\u201d
         with quotes,
        <literal classes="code">
            \xabliteral\xbb
         with quotes
"""],
[r"""
``literal ``no literal

No warning for `standalone TeX quotes' or other *unbalanced markup**.
""",
"""\
<document source="test data">
    <paragraph>
        <literal classes="code">
            literal \n\
        no literal
    <paragraph>
        No warning for `standalone TeX quotes\' or other \n\
        <emphasis>
            unbalanced markup
        *.
"""],
["""\
``not literal without closing backquotes
""",
"""\
<document source="test data">
    <paragraph>
        ``not literal without closing backquotes
"""],
[r"""
Python ``list``s use square bracket syntax.
""",
"""\
<document source="test data">
    <paragraph>
        Python \n\
        <literal classes="code">
            list
        s use square bracket syntax.
"""],
[r"""
Blank after opening `` not allowed.
""",
"""\
<document source="test data">
    <paragraph>
        Blank after opening `` not allowed.
"""],
[r"""
no blank ``after closing``still ends a literal.
""",
"""\
<document source="test data">
    <paragraph>
        no blank \n\
        <literal classes="code">
            after closing
        still ends a literal.
"""],
]

totest['references'] = [
["""\
[ref]

[ref]: /uri
""",
"""\
<document source="test data">
    <paragraph>
        <reference name="ref" refuri="/uri">
            ref
"""],
# Fails with recommonmark 0.6.0:
# ["""\
# Inline image ![foo *bar*]
# in a paragraph.
#
# [foo *bar*]: train.jpg "train & tracks"
# """,
# """\
# <document source="test data">
#     <paragraph>
#         Inline image \n\
#         <image alt="foo " title="train & tracks" uri="train.jpg">
#         \n\
#         in a paragraph.
# """],
["""\
[phrase reference]

[phrase reference]: /uri
""",
"""\
<document source="test data">
    <paragraph>
        <reference name="phrase reference" refuri="/uri">
            phrase reference
"""],
["""\
No whitespace required around a[phrase reference].

[phrase reference]: /uri
""",
"""\
<document source="test data">
    <paragraph>
        No whitespace required around a
        <reference name="phrase reference" refuri="/uri">
            phrase reference
        .
"""],
["""\
[phrase reference
across lines]

[phrase reference across lines]: /uri
""",
"""\
<document source="test data">
    <paragraph>
        <reference name="phrase reference across lines" refuri="/uri">
            phrase reference
            across lines
"""],
]

totest['appended_uris'] = [
["""\
[anonymous reference](http://example.com)
""",
"""\
<document source="test data">
    <paragraph>
        <reference refuri="http://example.com">
            anonymous reference
"""],
["""\
Inline image ![a train](train.jpg) more text.
""",
"""\
<document source="test data">
    <paragraph>
        Inline image \n\
        <image alt="a train" uri="train.jpg">
         more text.
"""],
# recommonmark 0.6.0 drops the "title"
# ["""\
# Inline image ![foo](/url "title") more text.
# """,
# """\
# <document source="test data">
#     <paragraph>
#         Inline image \n\
#         <image alt="foo" title="title" uri="/url">
#          more text.
# """],
["""\
[URI must follow immediately]
(http://example.com)
""",
"""\
<document source="test data">
    <paragraph>
        [URI must follow immediately]
        (http://example.com)
"""],
["""\
Relative URIs' reference text can't be omitted:

[reference](reference)
""",
"""\
<document source="test data">
    <paragraph>
        Relative URIs' reference text can't be omitted:
    <paragraph>
        <reference name="reference" refuri="reference">
            reference
"""],
]

totest['standalone_hyperlink'] = [
["""\
CommonMark calls standalone hyperlinks
like <http://example.com> "autolinks".
""",
"""\
<document source="test data">
    <paragraph>
        CommonMark calls standalone hyperlinks
        like \n\
        <reference refuri="http://example.com">
            http://example.com
         "autolinks".
"""],
]

totest['raw_html'] = [
["""\
foo <a href="uri"> bar
""",
"""\
<document source="test data">
    <paragraph>
        foo \n\
        <raw format="html" xml:space="preserve">
            <a href="uri">
         bar
"""],
["""\
foo <br /> bar
and <!-- this is an inline
comment - with hyphen -->
""",
"""\
<document source="test data">
    <paragraph>
        foo \n\
        <raw format="html" xml:space="preserve">
            <br />
         bar
        and \n\
        <raw format="html" xml:space="preserve">
            <!-- this is an inline
            comment - with hyphen -->
"""],
["""\
Hard line breaks are not supported by Docutils.
"recommonmark 0.6.0" converts both, invisible  \n\
(two or more trailing spaces) nor visible\\
(trailing backslash) to raw HTML.
""",
"""\
<document source="test data">
    <paragraph>
        Hard line breaks are not supported by Docutils.
        "recommonmark 0.6.0" converts both, invisible
        <raw format="html" xml:space="preserve">
            <br />
        (two or more trailing spaces) nor visible
        <raw format="html" xml:space="preserve">
            <br />
        (trailing backslash) to raw HTML.
"""],
]

totest['markup_recognition_rules'] = [
[r"""
Character-level m*a***r**`k`_u_p
works except for underline.
""",
"""\
<document source="test data">
    <paragraph>
        Character-level m
        <emphasis>
            a
        <strong>
            r
        <literal classes="code">
            k
        _u_p
        works except for underline.
"""],
]


if __name__ == '__main__':
    unittest.main()