[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 07/23] docs/qapidoc: add transmogrifier class stub
From: |
John Snow |
Subject: |
[PATCH v2 07/23] docs/qapidoc: add transmogrifier class stub |
Date: |
Tue, 14 Jan 2025 13:58:24 -0500 |
Add the beginnings of the Transmogrifier class by adding the rST
conversion helpers that will be used to build the virtual rST document.
This version of the class does not actually "do anything" yet; each
individual feature is added one-at-a-time in the forthcoming commits.
Signed-off-by: John Snow <jsnow@redhat.com>
---
docs/sphinx/qapidoc.py | 66 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/docs/sphinx/qapidoc.py b/docs/sphinx/qapidoc.py
index dc72f3fd3f3..6593c3f28cd 100644
--- a/docs/sphinx/qapidoc.py
+++ b/docs/sphinx/qapidoc.py
@@ -24,6 +24,7 @@
https://www.sphinx-doc.org/en/master/development/index.html
"""
+from contextlib import contextmanager
import os
import re
import sys
@@ -32,11 +33,12 @@
from docutils import nodes
from docutils.parsers.rst import Directive, directives
-from docutils.statemachine import ViewList
+from docutils.statemachine import StringList, ViewList
from qapi.error import QAPIError, QAPISemError
from qapi.gen import QAPISchemaVisitor
from qapi.parser import QAPIDoc
from qapi.schema import QAPISchema
+from qapi.source import QAPISourceInfo
from sphinx import addnodes
from sphinx.directives.code import CodeBlock
@@ -61,6 +63,68 @@ def dedent(text: str) -> str:
return lines[0] + textwrap.dedent("".join(lines[1:]))
+class Transmogrifier:
+ def __init__(self, schema):
+ self._result = StringList()
+ self.indent = 0
+
+ # General-purpose rST generation functions
+
+ def get_indent(self) -> str:
+ return " " * self.indent
+
+ @contextmanager
+ def indented(self):
+ self.indent += 1
+ try:
+ yield
+ finally:
+ self.indent -= 1
+
+ def add_line_raw(self, line: str, source: str, *lineno: int) -> None:
+ """Append one line of generated reST to the output."""
+
+ # NB: Sphinx uses zero-indexed lines; subtract one.
+ lineno = tuple((n - 1 for n in lineno))
+
+ if line.strip():
+ # not a blank line
+ self._result.append(
+ self.get_indent() + line.rstrip("\n"), source, *lineno
+ )
+ else:
+ self._result.append("", source, *lineno)
+
+ def add_line(self, content: str, info: QAPISourceInfo) -> None:
+ # NB: We *require* an info object; this works out OK because we
+ # don't document built-in objects that don't have
+ # one. Everything else should.
+ assert info
+ self.add_line_raw(content, info.fname, info.line)
+
+ def add_lines(
+ self,
+ content: str,
+ info: QAPISourceInfo,
+ ) -> None:
+ assert info
+ lines = content.splitlines(True)
+ for i, line in enumerate(lines):
+ self.add_line_raw(line, info.fname, info.line + i)
+
+ def ensure_blank_line(self) -> None:
+ # Empty document -- no blank line required.
+ if not self._result:
+ return
+
+ # Last line isn't blank, add one.
+ if self._result[-1].strip(): # pylint: disable=no-member
+ fname, line = self._result.info(-1)
+ # New blank line is credited to one-after the current last line.
+ # +2: correct for zero/one index, then increment by one.
+ self.add_line_raw("", fname, line + 2)
+
+
# Disable black auto-formatter until re-enabled:
# fmt: off
--
2.47.1
- [PATCH v2 09/23] qapi/source: allow multi-line QAPISourceInfo advancing, (continued)
- [PATCH v2 09/23] qapi/source: allow multi-line QAPISourceInfo advancing, John Snow, 2025/01/14
- [PATCH v2 11/23] docs/qapidoc: add preamble() method, John Snow, 2025/01/14
- [PATCH v2 19/23] docs/qapidoc: add visit_member() method, John Snow, 2025/01/14
- [PATCH v2 05/23] qapi/schema: add __repr__ to QAPIDoc.Section, John Snow, 2025/01/14
- [PATCH v2 12/23] docs/qapidoc: add visit_paragraph() method, John Snow, 2025/01/14
- [PATCH v2 17/23] docs/qapidoc: prepare to record entity being transmogrified, John Snow, 2025/01/14
- [PATCH v2 22/23] docs/qapidoc: implement transmogrify() method, John Snow, 2025/01/14
- [PATCH v2 03/23] docs/qapidoc: remove example section support, John Snow, 2025/01/14
- [PATCH v2 07/23] docs/qapidoc: add transmogrifier class stub,
John Snow <=
- [PATCH v2 10/23] docs/qapidoc: add visit_freeform() method, John Snow, 2025/01/14
- [PATCH v2 18/23] docs/qapidoc: add visit_returns() method, John Snow, 2025/01/14
- [PATCH v2 20/23] docs/qapidoc: add visit_sections() method, John Snow, 2025/01/14
- [PATCH v2 16/23] docs/qapidoc: add visit_feature() method, John Snow, 2025/01/14
- [PATCH v2 21/23] docs/qapidoc: add visit_entity(), John Snow, 2025/01/14
- [PATCH v2 23/23] docs/qapidoc: add transmogrifier test document, John Snow, 2025/01/14