Skip to content

Commit 25d44f5

Browse files
jnsnowMarkus Armbruster
authored andcommitted
docs/qapi-domain: add namespaced index support
Generate an index-per-namespace for the QAPI domain. Due to a limitation with Sphinx's architecture, these indices must be defined during setup time and cannot be dynamically created on-demand when a namespace directive is encountered. Owing to that limitation, add a configuration value to conf.py that specifies which QAPI namespaces we'll generate indices for. Indices will be named after their namespace, e.g. the "QMP" namespace will generate to "qapi-qmp-index.html" and can be referenced using `qapi-qmp-index`. 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 7127e14 commit 25d44f5

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

docs/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
"see also",
162162
}
163163

164+
# Due to a limitation in Sphinx, we need to know which indices to
165+
# generate in advance. Adding a namespace here allows that generation.
166+
qapi_namespaces = set()
164167

165168
# -- Options for HTML output ----------------------------------------------
166169

docs/sphinx/qapi_domain.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
from __future__ import annotations
99

1010
import re
11+
import types
1112
from typing import (
1213
TYPE_CHECKING,
1314
List,
1415
NamedTuple,
1516
Tuple,
17+
Type,
1618
cast,
1719
)
1820

@@ -669,6 +671,7 @@ class QAPIIndex(Index):
669671
name = "index"
670672
localname = _("QAPI Index")
671673
shortname = _("QAPI Index")
674+
namespace = ""
672675

673676
def generate(
674677
self,
@@ -678,36 +681,35 @@ def generate(
678681
content: Dict[str, List[IndexEntry]] = {}
679682
collapse = False
680683

681-
# list of all object (name, ObjectEntry) pairs, sorted by name
682-
# (ignoring the module)
683-
objects = sorted(
684-
self.domain.objects.items(),
685-
key=lambda x: x[0].split(".")[-1].lower(),
686-
)
687-
688-
for objname, obj in objects:
684+
for objname, obj in self.domain.objects.items():
689685
if docnames and obj.docname not in docnames:
690686
continue
691687

692-
# Strip the module name out:
693-
objname = objname.split(".")[-1]
688+
ns, _mod, name = QAPIDescription.split_fqn(objname)
689+
690+
if self.namespace != ns:
691+
continue
694692

695693
# Add an alphabetical entry:
696-
entries = content.setdefault(objname[0].upper(), [])
694+
entries = content.setdefault(name[0].upper(), [])
697695
entries.append(
698696
IndexEntry(
699-
objname, 0, obj.docname, obj.node_id, obj.objtype, "", ""
697+
name, 0, obj.docname, obj.node_id, obj.objtype, "", ""
700698
)
701699
)
702700

703701
# Add a categorical entry:
704702
category = obj.objtype.title() + "s"
705703
entries = content.setdefault(category, [])
706704
entries.append(
707-
IndexEntry(objname, 0, obj.docname, obj.node_id, "", "", "")
705+
IndexEntry(name, 0, obj.docname, obj.node_id, "", "", "")
708706
)
709707

710-
# alphabetically sort categories; type names first, ABC entries last.
708+
# Sort entries within each category alphabetically
709+
for category in content:
710+
content[category] = sorted(content[category])
711+
712+
# Sort the categories themselves; type names first, ABC entries last.
711713
sorted_content = sorted(
712714
content.items(),
713715
key=lambda x: (len(x[0]) == 1, x[0]),
@@ -780,6 +782,21 @@ def objects(self) -> Dict[str, ObjectEntry]:
780782
ret = self.data.setdefault("objects", {})
781783
return ret # type: ignore[no-any-return]
782784

785+
def setup(self) -> None:
786+
namespaces = set(self.env.app.config.qapi_namespaces)
787+
for namespace in namespaces:
788+
new_index: Type[QAPIIndex] = types.new_class(
789+
f"{namespace}Index", bases=(QAPIIndex,)
790+
)
791+
new_index.name = f"{namespace.lower()}-index"
792+
new_index.localname = _(f"{namespace} Index")
793+
new_index.shortname = _(f"{namespace} Index")
794+
new_index.namespace = namespace
795+
796+
self.indices.append(new_index)
797+
798+
super().setup()
799+
783800
def note_object(
784801
self,
785802
name: str,
@@ -1019,6 +1036,12 @@ def setup(app: Sphinx) -> Dict[str, Any]:
10191036
"env", # Setting impacts parsing phase
10201037
types=set,
10211038
)
1039+
app.add_config_value(
1040+
"qapi_namespaces",
1041+
set(),
1042+
"env",
1043+
types=set,
1044+
)
10221045
app.add_domain(QAPIDomain)
10231046

10241047
return {

0 commit comments

Comments
 (0)