Skip to content

Commit 5664626

Browse files
committed
final code review
1 parent fc158cb commit 5664626

File tree

4 files changed

+34
-40
lines changed

4 files changed

+34
-40
lines changed

sphinx_exercise/__init__.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# -*- coding: utf-8 -*-
22
"""
3-
sphinx_exercise
4-
~~~~~~~~~~~~~~~
5-
This package is a namespace package that contains all extensions
6-
distributed in the ``sphinx-contrib`` distribution.
7-
:copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
8-
:license: BSD, see LICENSE for details.
3+
sphinx_exercise
4+
~~~~~~~~~~~~~~~
5+
This package is an extension for sphinx to support exercise and solutions.
6+
:copyright: Copyright 2020-2021 by the Executable Books team, see AUTHORS.
7+
:license: MIT, see LICENSE for details.
98
"""
9+
1010
from pathlib import Path
1111
from typing import Any, Dict, Set, Union, cast
1212
from sphinx.config import Config
@@ -20,27 +20,19 @@
2020
from .directive import ExerciseDirective, SolutionDirective
2121
from .nodes import (
2222
exercise_node,
23-
exercise_enumerable_node,
24-
solution_node,
2523
visit_exercise_node,
2624
depart_exercise_node,
25+
exercise_enumerable_node,
2726
visit_exercise_enumerable_node,
2827
depart_exercise_enumerable_node,
28+
solution_node,
2929
visit_solution_node,
3030
depart_solution_node,
3131
is_extension_node,
3232
exercise_title,
33-
# visit_exercise_title,
34-
# depart_exercise_title,
3533
exercise_subtitle,
36-
# visit_exercise_subtitle,
37-
# depart_exercise_subtitle,
3834
solution_title,
39-
# visit_solution_title,
40-
# depart_solution_title,
4135
solution_subtitle,
42-
# visit_solution_subtitle,
43-
# depart_solution_subtitle,
4436
exercise_latex_number_reference,
4537
visit_exercise_latex_number_reference,
4638
depart_exercise_latex_number_reference,

sphinx_exercise/nodes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
55
Sphinx Exercise Nodes
66
7-
:copyright: Copyright 2020 by the QuantEcon team, see AUTHORS
7+
:copyright: Copyright 2020-2021 by the Executable Books team, see AUTHORS
88
:licences: see LICENSE for details
99
"""
10+
1011
from sphinx.util import logging
1112
from docutils.nodes import Node
1213
from docutils import nodes as docutil_nodes
@@ -111,7 +112,7 @@ def depart_exercise_node(self, node: Node) -> None:
111112
def visit_exercise_enumerable_node(self, node: Node) -> None:
112113
"""
113114
LaTeX Reference Structure is exercise:{label} and resolved by
114-
exercise_latex_number_reference
115+
exercise_latex_number_reference nodes (see below)
115116
"""
116117
if isinstance(self, LaTeXTranslator):
117118
label = (

sphinx_exercise/post_transforms.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def build_reference_node(app, target_node):
3535

3636
class UpdateReferencesToEnumerated(SphinxPostTransform):
3737
"""
38-
Check :ref: made to enumerated nodes and update to :numref:
38+
Check :ref: made to enumerated target nodes and update to :numref:
3939
"""
4040

4141
default_priority = 5
@@ -71,6 +71,9 @@ def run(self):
7171
class ResolveTitlesInExercises(SphinxPostTransform):
7272
"""
7373
Resolve Titles for Exercise Nodes and Enumerated Exercise Nodes
74+
for:
75+
1. Numbering
76+
2. Formatting Title and Subtitles into docutils.title node
7477
"""
7578

7679
default_priority = 20
@@ -117,10 +120,17 @@ def run(self):
117120

118121
def resolve_solution_title(app, node, exercise_node):
119122
"""
120-
Resolve Solution Nodes
121-
Note: Using a resolver function in case we need to resolve titles
123+
Resolve Titles for Solution Nodes for:
124+
125+
1. Numbering of Target Exercise Nodes
126+
2. Formatting Title and Subtitles into docutils.title node
127+
3. Ensure mathjax is triggered for pages that include path
128+
in titles inherited from Exercise Node
129+
130+
Note: Setup as a resolver function in case we need to resolve titles
122131
in references to solution nodes.
123132
"""
133+
124134
title = node.children[0]
125135
exercise_title = exercise_node.children[0]
126136
if isinstance(title, solution_title):
@@ -155,10 +165,6 @@ def resolve_solution_title(app, node, exercise_node):
155165

156166

157167
class ResolveTitlesInSolutions(SphinxPostTransform):
158-
"""
159-
Resolve Titles for Solutions Nodes and merge in
160-
the main title only from target_nodes
161-
"""
162168

163169
default_priority = 21
164170

@@ -209,19 +215,19 @@ def run(self):
209215
for child in node.children:
210216
new_node += child
211217
node.replace_self(new_node)
212-
if isinstance(target_node, solution_node):
213-
pass
214-
# import pdb; pdb.set_trace()
215218
if isinstance(target_node, solution_node):
219+
# TODO: Check if this condition is required?
220+
if not target_node.resolved_title:
221+
exercise_label = target_node.get("target_label")
222+
exercise_target = self.env.sphinx_exercise_registry[
223+
exercise_label
224+
] # noqa: E501
225+
exercise_node = exercise_target.get("node")
226+
target_node = resolve_solution_title(
227+
self.app, target_node, exercise_node
228+
) # noqa: E501
216229
title_text = target_node.children[0].astext()
217230
inline = node.children[0]
218231
inline.children = []
219232
inline += docutil_nodes.Text(title_text)
220233
node.children[0] = inline
221-
222-
# TODO: Is it possible for the target_node not to be resolved?
223-
# if not target_node.resolved_title:
224-
# exercise_label = target_node.get("target_label")
225-
# exercise_target = self.env.sphinx_exercise_registry[exercise_label] # noqa: E501
226-
# exercise_node = exercise_target.get("node")
227-
# target_node = resolve_solution_title(self.app, target_node, exercise_node) # noqa: E501

sphinx_exercise/utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ def find_parent(env, node, parent_tag):
1818
):
1919
return node.attributes["docname"]
2020

21-
if node.tagname == parent_tag:
22-
return node.attributes["docname"]
23-
24-
return None
25-
2621

2722
def get_node_number(self, node, typ) -> str:
2823
"""Get the number for the directive node for HTML."""

0 commit comments

Comments
 (0)