Skip to content

Commit 5c1636f

Browse files
jnsnowMarkus Armbruster
authored andcommitted
docs/qapidoc: implement transmogrify() method
This is the true top-level processor for the new transmogrifier; responsible both for generating the intermediate rST and then running the nested parse on that generated document to produce the final docutils tree that is then - very finally - postprocessed by sphinx for final rendering to HTML &c. Signed-off-by: John Snow <[email protected]> Message-ID: <[email protected]> Acked-by: Markus Armbruster <[email protected]> [Use the opportunity to move the __version__ assignment to where PEP 8 wants it] Signed-off-by: Markus Armbruster <[email protected]>
1 parent c05de72 commit 5c1636f

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

docs/sphinx/qapidoc.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# QEMU qapidoc QAPI file parsing extension
44
#
5+
# Copyright (c) 2024-2025 Red Hat
56
# Copyright (c) 2020 Linaro
67
#
78
# This work is licensed under the terms of the GNU GPLv2 or later.
@@ -26,6 +27,8 @@
2627

2728
from __future__ import annotations
2829

30+
__version__ = "2.0"
31+
2932
from contextlib import contextmanager
3033
import os
3134
from pathlib import Path
@@ -56,6 +59,7 @@
5659
from sphinx import addnodes
5760
from sphinx.directives.code import CodeBlock
5861
from sphinx.errors import ExtensionError
62+
from sphinx.util import logging
5963
from sphinx.util.docutils import switch_source_input
6064
from sphinx.util.nodes import nested_parse_with_titles
6165

@@ -74,7 +78,7 @@
7478
from sphinx.util.typing import ExtensionMetadata
7579

7680

77-
__version__ = "1.0"
81+
logger = logging.getLogger(__name__)
7882

7983

8084
class Transmogrifier:
@@ -95,6 +99,10 @@ def __init__(self) -> None:
9599
self._result = StringList()
96100
self.indent = 0
97101

102+
@property
103+
def result(self) -> StringList:
104+
return self._result
105+
98106
@property
99107
def entity(self) -> QAPISchemaDefinition:
100108
assert self._curr_ent is not None
@@ -438,7 +446,43 @@ def new_serialno(self) -> str:
438446
return "qapidoc-%d" % env.new_serialno("qapidoc")
439447

440448
def transmogrify(self, schema: QAPISchema) -> nodes.Element:
441-
raise NotImplementedError
449+
logger.info("Transmogrifying QAPI to rST ...")
450+
vis = Transmogrifier()
451+
modules = set()
452+
453+
for doc in schema.docs:
454+
module_source = doc.info.fname
455+
if module_source not in modules:
456+
vis.visit_module(module_source)
457+
modules.add(module_source)
458+
459+
if doc.symbol:
460+
ent = schema.lookup_entity(doc.symbol)
461+
assert isinstance(ent, QAPISchemaDefinition)
462+
vis.visit_entity(ent)
463+
else:
464+
vis.visit_freeform(doc)
465+
466+
logger.info("Transmogrification complete.")
467+
468+
contentnode = nodes.section()
469+
content = vis.result
470+
titles_allowed = True
471+
472+
logger.info("Transmogrifier running nested parse ...")
473+
with switch_source_input(self.state, content):
474+
if titles_allowed:
475+
node: nodes.Element = nodes.section()
476+
node.document = self.state.document
477+
nested_parse_with_titles(self.state, content, contentnode)
478+
else:
479+
node = nodes.paragraph()
480+
node.document = self.state.document
481+
self.state.nested_parse(content, 0, contentnode)
482+
logger.info("Transmogrifier's nested parse completed.")
483+
sys.stdout.flush()
484+
485+
return contentnode
442486

443487
def legacy(self, schema: QAPISchema) -> nodes.Element:
444488
vis = QAPISchemaGenRSTVisitor(self)
@@ -572,6 +616,7 @@ def run(self) -> List[nodes.Node]:
572616

573617
def setup(app: Sphinx) -> ExtensionMetadata:
574618
"""Register qapi-doc directive with Sphinx"""
619+
app.setup_extension("qapi_domain")
575620
app.add_config_value("qapidoc_srctree", None, "env")
576621
app.add_directive("qapi-doc", QAPIDocDirective)
577622
app.add_directive("qmp-example", QMPExample)

0 commit comments

Comments
 (0)