-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path__init__.py
More file actions
354 lines (294 loc) · 12.4 KB
/
Copy path__init__.py
File metadata and controls
354 lines (294 loc) · 12.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# -*- coding: utf-8 -*-
"""
sphinx_exercise
~~~~~~~~~~~~~~~
This package is an extension for sphinx to support exercise and solutions.
:copyright: Copyright 2020-2021 by the Executable Books team, see AUTHORS.
:license: MIT, see LICENSE for details.
"""
__version__ = "1.1.1"
from pathlib import Path
from typing import Any, Dict, Set, Union, cast
from sphinx.config import Config
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
from sphinx.domains.std import StandardDomain
from docutils.nodes import Node
from sphinx.util import logging
from sphinx.util.fileutil import copy_asset
from sphinx.locale import get_translation
from ._compat import findall
from .directive import (
ExerciseDirective,
ExerciseStartDirective,
ExerciseEndDirective,
SolutionDirective,
SolutionStartDirective,
SolutionEndDirective,
)
from .nodes import (
exercise_node,
visit_exercise_node,
depart_exercise_node,
exercise_enumerable_node,
visit_exercise_enumerable_node,
depart_exercise_enumerable_node,
exercise_end_node,
solution_node,
visit_solution_node,
depart_solution_node,
solution_start_node,
solution_end_node,
is_extension_node,
exercise_title,
exercise_subtitle,
solution_title,
solution_subtitle,
exercise_latex_number_reference,
visit_exercise_latex_number_reference,
depart_exercise_latex_number_reference,
)
from .transforms import (
CheckGatedDirectives,
MergeGatedSolutions,
MergeGatedExercises,
)
from .post_transforms import (
ResolveTitlesInExercises,
ResolveTitlesInSolutions,
UpdateReferencesToEnumerated,
ResolveLinkTextToSolutions,
)
logger = logging.getLogger(__name__)
MESSAGE_CATALOG_NAME = "exercise"
translate = get_translation(MESSAGE_CATALOG_NAME)
# Callback Functions
def purge_exercises(app: Sphinx, env: BuildEnvironment, docname: str) -> None:
"""Purge sphinx_exercise registry"""
if not hasattr(env, "sphinx_exercise_registry"):
return
# Purge env.sphinx_exercise_registry if matching docname
remove_labels = [
label
for (label, node) in env.sphinx_exercise_registry.items()
if node["docname"] == docname
]
if remove_labels:
for label in remove_labels:
del env.sphinx_exercise_registry[label]
# Purge node order tracking for this document
if (
hasattr(env, "sphinx_exercise_node_order")
and docname in env.sphinx_exercise_node_order
):
del env.sphinx_exercise_node_order[docname]
def merge_exercises(
app: Sphinx, env: BuildEnvironment, docnames: Set[str], other: BuildEnvironment
) -> None:
"""Merge sphinx_exercise_registry"""
if not hasattr(env, "sphinx_exercise_registry"):
env.sphinx_exercise_registry = {}
# Merge env stored data
if hasattr(other, "sphinx_exercise_registry"):
env.sphinx_exercise_registry = {
**env.sphinx_exercise_registry,
**other.sphinx_exercise_registry,
}
# Merge node order tracking
if not hasattr(env, "sphinx_exercise_node_order"):
env.sphinx_exercise_node_order = {}
if hasattr(other, "sphinx_exercise_node_order"):
env.sphinx_exercise_node_order = {
**env.sphinx_exercise_node_order,
**other.sphinx_exercise_node_order,
}
def init_numfig(app: Sphinx, config: Config) -> None:
"""Initialize numfig"""
config["numfig"] = True
numfig_format = {"exercise": f"{translate('Exercise')} %s"}
# Merge with current sphinx settings
numfig_format.update(config.numfig_format)
config.numfig_format = numfig_format
def copy_asset_files(app: Sphinx, exc: Union[bool, Exception]):
"""Copies required assets for formating in HTML"""
static_path = (
Path(__file__).parent.joinpath("assets", "html", "exercise.css").absolute()
)
asset_files = [str(static_path)]
if exc is None:
for path in asset_files:
copy_asset(path, str(Path(app.outdir).joinpath("_static").absolute()))
def validate_exercise_solution_order(app: Sphinx, env: BuildEnvironment) -> None:
"""
Validate that solutions follow their referenced exercises when
exercise_style='solution_follow_exercise' is set.
"""
# Only validate if the config option is set
if app.config.exercise_style != "solution_follow_exercise":
return
if not hasattr(env, "sphinx_exercise_node_order"):
return
logger = logging.getLogger(__name__)
# Process each document
for docname, nodes in env.sphinx_exercise_node_order.items():
# Build a map of exercise labels to their positions and info
exercise_info = {}
for i, node_info in enumerate(nodes):
if node_info["type"] == "exercise":
exercise_info[node_info["label"]] = {
"position": i,
"line": node_info.get("line"),
}
# Check each solution
for i, node_info in enumerate(nodes):
if node_info["type"] == "solution":
target_label = node_info["target_label"]
solution_label = node_info["label"]
solution_line = node_info.get("line")
if not target_label:
continue
# Check if target exercise exists in this document
if target_label not in exercise_info:
# Exercise is in a different document or doesn't exist
docpath = env.doc2path(docname)
path = str(Path(docpath).with_suffix(""))
# Build location string with line number if available
location = f"{path}:{solution_line}" if solution_line else path
logger.warning(
f"[sphinx-exercise] Solution '{solution_label}' references exercise '{target_label}' "
f"which is not in the same document. When exercise_style='solution_follow_exercise', "
f"solutions should appear in the same document as their exercises.",
location=location,
color="yellow",
)
continue
# Check if solution comes after exercise
exercise_data = exercise_info[target_label]
exercise_pos = exercise_data["position"]
exercise_line = exercise_data.get("line")
if i <= exercise_pos:
docpath = env.doc2path(docname)
path = str(Path(docpath).with_suffix(""))
# Build more informative message with line numbers
if solution_line and exercise_line:
location = f"{path}:{solution_line}"
msg = (
f"[sphinx-exercise] Solution '{solution_label}' (line {solution_line}) does not follow "
f"exercise '{target_label}' (line {exercise_line}). "
f"When exercise_style='solution_follow_exercise', solutions should "
f"appear after their referenced exercises."
)
elif solution_line:
location = f"{path}:{solution_line}"
msg = (
f"[sphinx-exercise] Solution '{solution_label}' does not follow exercise '{target_label}'. "
f"When exercise_style='solution_follow_exercise', solutions should "
f"appear after their referenced exercises."
)
else:
location = path
msg = (
f"[sphinx-exercise] Solution '{solution_label}' does not follow exercise '{target_label}'. "
f"When exercise_style='solution_follow_exercise', solutions should "
f"appear after their referenced exercises."
)
logger.warning(msg, location=location, color="yellow")
def doctree_read(app: Sphinx, document: Node) -> None:
"""
Read the doctree and apply updates to sphinx-exercise nodes
"""
domain = cast(StandardDomain, app.env.get_domain("std"))
# Initialize node order tracking for this document
if not hasattr(app.env, "sphinx_exercise_node_order"):
app.env.sphinx_exercise_node_order = {}
docname = app.env.docname
if docname not in app.env.sphinx_exercise_node_order:
app.env.sphinx_exercise_node_order[docname] = []
# Traverse sphinx-exercise nodes
for node in findall(document):
if is_extension_node(node):
name = node.get("names", [])[0]
label = document.nameids[name]
section_name = node.attributes.get("title")
domain.anonlabels[name] = docname, label
domain.labels[name] = docname, label, section_name
# Track node order for validation
node_type = node.get("type", "unknown")
node_label = node.get("label", "")
target_label = node.get("target_label", None) # Only for solution nodes
app.env.sphinx_exercise_node_order[docname].append(
{
"type": node_type,
"label": node_label,
"target_label": target_label,
"line": node.line if hasattr(node, "line") else None,
}
)
def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value("hide_solutions", False, "env")
app.add_config_value("exercise_style", "", "env")
app.connect("config-inited", init_numfig) # event order - 1
app.connect("env-purge-doc", purge_exercises) # event order - 5 per file
app.connect("doctree-read", doctree_read) # event order - 8
app.connect("env-merge-info", merge_exercises) # event order - 9
app.connect("env-updated", validate_exercise_solution_order) # event order - 10
app.connect("build-finished", copy_asset_files) # event order - 16
app.add_node(
exercise_node,
singlehtml=(visit_exercise_node, depart_exercise_node),
html=(visit_exercise_node, depart_exercise_node),
latex=(visit_exercise_node, depart_exercise_node),
)
app.add_enumerable_node(
exercise_enumerable_node,
"exercise",
None,
singlehtml=(visit_exercise_enumerable_node, depart_exercise_enumerable_node),
html=(visit_exercise_enumerable_node, depart_exercise_enumerable_node),
latex=(visit_exercise_enumerable_node, depart_exercise_enumerable_node),
)
app.add_node(
solution_node,
singlehtml=(visit_solution_node, depart_solution_node),
html=(visit_solution_node, depart_solution_node),
latex=(visit_solution_node, depart_solution_node),
)
# Internal Title Nodes that don't need visit_ and depart_ methods
# as they are resolved in post_transforms to docutil and sphinx nodes
app.add_node(exercise_end_node)
app.add_node(solution_start_node)
app.add_node(solution_end_node)
app.add_node(exercise_title)
app.add_node(exercise_subtitle)
app.add_node(solution_title)
app.add_node(solution_subtitle)
app.add_node(
exercise_latex_number_reference,
latex=(
visit_exercise_latex_number_reference,
depart_exercise_latex_number_reference,
),
)
app.add_directive("exercise", ExerciseDirective)
app.add_directive("exercise-start", ExerciseStartDirective)
app.add_directive("exercise-end", ExerciseEndDirective)
app.add_directive("solution", SolutionDirective)
app.add_directive("solution-start", SolutionStartDirective)
app.add_directive("solution-end", SolutionEndDirective)
app.add_transform(CheckGatedDirectives)
app.add_transform(MergeGatedExercises)
app.add_transform(MergeGatedSolutions)
app.add_post_transform(UpdateReferencesToEnumerated)
app.add_post_transform(ResolveTitlesInExercises)
app.add_post_transform(ResolveTitlesInSolutions)
app.add_post_transform(ResolveLinkTextToSolutions)
app.add_css_file("exercise.css")
# add translations
package_dir = Path(__file__).parent.resolve()
locale_dir = package_dir / "translations" / "locales"
app.add_message_catalog(MESSAGE_CATALOG_NAME, str(locale_dir))
return {
"version": "builtin",
"parallel_read_safe": True,
"parallel_write_safe": True,
}