create genindex entries for all tools and commands

develop
Myk Taylor 2022-09-20 22:51:44 -07:00
parent ab611f36e6
commit 35152bf022
No known key found for this signature in database
1 changed files with 20 additions and 2 deletions

@ -8,14 +8,18 @@ import os
from typing import List, Optional, Type from typing import List, Optional, Type
import docutils.nodes as nodes import docutils.nodes as nodes
from docutils.nodes import Node
import docutils.parsers.rst.directives as rst_directives import docutils.parsers.rst.directives as rst_directives
import sphinx import sphinx
import sphinx.addnodes as addnodes import sphinx.addnodes as addnodes
import sphinx.directives import sphinx.directives
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import process_index_entry
import dfhack.util import dfhack.util
logger = sphinx.util.logging.getLogger(__name__) logger = sphinx.util.logging.getLogger(__name__)
@ -46,6 +50,17 @@ def make_summary(builder: sphinx.builders.Builder, summary: str) -> nodes.paragr
para += nodes.inline(text=summary) para += nodes.inline(text=summary)
return para return para
def make_index(directive: SphinxDirective, name: str, summary: str) -> List[Node]:
targetid = 'index-%s' % directive.env.new_serialno('index')
targetnode = nodes.target('', '', ids=[targetid])
directive.state.document.note_explicit_target(targetnode)
indexnode = addnodes.index()
indexnode['entries'] = []
indexnode['inline'] = False
directive.set_source_info(indexnode)
entry_text = 'single: {}; {}'.format(name, summary)
indexnode['entries'].extend(process_index_entry(entry_text, targetnode['ids'][0]))
return [indexnode, targetnode]
_KEYBINDS = {} _KEYBINDS = {}
_KEYBINDS_RENDERED = set() # commands whose keybindings have been rendered _KEYBINDS_RENDERED = set() # commands whose keybindings have been rendered
@ -160,6 +175,7 @@ class DFHackToolDirective(DFHackToolDirectiveBase):
ret_nodes = [tag_paragraph] ret_nodes = [tag_paragraph]
if 'no-command' in self.options: if 'no-command' in self.options:
ret_nodes += make_index(self, self.get_name_or_docname() + ' (plugin)', self.options.get('summary', ''))
ret_nodes += [make_summary(self.env.app.builder, self.options.get('summary', ''))] ret_nodes += [make_summary(self.env.app.builder, self.options.get('summary', ''))]
return ret_nodes return ret_nodes
@ -177,11 +193,13 @@ class DFHackCommandDirective(DFHackToolDirectiveBase):
def render_content(self) -> List[nodes.Node]: def render_content(self) -> List[nodes.Node]:
command = self.get_name_or_docname() command = self.get_name_or_docname()
return [ ret_nodes = [self.make_labeled_paragraph('Command', command, content_class=nodes.literal)]
self.make_labeled_paragraph('Command', command, content_class=nodes.literal), ret_nodes += make_index(self, command, self.options.get('summary', ''))
ret_nodes += [
make_summary(self.env.app.builder, self.options.get('summary', '')), make_summary(self.env.app.builder, self.options.get('summary', '')),
*render_dfhack_keybind(command, builder=self.env.app.builder), *render_dfhack_keybind(command, builder=self.env.app.builder),
] ]
return ret_nodes
def register(app): def register(app):