| 1 |
|
| 2 |
e. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
""" |
| 12 |
Tests for inline markup in CommonMark parsers |
| 13 |
Cf. the `CommonMark Specification <https://spec.commonmark.org/>`__ |
| 14 |
""" |
| 15 |
|
| 16 |
from pathlib import Path |
| 17 |
import sys |
| 18 |
import unittest |
| 19 |
|
| 20 |
if __name__ == '__main__': |
| 21 |
|
| 22 |
|
| 23 |
sys.path.insert(0, str(Path(__file__).resolve().parents[3])) |
| 24 |
|
| 25 |
from docutils.frontend import get_default_settings |
| 26 |
from docutils.parsers.recommonmark_wrapper import Parser |
| 27 |
from docutils.utils import new_document |
| 28 |
|
| 29 |
|
| 30 |
class RecommonmarkParserTestCase(unittest.TestCase): |
| 31 |
def test_parser(self): |
| 32 |
parser = Parser() |
| 33 |
settings = get_default_settings(Parser) |
| 34 |
for name, cases in totest.items(): |
| 35 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 36 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 37 |
document = new_document('test data', settings.copy()) |
| 38 |
parser.parse(case_input, document) |
| 39 |
output = document.pformat() |
| 40 |
self.assertEqual(case_expected, output) |
| 41 |
|
| 42 |
|
| 43 |
totest = {} |
| 44 |
|
| 45 |
totest['emphasis'] = [ |
| 46 |
["""\ |
| 47 |
*emphasis* |
| 48 |
_also emphasis_ |
| 49 |
""", |
| 50 |
"""\ |
| 51 |
<document source="test data"> |
| 52 |
<paragraph> |
| 53 |
<emphasis> |
| 54 |
emphasis |
| 55 |
\n\ |
| 56 |
<emphasis> |
| 57 |
also emphasis |
| 58 |
"""], |
| 59 |
["""\ |
| 60 |
Partially*emphasised*word. |
| 61 |
""", |
| 62 |
"""\ |
| 63 |
<document source="test data"> |
| 64 |
<paragraph> |
| 65 |
Partially |
| 66 |
<emphasis> |
| 67 |
emphasised |
| 68 |
word. |
| 69 |
"""], |
| 70 |
["""\ |
| 71 |
*emphasized sentence |
| 72 |
across lines* |
| 73 |
""", |
| 74 |
"""\ |
| 75 |
<document source="test data"> |
| 76 |
<paragraph> |
| 77 |
<emphasis> |
| 78 |
emphasized sentence |
| 79 |
across lines |
| 80 |
"""], |
| 81 |
["""\ |
| 82 |
*no emphasis without closing asterisk |
| 83 |
""", |
| 84 |
"""\ |
| 85 |
<document source="test data"> |
| 86 |
<paragraph> |
| 87 |
*no emphasis without closing asterisk |
| 88 |
"""], |
| 89 |
[r""" |
| 90 |
No markup when \*escaped or unbalanced *. |
| 91 |
|
| 92 |
What about *this**? |
| 93 |
Unbalanced _markup__ is kept as-is without warning. |
| 94 |
""", |
| 95 |
"""\ |
| 96 |
<document source="test data"> |
| 97 |
<paragraph> |
| 98 |
No markup when *escaped or unbalanced *. |
| 99 |
<paragraph> |
| 100 |
What about \n\ |
| 101 |
<emphasis> |
| 102 |
this |
| 103 |
*? |
| 104 |
Unbalanced \n\ |
| 105 |
<emphasis> |
| 106 |
markup |
| 107 |
_ is kept as-is without warning. |
| 108 |
"""], |
| 109 |
[r""" |
| 110 |
Emphasized asterisk: *\** |
| 111 |
|
| 112 |
Emphasized double asterisk: *\*\** |
| 113 |
""", |
| 114 |
"""\ |
| 115 |
<document source="test data"> |
| 116 |
<paragraph> |
| 117 |
Emphasized asterisk: \n\ |
| 118 |
<emphasis> |
| 119 |
* |
| 120 |
<paragraph> |
| 121 |
Emphasized double asterisk: \n\ |
| 122 |
<emphasis> |
| 123 |
** |
| 124 |
"""], |
| 125 |
] |
| 126 |
|
| 127 |
totest['strong'] = [ |
| 128 |
["""\ |
| 129 |
**strong** |
| 130 |
__also strong__ |
| 131 |
""", |
| 132 |
"""\ |
| 133 |
<document source="test data"> |
| 134 |
<paragraph> |
| 135 |
<strong> |
| 136 |
strong |
| 137 |
\n\ |
| 138 |
<strong> |
| 139 |
also strong |
| 140 |
"""], |
| 141 |
["""\ |
| 142 |
Strong asterisk must be escaped **\\*** |
| 143 |
|
| 144 |
Strong double asterisk: **\\*\\*** |
| 145 |
""", |
| 146 |
"""\ |
| 147 |
<document source="test data"> |
| 148 |
<paragraph> |
| 149 |
Strong asterisk must be escaped \n\ |
| 150 |
<strong> |
| 151 |
* |
| 152 |
<paragraph> |
| 153 |
Strong double asterisk: \n\ |
| 154 |
<strong> |
| 155 |
** |
| 156 |
"""], |
| 157 |
["""\ |
| 158 |
**not strong without closing asterisks |
| 159 |
""", |
| 160 |
"""\ |
| 161 |
<document source="test data"> |
| 162 |
<paragraph> |
| 163 |
**not strong without closing asterisks |
| 164 |
"""], |
| 165 |
] |
| 166 |
|
| 167 |
totest['literal'] = [ |
| 168 |
["""\ |
| 169 |
Inline `literals` are called `code spans` in CommonMark. |
| 170 |
""", |
| 171 |
"""\ |
| 172 |
<document source="test data"> |
| 173 |
<paragraph> |
| 174 |
Inline \n\ |
| 175 |
<literal classes="code"> |
| 176 |
literals |
| 177 |
are called \n\ |
| 178 |
<literal classes="code"> |
| 179 |
code spans |
| 180 |
in CommonMark. |
| 181 |
"""], |
| 182 |
[r""" |
| 183 |
`\*literal` |
| 184 |
""", |
| 185 |
"""\ |
| 186 |
<document source="test data"> |
| 187 |
<paragraph> |
| 188 |
<literal classes="code"> |
| 189 |
\\*literal |
| 190 |
"""], |
| 191 |
[r""" |
| 192 |
``lite\ral`` |
| 193 |
""", |
| 194 |
"""\ |
| 195 |
<document source="test data"> |
| 196 |
<paragraph> |
| 197 |
<literal classes="code"> |
| 198 |
lite\\ral |
| 199 |
"""], |
| 200 |
[r""" |
| 201 |
``literal\`` |
| 202 |
""", |
| 203 |
"""\ |
| 204 |
<document source="test data"> |
| 205 |
<paragraph> |
| 206 |
<literal classes="code"> |
| 207 |
literal\\ |
| 208 |
"""], |
| 209 |
["""\ |
| 210 |
l'``literal`` and l\u2019``literal`` with apostrophe |
| 211 |
""", |
| 212 |
"""\ |
| 213 |
<document source="test data"> |
| 214 |
<paragraph> |
| 215 |
l' |
| 216 |
<literal classes="code"> |
| 217 |
literal |
| 218 |
and l\u2019 |
| 219 |
<literal classes="code"> |
| 220 |
literal |
| 221 |
with apostrophe |
| 222 |
"""], |
| 223 |
["""\ |
| 224 |
quoted '``literal``', quoted "``literal``", |
| 225 |
quoted \u2018``literal``\u2019, quoted \u201c``literal``\u201d, |
| 226 |
quoted \xab``literal``\xbb |
| 227 |
""", |
| 228 |
"""\ |
| 229 |
<document source="test data"> |
| 230 |
<paragraph> |
| 231 |
quoted ' |
| 232 |
<literal classes="code"> |
| 233 |
literal |
| 234 |
', quoted " |
| 235 |
<literal classes="code"> |
| 236 |
literal |
| 237 |
", |
| 238 |
quoted \u2018 |
| 239 |
<literal classes="code"> |
| 240 |
literal |
| 241 |
\u2019, quoted \u201c |
| 242 |
<literal classes="code"> |
| 243 |
literal |
| 244 |
\u201d, |
| 245 |
quoted \xab |
| 246 |
<literal classes="code"> |
| 247 |
literal |
| 248 |
\xbb |
| 249 |
"""], |
| 250 |
["""\ |
| 251 |
``'literal'`` with quotes, ``"literal"`` with quotes, |
| 252 |
``\u2018literal\u2019`` with quotes, ``\u201cliteral\u201d`` with quotes, |
| 253 |
``\xabliteral\xbb`` with quotes |
| 254 |
""", |
| 255 |
"""\ |
| 256 |
<document source="test data"> |
| 257 |
<paragraph> |
| 258 |
<literal classes="code"> |
| 259 |
'literal' |
| 260 |
with quotes, \n\ |
| 261 |
<literal classes="code"> |
| 262 |
"literal" |
| 263 |
with quotes, |
| 264 |
<literal classes="code"> |
| 265 |
\u2018literal\u2019 |
| 266 |
with quotes, \n\ |
| 267 |
<literal classes="code"> |
| 268 |
\u201cliteral\u201d |
| 269 |
with quotes, |
| 270 |
<literal classes="code"> |
| 271 |
\xabliteral\xbb |
| 272 |
with quotes |
| 273 |
"""], |
| 274 |
[r""" |
| 275 |
``literal ``no literal |
| 276 |
|
| 277 |
No warning for `standalone TeX quotes' or other *unbalanced markup**. |
| 278 |
""", |
| 279 |
"""\ |
| 280 |
<document source="test data"> |
| 281 |
<paragraph> |
| 282 |
<literal classes="code"> |
| 283 |
literal \n\ |
| 284 |
no literal |
| 285 |
<paragraph> |
| 286 |
No warning for `standalone TeX quotes\' or other \n\ |
| 287 |
<emphasis> |
| 288 |
unbalanced markup |
| 289 |
*. |
| 290 |
"""], |
| 291 |
["""\ |
| 292 |
``not literal without closing backquotes |
| 293 |
""", |
| 294 |
"""\ |
| 295 |
<document source="test data"> |
| 296 |
<paragraph> |
| 297 |
``not literal without closing backquotes |
| 298 |
"""], |
| 299 |
[r""" |
| 300 |
Python ``list``s use square bracket syntax. |
| 301 |
""", |
| 302 |
"""\ |
| 303 |
<document source="test data"> |
| 304 |
<paragraph> |
| 305 |
Python \n\ |
| 306 |
<literal classes="code"> |
| 307 |
list |
| 308 |
s use square bracket syntax. |
| 309 |
"""], |
| 310 |
[r""" |
| 311 |
Blank after opening `` not allowed. |
| 312 |
""", |
| 313 |
"""\ |
| 314 |
<document source="test data"> |
| 315 |
<paragraph> |
| 316 |
Blank after opening `` 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 |
"""], |
| 329 |
] |
| 330 |
|
| 331 |
totest['references'] = [ |
| 332 |
["""\ |
| 333 |
[ref] |
| 334 |
|
| 335 |
[ref]: /uri |
| 336 |
""", |
| 337 |
"""\ |
| 338 |
<document source="test data"> |
| 339 |
<paragraph> |
| 340 |
<reference name="ref" refuri="/uri"> |
| 341 |
ref |
| 342 |
"""], |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
["""\ |
| 359 |
[phrase reference] |
| 360 |
|
| 361 |
[phrase reference]: /uri |
| 362 |
""", |
| 363 |
"""\ |
| 364 |
<document source="test data"> |
| 365 |
<paragraph> |
| 366 |
<reference name="phrase reference" refuri="/uri"> |
| 367 |
phrase reference |
| 368 |
"""], |
| 369 |
["""\ |
| 370 |
No whitespace required around a[phrase reference]. |
| 371 |
|
| 372 |
[phrase reference]: /uri |
| 373 |
""", |
| 374 |
"""\ |
| 375 |
<document source="test data"> |
| 376 |
<paragraph> |
| 377 |
No whitespace required around a |
| 378 |
<reference name="phrase reference" refuri="/uri"> |
| 379 |
phrase reference |
| 380 |
. |
| 381 |
"""], |
| 382 |
["""\ |
| 383 |
[phrase reference |
| 384 |
across lines] |
| 385 |
|
| 386 |
[phrase reference across lines]: /uri |
| 387 |
""", |
| 388 |
"""\ |
| 389 |
<document source="test data"> |
| 390 |
<paragraph> |
| 391 |
<reference name="phrase reference across lines" refuri="/uri"> |
| 392 |
phrase reference |
| 393 |
across lines |
| 394 |
"""], |
| 395 |
] |
| 396 |
|
| 397 |
totest['appended_uris'] = [ |
| 398 |
["""\ |
| 399 |
[anonymous reference](http://example.com) |
| 400 |
""", |
| 401 |
"""\ |
| 402 |
<document source="test data"> |
| 403 |
<paragraph> |
| 404 |
<reference refuri="http://example.com"> |
| 405 |
anonymous reference |
| 406 |
"""], |
| 407 |
["""\ |
| 408 |
Inline image  more text. |
| 409 |
""", |
| 410 |
"""\ |
| 411 |
<document source="test data"> |
| 412 |
<paragraph> |
| 413 |
Inline image \n\ |
| 414 |
<image alt="a train" uri="train.jpg"> |
| 415 |
more text. |
| 416 |
"""], |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
["""\ |
| 429 |
[URI must follow immediately] |
| 430 |
(http://example.com) |
| 431 |
""", |
| 432 |
"""\ |
| 433 |
<document source="test data"> |
| 434 |
<paragraph> |
| 435 |
[URI must follow immediately] |
| 436 |
(http://example.com) |
| 437 |
"""], |
| 438 |
["""\ |
| 439 |
Relative URIs' reference text can't be omitted: |
| 440 |
|
| 441 |
[reference](reference) |
| 442 |
""", |
| 443 |
"""\ |
| 444 |
<document source="test data"> |
| 445 |
<paragraph> |
| 446 |
Relative URIs' reference text can't be omitted: |
| 447 |
<paragraph> |
| 448 |
<reference name="reference" refuri="reference"> |
| 449 |
reference |
| 450 |
"""], |
| 451 |
] |
| 452 |
|
| 453 |
totest['standalone_hyperlink'] = [ |
| 454 |
["""\ |
| 455 |
CommonMark calls standalone hyperlinks |
| 456 |
like <http://example.com> "autolinks". |
| 457 |
""", |
| 458 |
"""\ |
| 459 |
<document source="test data"> |
| 460 |
<paragraph> |
| 461 |
CommonMark calls standalone hyperlinks |
| 462 |
like \n\ |
| 463 |
<reference refuri="http://example.com"> |
| 464 |
http://example.com |
| 465 |
"autolinks". |
| 466 |
"""], |
| 467 |
] |
| 468 |
|
| 469 |
totest['raw_html'] = [ |
| 470 |
["""\ |
| 471 |
foo <a href="uri"> bar |
| 472 |
""", |
| 473 |
"""\ |
| 474 |
<document source="test data"> |
| 475 |
<paragraph> |
| 476 |
foo \n\ |
| 477 |
<raw format="html" xml:space="preserve"> |
| 478 |
<a href="uri"> |
| 479 |
bar |
| 480 |
"""], |
| 481 |
["""\ |
| 482 |
foo <br /> bar |
| 483 |
and <!-- this is an inline |
| 484 |
comment - with hyphen --> |
| 485 |
""", |
| 486 |
"""\ |
| 487 |
<document source="test data"> |
| 488 |
<paragraph> |
| 489 |
foo \n\ |
| 490 |
<raw format="html" xml:space="preserve"> |
| 491 |
<br /> |
| 492 |
bar |
| 493 |
and \n\ |
| 494 |
<raw format="html" xml:space="preserve"> |
| 495 |
<!-- this is an inline |
| 496 |
comment - with hyphen --> |
| 497 |
"""], |
| 498 |
["""\ |
| 499 |
Hard line breaks are not supported by Docutils. |
| 500 |
"recommonmark 0.6.0" converts both, invisible \n\ |
| 501 |
(two or more trailing spaces) nor visible\\ |
| 502 |
(trailing backslash) to raw HTML. |
| 503 |
""", |
| 504 |
"""\ |
| 505 |
<document source="test data"> |
| 506 |
<paragraph> |
| 507 |
Hard line breaks are not supported by Docutils. |
| 508 |
"recommonmark 0.6.0" converts both, invisible |
| 509 |
<raw format="html" xml:space="preserve"> |
| 510 |
<br /> |
| 511 |
(two or more trailing spaces) nor visible |
| 512 |
<raw format="html" xml:space="preserve"> |
| 513 |
<br /> |
| 514 |
(trailing backslash) to raw HTML. |
| 515 |
"""], |
| 516 |
] |
| 517 |
|
| 518 |
totest['markup_recognition_rules'] = [ |
| 519 |
[r""" |
| 520 |
Character-level m*a***r**`k`_u_p |
| 521 |
works except for underline. |
| 522 |
""", |
| 523 |
"""\ |
| 524 |
<document source="test data"> |
| 525 |
<paragraph> |
| 526 |
Character-level m |
| 527 |
<emphasis> |
| 528 |
a |
| 529 |
<strong> |
| 530 |
r |
| 531 |
<literal classes="code"> |
| 532 |
k |
| 533 |
_u_p |
| 534 |
works except for underline. |
| 535 |
"""], |
| 536 |
] |
| 537 |
|
| 538 |
|
| 539 |
if __name__ == '__main__': |
| 540 |
unittest.main() |