Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e05812

Browse files
committed
Refactor everything to use make_labeled_paragraph()
1 parent fe7414b commit 7e05812

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

docs/sphinx_extensions/dfhack/tool_docs.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import logging
77
import os
8-
from typing import List
8+
from typing import List, Optional
99

1010
import docutils.nodes as nodes
1111
import docutils.parsers.rst.directives as rst_directives
@@ -18,6 +18,21 @@
1818

1919
logger = sphinx.util.logging.getLogger(__name__)
2020

21+
22+
def make_labeled_paragraph(label: Optional[str]=None, content: Optional[str]=None,
23+
label_class=nodes.strong, content_class=nodes.inline) -> nodes.paragraph:
24+
p = nodes.paragraph('', '')
25+
if label is not None:
26+
p += [
27+
# TODO: use inline instead of strong when rendering to text
28+
label_class('', '{}:'.format(label)),
29+
nodes.inline('', ' '),
30+
]
31+
if content is not None:
32+
p += content_class('', content)
33+
return p
34+
35+
2136
_KEYBINDS = {}
2237
_KEYBINDS_RENDERED = set() # commands whose keybindings have been rendered
2338

@@ -54,10 +69,7 @@ def render_dfhack_keybind(command) -> List[nodes.paragraph]:
5469
if command not in _KEYBINDS:
5570
return out
5671
for keycmd, key, ctx in _KEYBINDS[command]:
57-
n = nodes.paragraph()
58-
# TODO: use inline instead of strong when rendering to text
59-
n += nodes.strong('Keybinding:', 'Keybinding:')
60-
n += nodes.inline(' ', ' ')
72+
n = make_labeled_paragraph('Keybinding')
6173
for k in key:
6274
n += nodes.inline(k, k, classes=['kbd'])
6375
if keycmd != command:
@@ -95,15 +107,6 @@ def get_name_or_docname(self):
95107
else:
96108
return self.env.docname.split('/')[-1]
97109

98-
@staticmethod
99-
def make_labeled_paragraph(label, content, label_class=nodes.strong, content_class=nodes.inline) -> nodes.paragraph:
100-
return nodes.paragraph('', '', *[
101-
# TODO: use inline instead of strong when rendering to text
102-
label_class('', '{}:'.format(label)),
103-
nodes.inline(text=' '),
104-
content_class('', content),
105-
])
106-
107110
@staticmethod
108111
def wrap_box(*children: List[nodes.Node]) -> nodes.Admonition:
109112
return nodes.topic('', *children, classes=['dfhack-tool-summary'])
@@ -123,10 +126,9 @@ class DFHackToolDirective(DFHackToolDirectiveBase):
123126
}
124127

125128
def render_content(self) -> List[nodes.Node]:
126-
# TODO: use inline instead of strong when rendering to text
127-
tag_nodes = [nodes.strong(text='Tags:'), nodes.inline(text=' ')]
129+
tag_paragraph = make_labeled_paragraph('Tags')
128130
for tag in self.options.get('tags', []):
129-
tag_nodes += [
131+
tag_paragraph += [
130132
addnodes.pending_xref(tag, nodes.inline(text=tag), **{
131133
'reftype': 'ref',
132134
'refdomain': 'std',
@@ -136,11 +138,9 @@ def render_content(self) -> List[nodes.Node]:
136138
}),
137139
nodes.inline(text=' | '),
138140
]
139-
tag_nodes.pop()
141+
tag_paragraph.pop()
140142

141-
ret_nodes = [
142-
nodes.paragraph('', '', *tag_nodes),
143-
]
143+
ret_nodes = [tag_paragraph]
144144
if 'no-command' in self.options:
145145
ret_nodes += [nodes.paragraph('', '', nodes.inline(text=self.options.get('summary', '')))]
146146
return ret_nodes
@@ -160,7 +160,7 @@ class DFHackCommandDirective(DFHackToolDirectiveBase):
160160
def render_content(self) -> List[nodes.Node]:
161161
command = self.get_name_or_docname()
162162
return [
163-
self.make_labeled_paragraph('Command', command, content_class=nodes.literal),
163+
make_labeled_paragraph('Command', command, content_class=nodes.literal),
164164
nodes.paragraph('', '', nodes.inline(text=self.options.get('summary', ''))),
165165
*render_dfhack_keybind(command),
166166
]

0 commit comments

Comments
 (0)