Debian Codegraph @

test/test_parsers/test_rst/test_character_level_inline_markup.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_character_level_inline_markup.py 9425 2023-06-30 14:56:47Z milde $
3 # Author: David Goodger <[email protected]>
4 # Copyright: This module has been placed in the public domain.
5
6 """
7 Tests for inline markup in docutils/parsers/rst/states.py
8 with the "character-level-inline-markup" setting.
9
10 Experimental.
11 """
12
13 from pathlib import Path
14 import sys
15 import unittest
16
17 if __name__ == '__main__':
18 # prepend the "docutils root" to the Python library path
19 # so we import the local `docutils` package.
20 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
21
22 from docutils.frontend import get_default_settings
23 from docutils.parsers.rst import Parser
24 from docutils.utils import new_document
25
26
27 class ParserTestCase(unittest.TestCase):
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.warning_stream = ''
32 settings.character_level_inline_markup = True
33 for name, cases in totest.items():
34 for casenum, (case_input, case_expected) in enumerate(cases):
35 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
36 document = new_document('test data', settings.copy())
37 parser.parse(case_input, document)
38 output = document.pformat()
39 self.assertEqual(case_expected, output)
40
41
42 totest = {}
43
44 totest['emphasis'] = [
45 [r"""some punctuation is allowed around inline markup, e.g.
46 /*emphasis*/, -*emphasis*-, and :*emphasis*: (delimiters),
47 (*emphasis*), [*emphasis*], <*emphasis*>, {*emphasis*} (open/close pairs)
48 *emphasis*., *emphasis*,, *emphasis*!, and *emphasis*\ (closing delimiters),
49
50 With simple-inline-markup also
51 )*emphasis*(, ]*emphasis*[, >*emphasis*>, }*emphasis*{ (close/open pairs),
52 x*2* or 2*x* (alphanumeric char before),
53
54 but not
55 (*), [*], '*' or '"*"' ("quoted" start-string),
56 \*args or * (escaped; whitespace behind start-string),
57 or *the\* *stars\* *inside* (escaped; whitespace before end-string).
58
59 However, '*args' triggers a warning.
60
61 Also *this**.
62 """,
63 """\
64 <document source="test data">
65 <paragraph>
66 some punctuation is allowed around inline markup, e.g.
67 /
68 <emphasis>
69 emphasis
70 /, -
71 <emphasis>
72 emphasis
73 -, and :
74 <emphasis>
75 emphasis
76 : (delimiters),
77 (
78 <emphasis>
79 emphasis
80 ), [
81 <emphasis>
82 emphasis
83 ], <
84 <emphasis>
85 emphasis
86 >, {
87 <emphasis>
88 emphasis
89 } (open/close pairs)
90 <emphasis>
91 emphasis
92 ., \n\
93 <emphasis>
94 emphasis
95 ,, \n\
96 <emphasis>
97 emphasis
98 !, and \n\
99 <emphasis>
100 emphasis
101 (closing delimiters),
102 <paragraph>
103 With simple-inline-markup also
104 )
105 <emphasis>
106 emphasis
107 (, ]
108 <emphasis>
109 emphasis
110 [, >
111 <emphasis>
112 emphasis
113 >, }
114 <emphasis>
115 emphasis
116 { (close/open pairs),
117 x
118 <emphasis>
119 2
120 or 2
121 <emphasis>
122 x
123 (alphanumeric char before),
124 <paragraph>
125 but not
126 (*), [*], '*' or '"*"' ("quoted" start-string),
127 *args or * (escaped; whitespace behind start-string),
128 or \n\
129 <emphasis>
130 the* *stars* *inside
131 (escaped; whitespace before end-string).
132 <paragraph>
133 However, '
134 <problematic ids="problematic-1" refid="system-message-1">
135 *
136 args' triggers a warning.
137 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="15" source="test data" type="WARNING">
138 <paragraph>
139 Inline emphasis start-string without end-string.
140 <paragraph>
141 Also \n\
142 <emphasis>
143 this
144 <problematic ids="problematic-2" refid="system-message-2">
145 *
146 .
147 <system_message backrefs="problematic-2" ids="system-message-2" level="2" line="17" source="test data" type="WARNING">
148 <paragraph>
149 Inline emphasis start-string without end-string.
150 """],
151 [r"""
152 Emphasized asterisk: *\**
153
154 Emphasized double asterisk: *\*\** (requires two escape chars).
155 """,
156 """\
157 <document source="test data">
158 <paragraph>
159 Emphasized asterisk: \n\
160 <emphasis>
161 *
162 <paragraph>
163 Emphasized double asterisk: \n\
164 <emphasis>
165 **
166 (requires two escape chars).
167 """],
168 ]
169
170 totest['strong'] = [
171 [r"""
172 (**strong**) but not (**) or '(** '
173
174 However, '**kwargs' and x**2 are recognized as strong markup
175 and \**kwargs or ** as emphasized.
176 """,
177 """\
178 <document source="test data">
179 <paragraph>
180 (
181 <strong>
182 strong
183 ) but not (**) or '(** '
184 <paragraph>
185 However, '
186 <strong>
187 kwargs' and x
188 2 are recognized as strong markup
189 and *
190 <emphasis>
191 kwargs or *
192 as emphasized.
193 """],
194 ["""\
195 Strong asterisk: **\\***
196 and
197 strong double asterisk: **\\*\\***
198 require escaping with simple-inline-markup.
199 """,
200 """\
201 <document source="test data">
202 <paragraph>
203 Strong asterisk: \n\
204 <strong>
205 *
206 \n\
207 and
208 strong double asterisk: \n\
209 <strong>
210 **
211 \n\
212 require escaping with simple-inline-markup.
213 """],
214 ]
215
216 totest['literal'] = [
217 ["""\
218 With simple-inline-markup, this is ```interpreted text``` in backquotes!
219 """,
220 """\
221 <document source="test data">
222 <paragraph>
223 With simple-inline-markup, this is \n\
224 <literal>
225 `interpreted text
226 ` in backquotes!
227 """],
228 ["""\
229 ``literal without closing backquotes
230 """,
231 """\
232 <document source="test data">
233 <paragraph>
234 <problematic ids="problematic-1" refid="system-message-1">
235 ``
236 literal without closing backquotes
237 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="1" source="test data" type="WARNING">
238 <paragraph>
239 Inline literal start-string without end-string.
240 """],
241 [r"""
242 Python ``list``s use square bracket syntax.
243 """,
244 """\
245 <document source="test data">
246 <paragraph>
247 Python \n\
248 <literal>
249 list
250 s use square bracket syntax.
251 """],
252 ]
253
254 totest['references'] = [
255 ["""\
256 ref_, r_, r_e-f_, -ref_, and anonymousref__,
257 beware of _ref_ or __attr__ or object.__attr__
258 """,
259 """\
260 <document source="test data">
261 <paragraph>
262 <reference name="ref" refname="ref">
263 ref
264 , \n\
265 <reference name="r" refname="r">
266 r
267 , \n\
268 <reference name="r_e-f" refname="r_e-f">
269 r_e-f
270 , -
271 <reference name="ref" refname="ref">
272 ref
273 , and \n\
274 <reference anonymous="1" name="anonymousref">
275 anonymousref
276 ,
277 beware of _
278 <reference name="ref" refname="ref">
279 ref
280 or __
281 <reference anonymous="1" name="attr">
282 attr
283 or object.__
284 <reference anonymous="1" name="attr">
285 attr
286 """],
287 ]
288
289 totest['embedded_uris'] = [
290 [r"""
291 Escape chars in URIs:
292
293 `<reference\:1>`_
294
295 `<anonymous\\call>`__
296
297 `<anonymous\_call>`__
298 """,
299 """\
300 <document source="test data">
301 <paragraph>
302 Escape chars in URIs:
303 <paragraph>
304 <reference name="reference:1" refuri="reference:1">
305 reference:1
306 <target ids="reference-1" names="reference:1" refuri="reference:1">
307 <paragraph>
308 <reference name="anonymous\\call" refuri="anonymous\\call">
309 anonymous\\call
310 <paragraph>
311 <reference name="anonymous_call" refuri="anonymous_call">
312 anonymous_call
313 """],
314 ]
315
316 totest['inline_targets'] = [
317 ["""\
318 This isn't a _target; targets require backquotes.
319
320 With simple-inline-markup, _`this`_ is a a target followed by an
321 underscore.
322 """,
323 """\
324 <document source="test data">
325 <paragraph>
326 This isn't a _target; targets require backquotes.
327 <paragraph>
328 With simple-inline-markup, \n\
329 <target ids="this" names="this">
330 this
331 _ is a a target followed by an
332 underscore.
333 """],
334 ]
335
336 totest['footnote_reference'] = [
337 ["""\
338 Adjacent footnote refs are possible with simple-inline-markup:
339 [*]_[#label]_ [#]_[2]_ [1]_[*]_
340
341 .. [#] test1
342 .. [*] test2
343 """,
344 """\
345 <document source="test data">
346 <paragraph>
347 Adjacent footnote refs are possible with simple-inline-markup:
348 <footnote_reference auto="*" ids="footnote-reference-1">
349 <footnote_reference auto="1" ids="footnote-reference-2" refname="label">
350 \n\
351 <footnote_reference auto="1" ids="footnote-reference-3">
352 <footnote_reference ids="footnote-reference-4" refname="2">
353 2
354 \n\
355 <footnote_reference ids="footnote-reference-5" refname="1">
356 1
357 <footnote_reference auto="*" ids="footnote-reference-6">
358 <footnote auto="1" ids="footnote-1">
359 <paragraph>
360 test1
361 <footnote auto="*" ids="footnote-2">
362 <paragraph>
363 test2
364 """],
365 ]
366
367 totest['citation_reference'] = [
368 ["""\
369 Adjacent citation refs are possible with simple-inline-markup:
370 [citation]_[CIT1]_
371 """,
372 """\
373 <document source="test data">
374 <paragraph>
375 Adjacent citation refs are possible with simple-inline-markup:
376 <citation_reference ids="citation-reference-1" refname="citation">
377 citation
378 <citation_reference ids="citation-reference-2" refname="cit1">
379 CIT1
380 """],
381 ]
382
383 totest['substitution_references'] = [
384 ["""\
385 |sub|ref
386 """,
387 """\
388 <document source="test data">
389 <paragraph>
390 <substitution_reference refname="sub">
391 sub
392 ref
393 """],
394 ]
395
396 totest['standalone_hyperlink'] = [
397 [r"""
398 Valid URLs with escaped markup characters:
399
400 http://example.com/\*content\*/whatever
401
402 Invalid with the simple-inline-markup setting:
403
404 http://example.com/\*content*/whatever
405 http://example.com/rST_for_all.html
406 """,
407 """\
408 <document source="test data">
409 <paragraph>
410 Valid URLs with escaped markup characters:
411 <paragraph>
412 <reference refuri="http://example.com/*content*/whatever">
413 http://example.com/*content*/whatever
414 <paragraph>
415 Invalid with the simple-inline-markup setting:
416 <paragraph>
417 <reference refuri="http://example.com/*content">
418 http://example.com/*content
419 <problematic ids="problematic-1" refid="system-message-1">
420 *
421 /whatever
422 <reference refuri="http://example.com/">
423 http://example.com/
424 <reference name="rST_for" refname="rst_for">
425 rST_for
426 all.html
427 <system_message backrefs="problematic-1" ids="system-message-1" level="2" line="8" source="test data" type="WARNING">
428 <paragraph>
429 Inline emphasis start-string without end-string.
430 """],
431 ]
432
433 totest['markup_recognition_rules'] = [
434 ["""\
435 __This__ is an anonymous reference with simple-inline-markup.
436 """,
437 """\
438 <document source="test data">
439 <paragraph>
440 __
441 <reference anonymous="1" name="This">
442 This
443 is an anonymous reference with simple-inline-markup.
444 """],
445 [r"""
446 Character-level m*a***r**``k``\ `u`:title:\p
447 with backslash-escaped whitespace, including new\
448 lines.
449 """,
450 """\
451 <document source="test data">
452 <paragraph>
453 Character-level m
454 <emphasis>
455 a
456 <strong>
457 r
458 <literal>
459 k
460 <title_reference>
461 u
462 p
463 with backslash-escaped whitespace, including newlines.
464 """],
465 ["""\
466 text-*separated*\u2010*by*\u2011*various*\u2012*dashes*\u2013*and*\u2014*hyphens*.
467 \u00bf*punctuation*? \u00a1*examples*!\xa0*no-break-space*\xa0.
468 """,
469 """\
470 <document source="test data">
471 <paragraph>
472 text-
473 <emphasis>
474 separated
475 \u2010
476 <emphasis>
477 by
478 \u2011
479 <emphasis>
480 various
481 \u2012
482 <emphasis>
483 dashes
484 \u2013
485 <emphasis>
486 and
487 \u2014
488 <emphasis>
489 hyphens
490 .
491 \xbf
492 <emphasis>
493 punctuation
494 ? \xa1
495 <emphasis>
496 examples
497 !\xa0
498 <emphasis>
499 no-break-space
500 \xa0.
501 """],
502 # Whitespace characters:
503 ["""\
504 inline markup surrounded by various whitespace characters:
505 *newline*
506 or *space* or one of
507 \xa0*NO-BREAK SPACE*\xa0,
508 \u1680*OGHAM SPACE MARK*\u1680,
509 \u180e*MONGOLIAN VOWEL SEPARATOR*\u180e,
510 \u2000*EN QUAD*\u2000,
511 \u2001*EM QUAD*\u2001,
512 \u2002*EN SPACE*\u2002,
513 \u2003*EM SPACE*\u2003,
514 \u2004*THREE-PER-EM SPACE*\u2004,
515 \u2005*FOUR-PER-EM SPACE*\u2005,
516 \u2006*SIX-PER-EM SPACE*\u2006,
517 \u2007*FIGURE SPACE*\u2007,
518 \u2008*PUNCTUATION SPACE*\u2008,
519 \u2009*THIN SPACE*\u2009,
520 \u200a*HAIR SPACE*\u200a,
521 \u202f*NARROW NO-BREAK SPACE*\u202f,
522 \u205f*MEDIUM MATHEMATICAL SPACE*\u205f,
523 \u3000*IDEOGRAPHIC SPACE*\u3000,
524 \u2028*LINE SEPARATOR*\u2028
525 """,
526 """\
527 <document source="test data">
528 <paragraph>
529 inline markup surrounded by various whitespace characters:
530 <emphasis>
531 newline
532 \n\
533 or \n\
534 <emphasis>
535 space
536 or one of
537 \xa0
538 <emphasis>
539 NO-BREAK SPACE
540 \xa0,
541 \u1680
542 <emphasis>
543 OGHAM SPACE MARK
544 \u1680,
545 \u180e
546 <emphasis>
547 MONGOLIAN VOWEL SEPARATOR
548 \u180e,
549 \u2000
550 <emphasis>
551 EN QUAD
552 \u2000,
553 \u2001
554 <emphasis>
555 EM QUAD
556 \u2001,
557 \u2002
558 <emphasis>
559 EN SPACE
560 \u2002,
561 \u2003
562 <emphasis>
563 EM SPACE
564 \u2003,
565 \u2004
566 <emphasis>
567 THREE-PER-EM SPACE
568 \u2004,
569 \u2005
570 <emphasis>
571 FOUR-PER-EM SPACE
572 \u2005,
573 \u2006
574 <emphasis>
575 SIX-PER-EM SPACE
576 \u2006,
577 \u2007
578 <emphasis>
579 FIGURE SPACE
580 \u2007,
581 \u2008
582 <emphasis>
583 PUNCTUATION SPACE
584 \u2008,
585 \u2009
586 <emphasis>
587 THIN SPACE
588 \u2009,
589 \u200a
590 <emphasis>
591 HAIR SPACE
592 \u200a,
593 \u202f
594 <emphasis>
595 NARROW NO-BREAK SPACE
596 \u202f,
597 \u205f
598 <emphasis>
599 MEDIUM MATHEMATICAL SPACE
600 \u205f,
601 \u3000
602 <emphasis>
603 IDEOGRAPHIC SPACE
604 \u3000,
605 <paragraph>
606 <emphasis>
607 LINE SEPARATOR
608 """],
609 ["""\
610 no inline markup due to whitespace inside and behind: *
611 newline
612 *
613 * space * or one of
614 *\xa0NO-BREAK SPACE\xa0*
615 *\u1680OGHAM SPACE MARK\u1680*
616 *\u2000EN QUAD\u2000*
617 *\u2001EM QUAD\u2001*
618 *\u2002EN SPACE\u2002*
619 *\u2003EM SPACE\u2003*
620 *\u2004THREE-PER-EM SPACE\u2004*
621 *\u2005FOUR-PER-EM SPACE\u2005*
622 *\u2006SIX-PER-EM SPACE\u2006*
623 *\u2007FIGURE SPACE\u2007*
624 *\u2008PUNCTUATION SPACE\u2008*
625 *\u2009THIN SPACE\u2009*
626 *\u200aHAIR SPACE\u200a*
627 *\u202fNARROW NO-BREAK SPACE\u202f*
628 *\u205fMEDIUM MATHEMATICAL SPACE\u205f*
629 *\u3000IDEOGRAPHIC SPACE\u3000*
630 *\u2028LINE SEPARATOR\u2028*
631 """,
632 """\
633 <document source="test data">
634 <paragraph>
635 no inline markup due to whitespace inside and behind: *
636 newline
637 *
638 * space * or one of
639 *\xa0NO-BREAK SPACE\xa0*
640 *\u1680OGHAM SPACE MARK\u1680*
641 *\u2000EN QUAD\u2000*
642 *\u2001EM QUAD\u2001*
643 *\u2002EN SPACE\u2002*
644 *\u2003EM SPACE\u2003*
645 *\u2004THREE-PER-EM SPACE\u2004*
646 *\u2005FOUR-PER-EM SPACE\u2005*
647 *\u2006SIX-PER-EM SPACE\u2006*
648 *\u2007FIGURE SPACE\u2007*
649 *\u2008PUNCTUATION SPACE\u2008*
650 *\u2009THIN SPACE\u2009*
651 *\u200aHAIR SPACE\u200a*
652 *\u202fNARROW NO-BREAK SPACE\u202f*
653 *\u205fMEDIUM MATHEMATICAL SPACE\u205f*
654 *\u3000IDEOGRAPHIC SPACE\u3000*
655 *
656 LINE SEPARATOR
657 *
658 """],
659 # « * » ‹ * › « * » ‹ * › « * » ‹ * › French,
660 ["""\
661 "Quoted" markup start-string (matched openers & closers) -> no markup:
662
663 '*' "*" (*) <*> [*] {*}
664 ⁅*⁆
665
666 Some international quoting styles:
667 ‘*’ “*” English, ...,
668 „*“ ‚*‘ »*« ›*‹ German, Czech, ...,
669 „*” «*» Romanian,
670 “*„ ‘*‚ Greek,
671 「*」 『*』traditional Chinese,
672 ”*” ’*’ »*» ›*› Swedish, Finnish,
673 „*” ‚*’ Polish,
674 „*” »*« ’*’ Hungarian,
675
676 But this is „*’ emphasized »*‹.
677 """,
678 """\
679 <document source="test data">
680 <paragraph>
681 "Quoted" markup start-string (matched openers & closers) -> no markup:
682 <paragraph>
683 '*' "*" (*) <*> [*] {*}
684 ⁅*⁆
685 <paragraph>
686 Some international quoting styles:
687 ‘*’ “*” English, ...,
688 „*“ ‚*‘ »*« ›*‹ German, Czech, ...,
689 „*” «*» Romanian,
690 “*„ ‘*‚ Greek,
691 「*」 『*』traditional Chinese,
692 ”*” ’*’ »*» ›*› Swedish, Finnish,
693 „*” ‚*’ Polish,
694 „*” »*« ’*’ Hungarian,
695 <paragraph>
696 But this is „
697 <emphasis>
698 ’ emphasized »
699 ‹.
700 """],
701 ]
702
703
704 if __name__ == '__main__':
705 unittest.main()