| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
""" |
| 7 |
Tests for nested parsing with support for sections (cf. states.py). |
| 8 |
|
| 9 |
The method states.RSTState.nested_parse() provides the argument `match_titles`. |
| 10 |
With ``match_titles=True``, sections are supported, the section level is |
| 11 |
determined by the document-wide hierarchy of title styles. [1]_ |
| 12 |
|
| 13 |
In Docutils, `nested_parse()` is only used with ``match_titles=False``. |
| 14 |
None of the standard Docutils directives support section titles in the |
| 15 |
directive content. Up to Docutils 0.22, sections with current level or |
| 16 |
higher were silently dropped! |
| 17 |
|
| 18 |
Directives supporting sections in the content are defined |
| 19 |
by Sphinx extensions, e.g., "autodoc" and "kerneldoc". |
| 20 |
|
| 21 |
.. [1] Sphinx uses the `sphinx.util.parsing._fresh_title_style_context` |
| 22 |
context manager to provide a separate title style hierarchy for |
| 23 |
nested parsing. |
| 24 |
""" |
| 25 |
|
| 26 |
from pathlib import Path |
| 27 |
import contextlib |
| 28 |
import sys |
| 29 |
import unittest |
| 30 |
|
| 31 |
if __name__ == '__main__': |
| 32 |
|
| 33 |
|
| 34 |
sys.path.insert(0, str(Path(__file__).resolve().parents[3])) |
| 35 |
|
| 36 |
from docutils import nodes |
| 37 |
from docutils.frontend import get_default_settings |
| 38 |
from docutils.parsers import rst |
| 39 |
from docutils.parsers.rst.directives import register_directive |
| 40 |
from docutils.utils import new_document |
| 41 |
|
| 42 |
|
| 43 |
class ParseIntoNode(rst.Directive): |
| 44 |
"""A directive implementing nested parsing with support for sections. |
| 45 |
""" |
| 46 |
final_argument_whitespace = True |
| 47 |
has_content = True |
| 48 |
|
| 49 |
def run(self): |
| 50 |
node = nodes.Element() |
| 51 |
node.document = self.state.document |
| 52 |
|
| 53 |
match_titles = isinstance(self.state_machine.node, |
| 54 |
(nodes.document, nodes.section)) |
| 55 |
|
| 56 |
self.state.nested_parse(self.content, input_offset=self.content_offset, |
| 57 |
node=node, match_titles=match_titles) |
| 58 |
self.state_machine.node += node.children |
| 59 |
|
| 60 |
|
| 61 |
try: |
| 62 |
while isinstance(self.state_machine.node[-1], nodes.section): |
| 63 |
self.state_machine.node = self.state_machine.node[-1] |
| 64 |
except IndexError: |
| 65 |
pass |
| 66 |
|
| 67 |
sm = self.state_machine |
| 68 |
try: |
| 69 |
while True: |
| 70 |
sm = sm.parent_state_machine |
| 71 |
sm.node = self.state_machine.node |
| 72 |
except AttributeError: |
| 73 |
pass |
| 74 |
return [] |
| 75 |
|
| 76 |
|
| 77 |
class ParseIntoCurrentNode(ParseIntoNode): |
| 78 |
|
| 79 |
|
| 80 |
def run(self): |
| 81 |
self.state.nested_parse(self.content, self.content_offset, |
| 82 |
match_titles=True) |
| 83 |
return [] |
| 84 |
|
| 85 |
|
| 86 |
class ParseIntoSectionNode(ParseIntoNode): |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
def run(self): |
| 94 |
node = nodes.section() |
| 95 |
self.state.nested_parse(self.content, self.content_offset, |
| 96 |
node, match_titles=True) |
| 97 |
return node.children |
| 98 |
|
| 99 |
|
| 100 |
class FreshParseIntoNode(ParseIntoNode): |
| 101 |
"""Nested parsing with support for sections (separate title styles). |
| 102 |
|
| 103 |
* no check for section validity, |
| 104 |
* "current" node not updated! -> element order may get lost. |
| 105 |
|
| 106 |
cf. `sphinx.util.nodes.nested_parse_with_titles()` |
| 107 |
and `sphinx.util.parsing.nested_parse_to_nodes()` |
| 108 |
""" |
| 109 |
def run(self): |
| 110 |
node = nodes.Element() |
| 111 |
with _fresh_title_style_context(self.state): |
| 112 |
self.state.nested_parse(self.content, self.content_offset, |
| 113 |
node, match_titles=True) |
| 114 |
return node.children |
| 115 |
|
| 116 |
|
| 117 |
class FreshParseIntoCurrentNode(ParseIntoNode): |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
def run(self): |
| 123 |
with _fresh_title_style_context(self.state): |
| 124 |
self.state.nested_parse(self.content, self.content_offset, |
| 125 |
match_titles=True) |
| 126 |
return [] |
| 127 |
|
| 128 |
|
| 129 |
@contextlib.contextmanager |
| 130 |
def _fresh_title_style_context(state): |
| 131 |
|
| 132 |
memo = state.memo |
| 133 |
surrounding_title_styles = memo.title_styles |
| 134 |
memo.title_styles = [] |
| 135 |
memo.section_level = 0 |
| 136 |
try: |
| 137 |
yield |
| 138 |
finally: |
| 139 |
memo.title_styles = surrounding_title_styles |
| 140 |
|
| 141 |
|
| 142 |
class ParserTestCase(unittest.TestCase): |
| 143 |
maxDiff = None |
| 144 |
|
| 145 |
def test_parser(self): |
| 146 |
register_directive('nested', ParseIntoNode) |
| 147 |
register_directive('nested-current', ParseIntoCurrentNode) |
| 148 |
register_directive('nested-section', ParseIntoSectionNode) |
| 149 |
register_directive('fresh', FreshParseIntoNode) |
| 150 |
register_directive('fresh-current', FreshParseIntoCurrentNode) |
| 151 |
parser = rst.Parser() |
| 152 |
settings = get_default_settings(rst.Parser) |
| 153 |
settings.legacy_ids = False |
| 154 |
settings.warning_stream = '' |
| 155 |
settings.halt_level = 5 |
| 156 |
for name, cases in totest.items(): |
| 157 |
for casenum, (case_input, case_expected) in enumerate(cases): |
| 158 |
with self.subTest(id=f'totest[{name!r}][{casenum}]'): |
| 159 |
document = new_document('test data', settings.copy()) |
| 160 |
parser.parse(case_input, document) |
| 161 |
try: |
| 162 |
document.validate() |
| 163 |
except nodes.ValidationError as e: |
| 164 |
document.append(document.reporter.warning( |
| 165 |
str(e), base_node=e.problematic_element or None)) |
| 166 |
output = document.pformat() |
| 167 |
self.assertEqual(case_expected, output) |
| 168 |
|
| 169 |
|
| 170 |
totest = {} |
| 171 |
|
| 172 |
totest['nested_parsing'] = [ |
| 173 |
|
| 174 |
["""\ |
| 175 |
sec1 |
| 176 |
==== |
| 177 |
sec1.1 |
| 178 |
------ |
| 179 |
.. nested:: |
| 180 |
|
| 181 |
nested1.1.1 |
| 182 |
*********** |
| 183 |
nested1.1.1.1 |
| 184 |
~~~~~~~~~~~~~ |
| 185 |
|
| 186 |
sec2 |
| 187 |
==== |
| 188 |
.. nested:: |
| 189 |
|
| 190 |
skipping2.1 |
| 191 |
*********** |
| 192 |
nested2.1 |
| 193 |
--------- |
| 194 |
nested2.2 |
| 195 |
--------- |
| 196 |
inaccessible2 |
| 197 |
============= |
| 198 |
|
| 199 |
sec2.2 |
| 200 |
------ |
| 201 |
skipping2.2.1 |
| 202 |
~~~~~~~~~~~~~ |
| 203 |
""", |
| 204 |
"""\ |
| 205 |
<document source="test data"> |
| 206 |
<section names="sec1"> |
| 207 |
<title> |
| 208 |
sec1 |
| 209 |
<section names="sec1.1"> |
| 210 |
<title> |
| 211 |
sec1.1 |
| 212 |
<section names="nested1.1.1"> |
| 213 |
<title> |
| 214 |
nested1.1.1 |
| 215 |
<section names="nested1.1.1.1"> |
| 216 |
<title> |
| 217 |
nested1.1.1.1 |
| 218 |
<section names="sec2"> |
| 219 |
<title> |
| 220 |
sec2 |
| 221 |
<system_message level="3" line="16" source="test data" type="ERROR"> |
| 222 |
<paragraph> |
| 223 |
Inconsistent title style: skip from level 1 to 3. |
| 224 |
<literal_block xml:space="preserve"> |
| 225 |
skipping2.1 |
| 226 |
*********** |
| 227 |
<paragraph> |
| 228 |
Established title styles: = - * ~ |
| 229 |
<section names="nested2.1"> |
| 230 |
<title> |
| 231 |
nested2.1 |
| 232 |
<section names="nested2.2"> |
| 233 |
<title> |
| 234 |
nested2.2 |
| 235 |
<system_message level="3" line="22" source="test data" type="ERROR"> |
| 236 |
<paragraph> |
| 237 |
A level 1 section cannot be used here. |
| 238 |
<literal_block xml:space="preserve"> |
| 239 |
inaccessible2 |
| 240 |
============= |
| 241 |
<paragraph> |
| 242 |
Established title styles: = - * ~ |
| 243 |
<paragraph> |
| 244 |
The parent of level 1 sections cannot be reached. The parser is at section level 2 but the current node has only 1 parent section(s). |
| 245 |
One reason may be a high level section used in a directive that parses its content into a base node not attached to the document |
| 246 |
(up to Docutils 0.21, these sections were silently dropped). |
| 247 |
<section names="sec2.2"> |
| 248 |
<title> |
| 249 |
sec2.2 |
| 250 |
<system_message level="3" line="27" source="test data" type="ERROR"> |
| 251 |
<paragraph> |
| 252 |
Inconsistent title style: skip from level 2 to 4. |
| 253 |
<literal_block xml:space="preserve"> |
| 254 |
skipping2.2.1 |
| 255 |
~~~~~~~~~~~~~ |
| 256 |
<paragraph> |
| 257 |
Established title styles: = - * ~ |
| 258 |
"""], |
| 259 |
|
| 260 |
|
| 261 |
["""\ |
| 262 |
.. nested:: |
| 263 |
|
| 264 |
nested1 |
| 265 |
******* |
| 266 |
nested2 |
| 267 |
******* |
| 268 |
nested2.1 |
| 269 |
--------- |
| 270 |
|
| 271 |
This paragraph belongs to the last nested section. |
| 272 |
""", |
| 273 |
"""\ |
| 274 |
<document source="test data"> |
| 275 |
<section names="nested1"> |
| 276 |
<title> |
| 277 |
nested1 |
| 278 |
<section names="nested2"> |
| 279 |
<title> |
| 280 |
nested2 |
| 281 |
<section names="nested2.1"> |
| 282 |
<title> |
| 283 |
nested2.1 |
| 284 |
<paragraph> |
| 285 |
This paragraph belongs to the last nested section. |
| 286 |
"""], |
| 287 |
["""\ |
| 288 |
.. note:: The next directive is parsed with "nested_list_parse()". |
| 289 |
.. nested:: |
| 290 |
|
| 291 |
nested1 |
| 292 |
********* |
| 293 |
nested1.1 |
| 294 |
--------- |
| 295 |
|
| 296 |
This paragraph belongs to the last nested section. |
| 297 |
""", |
| 298 |
"""\ |
| 299 |
<document source="test data"> |
| 300 |
<note> |
| 301 |
<paragraph> |
| 302 |
The next directive is parsed with "nested_list_parse()". |
| 303 |
<section names="nested1"> |
| 304 |
<title> |
| 305 |
nested1 |
| 306 |
<section names="nested1.1"> |
| 307 |
<title> |
| 308 |
nested1.1 |
| 309 |
<paragraph> |
| 310 |
This paragraph belongs to the last nested section. |
| 311 |
"""], |
| 312 |
["""\ |
| 313 |
.. nested:: |
| 314 |
|
| 315 |
Keep the "current node", if the nested parse does not |
| 316 |
contain a section. |
| 317 |
|
| 318 |
This paragraph belongs to the document. |
| 319 |
""", |
| 320 |
"""\ |
| 321 |
<document source="test data"> |
| 322 |
<paragraph> |
| 323 |
Keep the "current node", if the nested parse does not |
| 324 |
contain a section. |
| 325 |
<paragraph> |
| 326 |
This paragraph belongs to the document. |
| 327 |
"""], |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
["""\ |
| 333 |
sec1 |
| 334 |
==== |
| 335 |
sec1.1 |
| 336 |
------ |
| 337 |
.. note:: The next directive is parsed with "nested_list_parse()". |
| 338 |
.. nested-current:: |
| 339 |
|
| 340 |
nc1.1.1 |
| 341 |
******* |
| 342 |
nc1.2 |
| 343 |
----- |
| 344 |
nc2 |
| 345 |
=== |
| 346 |
|
| 347 |
sec2.2 |
| 348 |
------ |
| 349 |
""", |
| 350 |
"""\ |
| 351 |
<document source="test data"> |
| 352 |
<section names="sec1"> |
| 353 |
<title> |
| 354 |
sec1 |
| 355 |
<section names="sec1.1"> |
| 356 |
<title> |
| 357 |
sec1.1 |
| 358 |
<note> |
| 359 |
<paragraph> |
| 360 |
The next directive is parsed with "nested_list_parse()". |
| 361 |
<section names="nc1.1.1"> |
| 362 |
<title> |
| 363 |
nc1.1.1 |
| 364 |
<section names="nc1.2"> |
| 365 |
<title> |
| 366 |
nc1.2 |
| 367 |
<section names="nc2"> |
| 368 |
<title> |
| 369 |
nc2 |
| 370 |
<section names="sec2.2"> |
| 371 |
<title> |
| 372 |
sec2.2 |
| 373 |
"""], |
| 374 |
|
| 375 |
["""\ |
| 376 |
sec1 |
| 377 |
==== |
| 378 |
sec1.1 |
| 379 |
------ |
| 380 |
.. nested-section:: |
| 381 |
|
| 382 |
nested-section1.1.1 |
| 383 |
******************* |
| 384 |
|
| 385 |
This paragraph belongs to the last nested section (sic!). |
| 386 |
|
| 387 |
sec2 |
| 388 |
==== |
| 389 |
""", |
| 390 |
"""\ |
| 391 |
<document source="test data"> |
| 392 |
<section names="sec1"> |
| 393 |
<title> |
| 394 |
sec1 |
| 395 |
<section names="sec1.1"> |
| 396 |
<title> |
| 397 |
sec1.1 |
| 398 |
<section names="nested-section1.1.1"> |
| 399 |
<title> |
| 400 |
nested-section1.1.1 |
| 401 |
<paragraph> |
| 402 |
This paragraph belongs to the last nested section (sic!). |
| 403 |
<section names="sec2"> |
| 404 |
<title> |
| 405 |
sec2 |
| 406 |
<system_message level="2" line="10" source="test data" type="WARNING"> |
| 407 |
<paragraph> |
| 408 |
Element <section names="sec1.1"> invalid: |
| 409 |
Child element <paragraph> not allowed at this position. |
| 410 |
"""], |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
["""\ |
| 415 |
sec1 |
| 416 |
==== |
| 417 |
.. nested-section:: |
| 418 |
|
| 419 |
nested-section1 |
| 420 |
=============== |
| 421 |
with content |
| 422 |
""", |
| 423 |
"""\ |
| 424 |
<document source="test data"> |
| 425 |
<section names="sec1"> |
| 426 |
<title> |
| 427 |
sec1 |
| 428 |
<system_message level="3" line="5" source="test data" type="ERROR"> |
| 429 |
<paragraph> |
| 430 |
A level 1 section cannot be used here. |
| 431 |
<literal_block xml:space="preserve"> |
| 432 |
nested-section1 |
| 433 |
=============== |
| 434 |
<paragraph> |
| 435 |
Established title styles: = |
| 436 |
<paragraph> |
| 437 |
The parent of level 1 sections cannot be reached. The parser is at section level 1 but the current node has only 0 parent section(s). |
| 438 |
One reason may be a high level section used in a directive that parses its content into a base node not attached to the document |
| 439 |
(up to Docutils 0.21, these sections were silently dropped). |
| 440 |
<paragraph> |
| 441 |
with content |
| 442 |
"""], |
| 443 |
|
| 444 |
["""\ |
| 445 |
.. nested:: |
| 446 |
|
| 447 |
A section in a block-quote is invalid. |
| 448 |
|
| 449 |
invalid section |
| 450 |
--------------- |
| 451 |
|
| 452 |
.. nested-current:: |
| 453 |
|
| 454 |
invalid, too |
| 455 |
============ |
| 456 |
|
| 457 |
.. nested-section:: |
| 458 |
|
| 459 |
The <section> base node is discarded. |
| 460 |
|
| 461 |
invalid section (sic!) |
| 462 |
---------------------- |
| 463 |
""", |
| 464 |
"""\ |
| 465 |
<document source="test data"> |
| 466 |
<block_quote> |
| 467 |
<paragraph> |
| 468 |
A section in a block-quote is invalid. |
| 469 |
<system_message level="3" line="6" source="test data" type="ERROR"> |
| 470 |
<paragraph> |
| 471 |
Unexpected section title. |
| 472 |
<literal_block xml:space="preserve"> |
| 473 |
invalid section |
| 474 |
--------------- |
| 475 |
<system_message level="3" line="11" source="test data" type="ERROR"> |
| 476 |
<paragraph> |
| 477 |
Unexpected section title. |
| 478 |
<literal_block xml:space="preserve"> |
| 479 |
invalid, too |
| 480 |
============ |
| 481 |
<paragraph> |
| 482 |
The <section> base node is discarded. |
| 483 |
<section names="invalid\\ section\\ (sic!)"> |
| 484 |
<title> |
| 485 |
invalid section (sic!) |
| 486 |
<system_message level="2" line="1" source="test data" type="WARNING"> |
| 487 |
<paragraph> |
| 488 |
Element <block_quote> invalid: |
| 489 |
Child element <section names="invalid\\ section\\ (sic!)"> not allowed at this position. |
| 490 |
"""], |
| 491 |
|
| 492 |
["""\ |
| 493 |
sec1 |
| 494 |
==== |
| 495 |
sec1.1 |
| 496 |
------ |
| 497 |
.. fresh:: |
| 498 |
|
| 499 |
fresh1.1.1 |
| 500 |
========== |
| 501 |
fresh1.1.1.1 |
| 502 |
~~~~~~~~~~~~~ |
| 503 |
|
| 504 |
sec2 |
| 505 |
==== |
| 506 |
.. fresh:: |
| 507 |
|
| 508 |
fresh2.1 |
| 509 |
*********** |
| 510 |
New title styles with every directive. |
| 511 |
|
| 512 |
fresh2.1.1 |
| 513 |
----------- |
| 514 |
fresh2.1.2 |
| 515 |
----------- |
| 516 |
fresh2.1.2.1 |
| 517 |
============= |
| 518 |
|
| 519 |
This text belongs into the last nested section (sic!). |
| 520 |
|
| 521 |
sec2.2 |
| 522 |
------ |
| 523 |
Document-wide title styles unchanged |
| 524 |
|
| 525 |
sec2.2.1 |
| 526 |
******** |
| 527 |
""", |
| 528 |
"""\ |
| 529 |
<document source="test data"> |
| 530 |
<section names="sec1"> |
| 531 |
<title> |
| 532 |
sec1 |
| 533 |
<section names="sec1.1"> |
| 534 |
<title> |
| 535 |
sec1.1 |
| 536 |
<section names="fresh1.1.1"> |
| 537 |
<title> |
| 538 |
fresh1.1.1 |
| 539 |
<section names="fresh1.1.1.1"> |
| 540 |
<title> |
| 541 |
fresh1.1.1.1 |
| 542 |
<section names="sec2"> |
| 543 |
<title> |
| 544 |
sec2 |
| 545 |
<section names="fresh2.1"> |
| 546 |
<title> |
| 547 |
fresh2.1 |
| 548 |
<paragraph> |
| 549 |
New title styles with every directive. |
| 550 |
<section names="fresh2.1.1"> |
| 551 |
<title> |
| 552 |
fresh2.1.1 |
| 553 |
<section names="fresh2.1.2"> |
| 554 |
<title> |
| 555 |
fresh2.1.2 |
| 556 |
<section names="fresh2.1.2.1"> |
| 557 |
<title> |
| 558 |
fresh2.1.2.1 |
| 559 |
<paragraph> |
| 560 |
This text belongs into the last nested section (sic!). |
| 561 |
<section names="sec2.2"> |
| 562 |
<title> |
| 563 |
sec2.2 |
| 564 |
<paragraph> |
| 565 |
Document-wide title styles unchanged |
| 566 |
<section names="sec2.2.1"> |
| 567 |
<title> |
| 568 |
sec2.2.1 |
| 569 |
<system_message level="2" line="27" source="test data" type="WARNING"> |
| 570 |
<paragraph> |
| 571 |
Element <section names="sec2"> invalid: |
| 572 |
Child element <paragraph> not allowed at this position. |
| 573 |
"""], |
| 574 |
|
| 575 |
["""\ |
| 576 |
sec1 |
| 577 |
==== |
| 578 |
sec1.1 |
| 579 |
------ |
| 580 |
.. fresh-current:: |
| 581 |
|
| 582 |
fc1.1.1 |
| 583 |
------- |
| 584 |
fc1.1.2 |
| 585 |
------- |
| 586 |
fc1.1.2.1 |
| 587 |
========= |
| 588 |
|
| 589 |
This text belongs into the last nested section. |
| 590 |
|
| 591 |
sec1.2 |
| 592 |
------ |
| 593 |
""", |
| 594 |
"""\ |
| 595 |
<document source="test data"> |
| 596 |
<section names="sec1"> |
| 597 |
<title> |
| 598 |
sec1 |
| 599 |
<section names="sec1.1"> |
| 600 |
<title> |
| 601 |
sec1.1 |
| 602 |
<section names="fc1.1.1"> |
| 603 |
<title> |
| 604 |
fc1.1.1 |
| 605 |
<section names="fc1.1.2"> |
| 606 |
<title> |
| 607 |
fc1.1.2 |
| 608 |
<section names="fc1.1.2.1"> |
| 609 |
<title> |
| 610 |
fc1.1.2.1 |
| 611 |
<paragraph> |
| 612 |
This text belongs into the last nested section. |
| 613 |
<section names="sec1.2"> |
| 614 |
<title> |
| 615 |
sec1.2 |
| 616 |
"""], |
| 617 |
] |
| 618 |
|
| 619 |
|
| 620 |
if __name__ == '__main__': |
| 621 |
unittest.main() |