-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathnodes.py
More file actions
217 lines (153 loc) · 5.77 KB
/
nodes.py
File metadata and controls
217 lines (153 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
sphinx_proof.nodes
~~~~~~~~~~~~~~~~~~
Enumerable and unenumerable nodes
:copyright: Copyright 2020 by the QuantEcon team, see AUTHORS
:licences: see LICENSE for details
"""
from docutils import nodes
from docutils.nodes import Node
from sphinx.writers.latex import LaTeXTranslator
from sphinx.locale import get_translation
from sphinx.util import logging
logger = logging.getLogger(__name__)
MESSAGE_CATALOG_NAME = "proof"
_ = get_translation(MESSAGE_CATALOG_NAME)
CR = "\n"
latex_admonition_start = CR + "\\begin{sphinxadmonition}{note}"
latex_admonition_end = "\\end{sphinxadmonition}" + CR
def visit_enumerable_node(self, node: Node) -> None:
if isinstance(self, LaTeXTranslator):
docname = find_parent(self.builder.env, node, "section")
self.body.append("\\label{" + f"{docname}:{node.attributes['label']}" + "}")
self.body.append(latex_admonition_start)
else:
self.body.append(self.starttag(node, "div", CLASS="admonition"))
def depart_enumerable_node(self, node: Node) -> None:
countertyp = node.attributes.get("countertype", "")
realtyp = node.attributes.get("realtype", "")
if isinstance(self, LaTeXTranslator):
number = get_node_number(self, node, countertyp)
idx = list_rindex(self.body, latex_admonition_start) + 2
self.body.insert(idx, f"{realtyp.title()} {number}")
self.body.append(latex_admonition_end)
else:
# Find index in list of 'Proof #'
number = get_node_number(self, node, countertyp)
idx = self.body.index(f"{countertyp} {number} ")
self.body[idx] = f"{_(realtyp.title())} {number} "
self.body.append("</div>")
def visit_unenumerable_node(self, node: Node) -> None:
if isinstance(self, LaTeXTranslator):
docname = find_parent(self.builder.env, node, "section")
self.body.append("\\label{" + f"{docname}:{node.attributes['label']}" + "}")
self.body.append(latex_admonition_start)
else:
self.body.append(self.starttag(node, "div", CLASS="admonition"))
def depart_unenumerable_node(self, node: Node) -> None:
realtyp = node.attributes.get("realtype", "")
title = node.attributes.get("title", "")
if isinstance(self, LaTeXTranslator):
idx = list_rindex(self.body, latex_admonition_start) + 2
self.body.insert(idx, f"{realtyp.title()}")
self.body.append(latex_admonition_end)
else:
if title == "":
idx = list_rindex(self.body, '<p class="admonition-title">') + 1
else:
idx = list_rindex(self.body, title)
element = f"<span>{_(realtyp.title())} </span>"
self.body.insert(idx, element)
self.body.append("</div>")
def visit_proof_node(self, node: Node) -> None:
pass
def depart_proof_node(self, node: Node) -> None:
pass
def get_node_number(self, node: Node, countertyp) -> str:
"""Get the number for the directive node for HTML."""
ids = node.attributes.get("ids", [])[0]
key = countertyp
if isinstance(self, LaTeXTranslator):
docname = find_parent(self.builder.env, node, "section")
fignumbers = self.builder.env.toc_fignumbers.get(
docname, {}
) # Latex does not have builder.fignumbers
else:
fignumbers = self.builder.fignumbers
if self.builder.name == "singlehtml":
key = "%s/%s" % (self.docnames[-1], countertyp)
number = fignumbers.get(key, {}).get(ids, ())
return ".".join(map(str, number))
def find_parent(env, node, parent_tag):
"""Find the nearest parent node with the given tagname."""
while True:
node = node.parent
if node is None:
return None
# parent should be a document in toc
if (
"docname" in node.attributes
and env.titles[node.attributes["docname"]].astext().lower()
in node.attributes["names"]
):
return node.attributes["docname"]
if node.tagname == parent_tag:
return node.attributes["docname"]
return None
def list_rindex(li, x) -> int:
"""Getting the last occurence of an item in a list."""
for i in reversed(range(len(li))):
if li[i] == x:
return i
raise ValueError("{} is not in list".format(x))
class proof_node(nodes.Admonition, nodes.Element):
pass
class axiom_node(nodes.Admonition, nodes.Element):
pass
class theorem_node(nodes.Admonition, nodes.Element):
pass
class lemma_node(nodes.Admonition, nodes.Element):
pass
class algorithm_node(nodes.Admonition, nodes.Element):
pass
class definition_node(nodes.Admonition, nodes.Element):
pass
class remark_node(nodes.Admonition, nodes.Element):
pass
class conjecture_node(nodes.Admonition, nodes.Element):
pass
class corollary_node(nodes.Admonition, nodes.Element):
pass
class criterion_node(nodes.Admonition, nodes.Element):
pass
class example_node(nodes.Admonition, nodes.Element):
pass
class property_node(nodes.Admonition, nodes.Element):
pass
class observation_node(nodes.Admonition, nodes.Element):
pass
class proposition_node(nodes.Admonition, nodes.Element):
pass
class unenumerable_node(nodes.Admonition, nodes.Element):
pass
class assumption_node(nodes.Admonition, nodes.Element):
pass
class notation_node(nodes.Admonition, nodes.Element):
pass
NODE_TYPES = {
"axiom": axiom_node,
"theorem": theorem_node,
"lemma": lemma_node,
"algorithm": algorithm_node,
"definition": definition_node,
"remark": remark_node,
"conjecture": conjecture_node,
"corollary": corollary_node,
"criterion": criterion_node,
"example": example_node,
"property": property_node,
"observation": observation_node,
"proposition": proposition_node,
"assumption": assumption_node,
"notation": notation_node,
}