[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC PATCH v2 04/35] docs/sphinx: add compat.py module and nested_parse
From: |
John Snow |
Subject: |
[RFC PATCH v2 04/35] docs/sphinx: add compat.py module and nested_parse helper |
Date: |
Thu, 12 Dec 2024 20:12:33 -0500 |
Create a compat module that handles sphinx cross-version compatibility
issues. For the inaugural function, add a nested_parse() helper that
handles differences in line number tracking for nested directive body
parsing.
Spoilers: there are more cross-version hacks to come throughout the
series.
Signed-off-by: John Snow <jsnow@redhat.com>
---
docs/sphinx/compat.py | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
create mode 100644 docs/sphinx/compat.py
diff --git a/docs/sphinx/compat.py b/docs/sphinx/compat.py
new file mode 100644
index 00000000000..4c8d1e94530
--- /dev/null
+++ b/docs/sphinx/compat.py
@@ -0,0 +1,33 @@
+"""
+Sphinx cross-version compatibility goop
+"""
+
+from docutils.nodes import Element
+
+from sphinx.util.docutils import SphinxDirective, switch_source_input
+from sphinx.util.nodes import nested_parse_with_titles
+
+
+def nested_parse(directive: SphinxDirective, content_node: Element) -> None:
+ """
+ This helper preserves error parsing context across sphinx versions.
+ """
+
+ # necessary so that the child nodes get the right source/line set
+ content_node.document = directive.state.document
+
+ try:
+ # Modern sphinx (6.2.0+) supports proper offsetting for
+ # nested parse error context management
+ nested_parse_with_titles(
+ directive.state,
+ directive.content,
+ content_node,
+ content_offset=directive.content_offset, # type: ignore[call-arg]
+ )
+ except TypeError:
+ # No content_offset argument. Fall back to SSI method.
+ with switch_source_input(directive.state, directive.content):
+ nested_parse_with_titles(
+ directive.state, directive.content, content_node
+ )
--
2.47.0
- [RFC PATCH v2 00/35] Add qapi-domain Sphinx extension, John Snow, 2024/12/12
- [RFC PATCH v2 01/35] do-not-merge, John Snow, 2024/12/12
- [RFC PATCH v2 02/35] pylint touchups, John Snow, 2024/12/12
- [RFC PATCH v2 03/35] docs/sphinx: create QAPI domain extension stub, John Snow, 2024/12/12
- [RFC PATCH v2 04/35] docs/sphinx: add compat.py module and nested_parse helper,
John Snow <=
- [RFC PATCH v2 05/35] docs/qapi-domain: add qapi:module directive, John Snow, 2024/12/12
- [RFC PATCH v2 08/35] docs/qapi-domain: add resolve_any_xref(), John Snow, 2024/12/12
- [RFC PATCH v2 07/35] docs/qapi-domain: add QAPI index, John Snow, 2024/12/12
- [RFC PATCH v2 09/35] docs/qapi-domain: add QAPI xref roles, John Snow, 2024/12/12
- [RFC PATCH v2 06/35] docs/qapi-domain: add QAPI domain object registry, John Snow, 2024/12/12
- [RFC PATCH v2 10/35] docs/qapi-domain: add compatibility node classes, John Snow, 2024/12/12
- [RFC PATCH v2 11/35] docs/qapi-domain: add qapi:command directive, John Snow, 2024/12/12
- [RFC PATCH v2 12/35] docs/qapi-domain: add :since: directive option, John Snow, 2024/12/12
- [RFC PATCH v2 13/35] docs/qapi-domain: add "Arguments:" field lists, John Snow, 2024/12/12
- [RFC PATCH v2 14/35] docs/qapi-domain: add "Features:" field lists, John Snow, 2024/12/12