From 52011bde7bef90ac550070a1dbe01378d4950b6e Mon Sep 17 00:00:00 2001 From: myk002 Date: Fri, 23 Sep 2022 11:13:49 -0700 Subject: [PATCH] share tag list between tool and commands so all relevant tag index entries get generated --- docs/sphinx_extensions/dfhack/tool_docs.py | 29 ++++++++++++++++------ 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/sphinx_extensions/dfhack/tool_docs.py b/docs/sphinx_extensions/dfhack/tool_docs.py index fef6ca15a..e9aa6e888 100644 --- a/docs/sphinx_extensions/dfhack/tool_docs.py +++ b/docs/sphinx_extensions/dfhack/tool_docs.py @@ -125,9 +125,13 @@ class DFHackToolDirectiveBase(sphinx.directives.ObjectDescription): else: return parts[-1] - def add_index_entry(self, name, tag) -> None: - indexdata = (name, self.options.get('summary', ''), '', self.env.docname, '', 0) - self.env.domaindata[tag]['objects'].append(indexdata) + def add_index_entries(self, name) -> None: + docname = self.env.docname + tags = self.env.domaindata['tag-repo']['doctags'][docname] + indexdata = (name, self.options.get('summary', ''), '', docname, '', 0) + self.env.domaindata['all']['objects'].append(indexdata) + for tag in tags: + self.env.domaindata[tag]['objects'].append(indexdata) @staticmethod def wrap_box(*children: List[nodes.Node]) -> nodes.Admonition: @@ -154,7 +158,9 @@ class DFHackToolDirective(DFHackToolDirectiveBase): def render_content(self) -> List[nodes.Node]: tag_paragraph = self.make_labeled_paragraph('Tags') - for tag in self.options.get('tags', []): + tags = self.options.get('tags', []) + self.env.domaindata['tag-repo']['doctags'][self.env.docname] = tags + for tag in tags: tag_paragraph += [ addnodes.pending_xref(tag, nodes.inline(text=tag), **{ 'reftype': 'ref', @@ -165,12 +171,11 @@ class DFHackToolDirective(DFHackToolDirectiveBase): }), nodes.inline(text=' | '), ] - self.add_index_entry(self.get_name_or_docname(), tag) tag_paragraph.pop() ret_nodes = [tag_paragraph] if 'no-command' in self.options: - self.add_index_entry(self.get_name_or_docname() + ' (plugin)', 'all') + self.add_index_entries(self.get_name_or_docname() + ' (plugin)') ret_nodes += [make_summary(self.env.app.builder, self.options.get('summary', ''))] return ret_nodes @@ -188,7 +193,7 @@ class DFHackCommandDirective(DFHackToolDirectiveBase): def render_content(self) -> List[nodes.Node]: command = self.get_name_or_docname() - self.add_index_entry(command, 'all') + self.add_index_entries(command) return [ self.make_labeled_paragraph('Command', command, content_class=nodes.literal), make_summary(self.env.app.builder, self.options.get('summary', '')), @@ -196,6 +201,15 @@ class DFHackCommandDirective(DFHackToolDirectiveBase): ] +class TagRepoDomain(Domain): + name = 'tag-repo' + label = 'Holds tag associations per document' + initial_data = {'doctags': {}} + + def merge_domaindata(self, docnames: List[str], otherdata: Dict) -> None: + self.data['doctags'].update(otherdata['doctags']) + + def get_tags(): groups = {} group_re = re.compile(r'"([^"]+)"') @@ -277,6 +291,7 @@ def register(app): def setup(app): app.connect('builder-inited', register) + app.add_domain(TagRepoDomain) register_index(app, 'all', 'Index of DFHack tools') init_tag_indices(app)