Skip to content

Commit 7f6f24a

Browse files
jnsnowMarkus Armbruster
authored andcommitted
docs/qapidoc: add intermediate output debugger
Add debugging output for the qapidoc transmogrifier - setting DEBUG=1 will produce .ir files (one for each qapidoc directive) that write the generated rst file to disk to allow for easy debugging and verification of the generated document. Signed-off-by: John Snow <[email protected]> Message-ID: <[email protected]> Acked-by: Markus Armbruster <[email protected]> Signed-off-by: Markus Armbruster <[email protected]>
1 parent c9b6f98 commit 7f6f24a

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

docs/sphinx/qapidoc.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from typing import TYPE_CHECKING
3838

3939
from docutils import nodes
40-
from docutils.parsers.rst import Directive, directives
40+
from docutils.parsers.rst import directives
4141
from docutils.statemachine import StringList
4242
from qapi.error import QAPIError
4343
from qapi.parser import QAPIDoc
@@ -60,7 +60,7 @@
6060
from sphinx.directives.code import CodeBlock
6161
from sphinx.errors import ExtensionError
6262
from sphinx.util import logging
63-
from sphinx.util.docutils import switch_source_input
63+
from sphinx.util.docutils import SphinxDirective, switch_source_input
6464
from sphinx.util.nodes import nested_parse_with_titles
6565

6666

@@ -414,7 +414,7 @@ def visit_module(self, name: str) -> None:
414414
super().visit_module(name)
415415

416416

417-
class NestedDirective(Directive):
417+
class NestedDirective(SphinxDirective):
418418
def run(self) -> Sequence[nodes.Node]:
419419
raise NotImplementedError
420420

@@ -483,10 +483,43 @@ def transmogrify(self, schema: QAPISchema) -> nodes.Element:
483483
node.document = self.state.document
484484
self.state.nested_parse(content, 0, contentnode)
485485
logger.info("Transmogrifier's nested parse completed.")
486-
sys.stdout.flush()
487486

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()
488493
return contentnode
489494

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+
490523
def legacy(self, schema: QAPISchema) -> nodes.Element:
491524
vis = QAPISchemaGenRSTVisitor(self)
492525
vis.visit_begin(schema)

0 commit comments

Comments
 (0)