|
37 | 37 | from typing import TYPE_CHECKING
|
38 | 38 |
|
39 | 39 | from docutils import nodes
|
40 |
| -from docutils.parsers.rst import Directive, directives |
| 40 | +from docutils.parsers.rst import directives |
41 | 41 | from docutils.statemachine import StringList
|
42 | 42 | from qapi.error import QAPIError
|
43 | 43 | from qapi.parser import QAPIDoc
|
|
60 | 60 | from sphinx.directives.code import CodeBlock
|
61 | 61 | from sphinx.errors import ExtensionError
|
62 | 62 | from sphinx.util import logging
|
63 |
| -from sphinx.util.docutils import switch_source_input |
| 63 | +from sphinx.util.docutils import SphinxDirective, switch_source_input |
64 | 64 | from sphinx.util.nodes import nested_parse_with_titles
|
65 | 65 |
|
66 | 66 |
|
@@ -414,7 +414,7 @@ def visit_module(self, name: str) -> None:
|
414 | 414 | super().visit_module(name)
|
415 | 415 |
|
416 | 416 |
|
417 |
| -class NestedDirective(Directive): |
| 417 | +class NestedDirective(SphinxDirective): |
418 | 418 | def run(self) -> Sequence[nodes.Node]:
|
419 | 419 | raise NotImplementedError
|
420 | 420 |
|
@@ -483,10 +483,43 @@ def transmogrify(self, schema: QAPISchema) -> nodes.Element:
|
483 | 483 | node.document = self.state.document
|
484 | 484 | self.state.nested_parse(content, 0, contentnode)
|
485 | 485 | logger.info("Transmogrifier's nested parse completed.")
|
486 |
| - sys.stdout.flush() |
487 | 486 |
|
| 487 | + if self.env.app.verbosity >= 2 or os.environ.get("DEBUG"): |
| 488 | + argname = "_".join(Path(self.arguments[0]).parts) |
| 489 | + name = Path(argname).stem + ".ir" |
| 490 | + self.write_intermediate(content, name) |
| 491 | + |
| 492 | + sys.stdout.flush() |
488 | 493 | return contentnode
|
489 | 494 |
|
| 495 | + def write_intermediate(self, content: StringList, filename: str) -> None: |
| 496 | + logger.info( |
| 497 | + "writing intermediate rST for '%s' to '%s'", |
| 498 | + self.arguments[0], |
| 499 | + filename, |
| 500 | + ) |
| 501 | + |
| 502 | + srctree = Path(self.env.app.config.qapidoc_srctree).resolve() |
| 503 | + outlines = [] |
| 504 | + lcol_width = 0 |
| 505 | + |
| 506 | + for i, line in enumerate(content): |
| 507 | + src, lineno = content.info(i) |
| 508 | + srcpath = Path(src).resolve() |
| 509 | + srcpath = srcpath.relative_to(srctree) |
| 510 | + |
| 511 | + lcol = f"{srcpath}:{lineno:04d}" |
| 512 | + lcol_width = max(lcol_width, len(lcol)) |
| 513 | + outlines.append((lcol, line)) |
| 514 | + |
| 515 | + with open(filename, "w", encoding="UTF-8") as outfile: |
| 516 | + for lcol, rcol in outlines: |
| 517 | + outfile.write(lcol.rjust(lcol_width)) |
| 518 | + outfile.write(" |") |
| 519 | + if rcol: |
| 520 | + outfile.write(f" {rcol}") |
| 521 | + outfile.write("\n") |
| 522 | + |
490 | 523 | def legacy(self, schema: QAPISchema) -> nodes.Element:
|
491 | 524 | vis = QAPISchemaGenRSTVisitor(self)
|
492 | 525 | vis.visit_begin(schema)
|
|
0 commit comments