2022-07-27 00:17:21 -06:00
|
|
|
# useful references:
|
|
|
|
# https://www.sphinx-doc.org/en/master/extdev/appapi.html
|
|
|
|
# https://www.sphinx-doc.org/en/master/development/tutorials/recipe.html
|
|
|
|
# https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#rst-directives
|
|
|
|
|
2022-08-07 00:16:38 -06:00
|
|
|
import logging
|
2022-08-06 23:37:14 -06:00
|
|
|
import os
|
2022-08-06 23:03:15 -06:00
|
|
|
from typing import List
|
|
|
|
|
2022-07-27 00:17:21 -06:00
|
|
|
import docutils.nodes as nodes
|
2022-08-06 23:03:15 -06:00
|
|
|
import docutils.parsers.rst.directives as rst_directives
|
2022-07-27 00:17:21 -06:00
|
|
|
import sphinx
|
2022-08-06 15:26:33 -06:00
|
|
|
import sphinx.addnodes as addnodes
|
2022-07-27 00:17:21 -06:00
|
|
|
import sphinx.directives
|
|
|
|
|
|
|
|
import dfhack.util
|
|
|
|
|
2022-08-06 23:37:14 -06:00
|
|
|
|
2022-08-07 00:16:38 -06:00
|
|
|
logger = sphinx.util.logging.getLogger(__name__)
|
|
|
|
|
2022-08-06 23:37:14 -06:00
|
|
|
_KEYBINDS = {}
|
2022-08-07 00:16:38 -06:00
|
|
|
_KEYBINDS_RENDERED = set() # commands whose keybindings have been rendered
|
2022-08-06 23:37:14 -06:00
|
|
|
|
|
|
|
def scan_keybinds(root, files, keybindings):
|
|
|
|
"""Add keybindings in the specified files to the
|
|
|
|
given keybindings dict.
|
|
|
|
"""
|
|
|
|
for file in files:
|
|
|
|
with open(os.path.join(root, file)) as f:
|
|
|
|
lines = [l.replace('keybinding add', '').strip() for l in f.readlines()
|
|
|
|
if l.startswith('keybinding add')]
|
|
|
|
for k in lines:
|
|
|
|
first, command = k.split(' ', 1)
|
|
|
|
bind, context = (first.split('@') + [''])[:2]
|
|
|
|
if ' ' not in command:
|
|
|
|
command = command.replace('"', '')
|
|
|
|
tool = command.split(' ')[0].replace('"', '')
|
|
|
|
keybindings[tool] = keybindings.get(tool, []) + [
|
|
|
|
(command, bind.split('-'), context)]
|
|
|
|
|
|
|
|
def scan_all_keybinds(root_dir):
|
|
|
|
"""Get the implemented keybinds, and return a dict of
|
|
|
|
{tool: [(full_command, keybinding, context), ...]}.
|
|
|
|
"""
|
|
|
|
keybindings = dict()
|
|
|
|
for root, _, files in os.walk(root_dir):
|
|
|
|
scan_keybinds(root, files, keybindings)
|
|
|
|
return keybindings
|
|
|
|
|
|
|
|
|
|
|
|
def render_dfhack_keybind(command) -> List[nodes.paragraph]:
|
2022-08-07 00:16:38 -06:00
|
|
|
_KEYBINDS_RENDERED.add(command)
|
2022-08-06 23:51:38 -06:00
|
|
|
out = []
|
2022-08-06 23:37:14 -06:00
|
|
|
if command not in _KEYBINDS:
|
2022-08-06 23:51:38 -06:00
|
|
|
return out
|
2022-08-06 23:37:14 -06:00
|
|
|
for keycmd, key, ctx in _KEYBINDS[command]:
|
|
|
|
n = nodes.paragraph()
|
|
|
|
n += nodes.strong('Keybinding:', 'Keybinding:')
|
|
|
|
n += nodes.inline(' ', ' ')
|
|
|
|
for k in key:
|
|
|
|
n += nodes.inline(k, k, classes=['kbd'])
|
|
|
|
if keycmd != command:
|
|
|
|
n += nodes.inline(' -> ', ' -> ')
|
|
|
|
n += nodes.literal(keycmd, keycmd, classes=['guilabel'])
|
|
|
|
if ctx:
|
|
|
|
n += nodes.inline(' in ', ' in ')
|
|
|
|
n += nodes.literal(ctx, ctx)
|
2022-08-06 23:51:38 -06:00
|
|
|
out.append(n)
|
|
|
|
return out
|
2022-08-06 23:37:14 -06:00
|
|
|
|
|
|
|
|
2022-08-07 00:16:38 -06:00
|
|
|
def check_missing_keybinds():
|
|
|
|
# FIXME: _KEYBINDS_RENDERED is empty in the parent process under parallel builds
|
|
|
|
# consider moving to a sphinx Domain to solve this properly
|
|
|
|
for missing_command in sorted(set(_KEYBINDS.keys()) - _KEYBINDS_RENDERED):
|
|
|
|
logger.warning('Undocumented keybindings for command: %s', missing_command)
|
|
|
|
|
|
|
|
|
2022-08-06 23:37:14 -06:00
|
|
|
# pylint:disable=unused-argument,dangerous-default-value,too-many-arguments
|
|
|
|
def dfhack_keybind_role(role, rawtext, text, lineno, inliner,
|
|
|
|
options={}, content=[]):
|
|
|
|
"""Custom role parser for DFHack default keybinds."""
|
|
|
|
return render_dfhack_keybind(text), []
|
|
|
|
|
|
|
|
|
2022-08-06 21:08:51 -06:00
|
|
|
class DFHackToolDirectiveBase(sphinx.directives.ObjectDescription):
|
2022-07-27 00:17:21 -06:00
|
|
|
has_content = False
|
2022-08-06 14:24:56 -06:00
|
|
|
required_arguments = 0
|
2022-08-06 21:12:26 -06:00
|
|
|
optional_arguments = 1
|
2022-07-27 00:17:21 -06:00
|
|
|
|
2022-08-06 21:08:51 -06:00
|
|
|
def get_name_or_docname(self):
|
2022-08-06 14:24:56 -06:00
|
|
|
if self.arguments:
|
2022-08-06 21:08:51 -06:00
|
|
|
return self.arguments[0]
|
2022-08-06 14:24:56 -06:00
|
|
|
else:
|
2022-08-06 21:08:51 -06:00
|
|
|
return self.env.docname.split('/')[-1]
|
|
|
|
|
2022-08-06 23:03:15 -06:00
|
|
|
@staticmethod
|
|
|
|
def make_labeled_paragraph(label, content, label_class=nodes.strong, content_class=nodes.inline) -> nodes.paragraph:
|
2022-08-06 21:08:51 -06:00
|
|
|
return nodes.paragraph('', '', *[
|
2022-08-08 00:19:07 -06:00
|
|
|
label_class('', '{}:'.format(label)),
|
|
|
|
nodes.inline(text=' '),
|
2022-08-06 21:11:12 -06:00
|
|
|
content_class('', content),
|
2022-08-06 21:08:51 -06:00
|
|
|
])
|
|
|
|
|
2022-08-06 23:03:15 -06:00
|
|
|
@staticmethod
|
|
|
|
def wrap_box(*children: List[nodes.Node]) -> nodes.Admonition:
|
2022-08-08 00:29:22 -06:00
|
|
|
return nodes.topic('', *children, classes=['dfhack-tool-summary'])
|
2022-08-06 23:03:15 -06:00
|
|
|
|
|
|
|
def render_content(self) -> List[nodes.Node]:
|
2022-08-06 21:08:51 -06:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def run(self):
|
2022-08-06 23:03:15 -06:00
|
|
|
return [self.wrap_box(*self.render_content())]
|
2022-07-27 00:17:21 -06:00
|
|
|
|
2022-08-06 21:08:51 -06:00
|
|
|
|
|
|
|
class DFHackToolDirective(DFHackToolDirectiveBase):
|
|
|
|
option_spec = {
|
|
|
|
'tags': dfhack.util.directive_arg_str_list,
|
2022-08-06 23:03:15 -06:00
|
|
|
'no-command': rst_directives.flag,
|
2022-08-06 21:08:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-06 23:03:15 -06:00
|
|
|
def render_content(self) -> List[nodes.Node]:
|
2022-08-08 00:19:07 -06:00
|
|
|
tag_nodes = [nodes.strong(text='Tags:'), nodes.inline(text=' ')]
|
2022-07-27 20:02:08 -06:00
|
|
|
for tag in self.options.get('tags', []):
|
2022-07-27 00:17:21 -06:00
|
|
|
tag_nodes += [
|
2022-08-06 15:26:33 -06:00
|
|
|
addnodes.pending_xref(tag, nodes.inline(text=tag), **{
|
|
|
|
'reftype': 'ref',
|
|
|
|
'refdomain': 'std',
|
|
|
|
'reftarget': 'tag/' + tag,
|
|
|
|
'refexplicit': False,
|
|
|
|
'refwarn': True,
|
|
|
|
}),
|
2022-07-27 00:17:21 -06:00
|
|
|
nodes.inline(text=' | '),
|
|
|
|
]
|
|
|
|
tag_nodes.pop()
|
|
|
|
|
|
|
|
return [
|
2022-08-06 21:08:51 -06:00
|
|
|
nodes.paragraph('', '', *tag_nodes),
|
|
|
|
]
|
|
|
|
|
2022-08-06 23:03:15 -06:00
|
|
|
def run(self):
|
|
|
|
out = DFHackToolDirectiveBase.run(self)
|
|
|
|
if 'no-command' not in self.options:
|
|
|
|
out += [self.wrap_box(*DFHackCommandDirective.render_content(self))]
|
|
|
|
return out
|
|
|
|
|
2022-08-06 21:08:51 -06:00
|
|
|
|
|
|
|
class DFHackCommandDirective(DFHackToolDirectiveBase):
|
2022-08-06 23:03:15 -06:00
|
|
|
def render_content(self) -> List[nodes.Node]:
|
2022-08-06 23:37:14 -06:00
|
|
|
command = self.get_name_or_docname()
|
2022-08-06 21:08:51 -06:00
|
|
|
return [
|
2022-08-06 23:37:14 -06:00
|
|
|
self.make_labeled_paragraph('Command', command, content_class=nodes.literal),
|
|
|
|
*render_dfhack_keybind(command),
|
2022-07-27 00:17:21 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def register(app):
|
|
|
|
app.add_directive('dfhack-tool', DFHackToolDirective)
|
2022-08-06 21:08:51 -06:00
|
|
|
app.add_directive('dfhack-command', DFHackCommandDirective)
|
2022-08-06 23:37:14 -06:00
|
|
|
app.add_role('dfhack-keybind', dfhack_keybind_role)
|
|
|
|
|
|
|
|
_KEYBINDS.update(scan_all_keybinds(os.path.join(dfhack.util.DFHACK_ROOT, 'data', 'init')))
|
|
|
|
|
2022-07-27 00:17:21 -06:00
|
|
|
|
|
|
|
def setup(app):
|
|
|
|
app.connect('builder-inited', register)
|
|
|
|
|
2022-08-07 00:16:38 -06:00
|
|
|
# TODO: re-enable once detection is corrected
|
|
|
|
# app.connect('build-finished', lambda *_: check_missing_keybinds())
|
|
|
|
|
2022-07-27 00:17:21 -06:00
|
|
|
return {
|
|
|
|
'version': '0.1',
|
|
|
|
'parallel_read_safe': True,
|
|
|
|
'parallel_write_safe': True,
|
|
|
|
}
|