Skip to content

Commit 08c469e

Browse files
authored
Merge pull request #37 from executablebooks/refactor-oct2021
MAINT: review and refactor package
2 parents a3cb4f6 + 02d8eff commit 08c469e

File tree

106 files changed

+1307
-913
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1307
-913
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ exclude: >
66
\.vscode/settings\.json|
77
docs/source/conf\.py|
88
tests/test_exercise/.*|
9+
tests/test_exercise_references/.*|
910
tests/test_solution/.*|
11+
tests/test_solution_references/.*|
1012
tests/test_hiddendirective/.*|
1113
)$
1214

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -- Project information -----------------------------------------------------
22

33
project = "sphinx-exercise"
4-
copyright = "2020, QuantEcon Developers"
5-
author = "QuantEcon Developers"
4+
copyright = "2020-2021, Exectuable Book Developers"
5+
author = "Executable Book Developers"
66
master_doc = "index"
77

88

docs/source/developer-design.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Design of the Package
2+
3+
This page documents some design decisions that were made when building this
4+
`sphinx` extension.
5+
6+
`sphinx-exercise` includes:
7+
8+
1. `directives`
9+
2. `nodes`
10+
3. `post_transforms`
11+
4. integrations with `sphinx.env`
12+
13+
## Directives
14+
15+
See [](syntax.md) for futher details on the available directives and options
16+
17+
## Nodes
18+
19+
The nodes support the directives:
20+
21+
1. `exercise_node`
22+
1. `exercise_enumerable_node`
23+
1. `solution_node`
24+
25+
as well as custom title infrastructure:
26+
27+
1. `exercise_title`
28+
1. `exercise_subtitle`
29+
1. `solution_title`
30+
1. `solution_subtitle`
31+
32+
and support for `:numref:` references to enumerated exercise nodes
33+
in the LaTeX context by resolving the node title numbering:
34+
35+
1. `exercise_latex_number_reference`
36+
37+
The `title` and `reference` nodes are used internally by the
38+
`directives` and are then removed in `post_transforms` negating the need
39+
for any custom `translator` methods. The use
40+
of custom nodes allows for simpler detection of objects by this
41+
extension, rather than catering for additional items that may be added by other
42+
sphinx components.
43+
44+
## Post Transforms
45+
46+
The `post_transforms` run in the following order:
47+
48+
1. UpdateReferencesToEnumerated (priority = 5, before `pending_xref` are resolved)
49+
2. ResolveTitlesInExercises (priority = 20)
50+
3. ResolveTitlesInSolutions (priority = 21)
51+
4. ResolveLinkTextToSolutions (priority = 22)
52+
53+
These `post_transforms` are setup to resolve in `Exercise` -> `Solution` -> `References`
54+
ordering. Any `:ref:` made to an enumerated `exercise` node are converted into `numref`
55+
references prior to `pending_xref` objects are resolved by `sphinx`.
56+
57+
This is aligned with most use cases in a document. In the case of `solutions`
58+
if the target node (title) has not been resolved, this is checked and then resolved
59+
as required.
60+
61+
**Design Decision:** It was decided to integrate with `:ref:` and `:numref:` roles
62+
to support both reference styles to `exercise` and `solution` directives.
63+
The `post_transforms` are required to make adjustments the the `sphinx` abstract
64+
syntax tree (AST) to support `:numref:` and the resolve `titles`
65+
in `exercise` and `solution` admonitions. This is required as components of
66+
`numref` are resolved at the `translator` phase for `html` and is activated
67+
essentially by default for LaTeX but leaves the numbering to the `LaTeX`
68+
builder such as `pdflatex`.
69+
70+
## Additional Notes
71+
72+
### Package Registry `sphinx.env.sphinx_exercise_registry`
73+
74+
This package includes a registry of all `exercise` and `solution`
75+
nodes that are parsed.
76+
77+
This registry includes nodes referenced by their `label`:
78+
79+
```python
80+
self.env.sphinx_exercise_registry[label] = {
81+
"type": self.name,
82+
"docname": self.env.docname,
83+
"node": node,
84+
}
85+
```
86+
87+
and records the `type`, `docname` where the node is parsed, and
88+
the `node` object.

docs/source/testing.md renamed to docs/source/developer.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
# Testing
1+
# Developers
2+
3+
Development of this package follows the following conventions and styles.
4+
5+
Design notes and considerations can be found in [](developer-design.md)
6+
7+
## Install
8+
9+
To install the package in `develop` mode:
10+
11+
```shell
12+
cd sphinx-exercise
13+
pip install -e .
14+
```
15+
16+
## Testing
217

318
For code tests, `sphinx-exercise` uses [pytest](https://docs.pytest.org/).
419

docs/source/index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
install
77
syntax
8-
testing
8+
developer
9+
developer-design
910
```
1011

1112

@@ -20,11 +21,11 @@ for producing exercise and solution directives, for html and pdf outputs.
2021

2122
**Features**:
2223

23-
The **exercise** directive is
24+
The **exercise** directive is:
2425

2526
1. automatically numbered
2627
2. supports options such as `class`, `label`, `nonumber`, and `hidden`
27-
3. can easily be referenced through `ref` and `numref` roles
28+
3. can be referenced through `ref` and `numref` roles
2829

2930
The **solution** directive
3031

@@ -37,7 +38,7 @@ The **solution** directive
3738
To get started with `sphinx-exercise`, first install it through `pip`:
3839

3940
```bash
40-
pip install -e.
41+
pip install sphinx-exercise
4142
```
4243

4344
### Jupyter-Book Project
@@ -50,7 +51,7 @@ sphinx:
5051
- sphinx_exercise
5152
```
5253
53-
you may then use `jb build <project>` and the extension will be used by your `JupyterBook` project.
54+
you may then use `jb build <project>` and the extension will be used by your `Jupyter Book` project.
5455

5556
### Sphinx Project
5657

docs/source/syntax.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,26 @@ _Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Ex
5858

5959
### Referencing Exercises
6060

61-
You can refer to an exercise using the `{ref}` role like ```{ref}`my-exercise` ```, which will display the title of the exercise directive. In the event that directive does not have a title, the title will default to "Exercise" like so: {ref}`my-exercise`.
61+
You can refer to an exercise using the `{ref}` role like ```{ref}`my-exercise` ```, which will display the title of the exercise directive. In the event that directive does not have a title, the title will be the default "Exercise" or "Exercise {number}" like so: {ref}`my-exercise`.
6262

63-
Enumerable directives can also be referenced through the `numref` role like ```{numref}`my-exercise` ```, which will display the number of the exercise directive. Referencing the above directive will display {numref}`my-exercise`. Furthermore, `numref` can take in three additional placeholders: _%s_ and _{number}_ which get replaced by the exercise number and _name_ by the exercise title.[^note]
63+
Enumerable directives can also be referenced through the `numref` role like ```{numref}`my-exercise` ```, which will display the number of the exercise directive. Referencing the above directive will display {numref}`my-exercise`. In this case it displays the same result as the `{ref}` role as `exerise` notes are (by default) enumerated.
6464

65+
Furthermore, `numref` can take in three additional placeholders for more customized titles:
6566

67+
1. _%s_
68+
2. _{number}_ which get replaced by the exercise number, and
69+
3. _{name}_ by the exercise title.[^note]
6670

67-
<!-- You can refer to an exercise using the `{ref}` role like: ```{ref}`my-exercise` ```, which will replace the reference with the exercise number like so: {ref}`my-exercise`. When an explicit text is provided, this caption will serve as the title of the reference. -->
71+
An example ```{numref}`My custom {number} title and {name}` ``` would resolve to {numref}`My custom {number} title and {name} <my-exercise>`
6872

6973
[^note]: If the exercise directive does not have a title, an `invalid numfig format` warning will be displayed during build if the user tries to use the _{name}_ placeholder.
7074

7175

7276
## Solution Directive
7377

74-
A solution directive can be included using the `solution` pattern. It takes in the label of the directive it wants to link to as a required argument. Unlike the `exercise` directive, the solution directive is unenumerable. The following options are also supported:
78+
A solution directive can be included using the `solution` pattern. It takes in the label of the directive it wants to link to as a required argument. Unlike the `exercise` directive, the solution directive not enumerable as it inherits directly from the linked exercise.
79+
80+
The following options are also supported:
7581

7682
* `label` : text
7783

@@ -83,9 +89,6 @@ A solution directive can be included using the `solution` pattern. It takes in t
8389

8490
Removes the directive from the final output.
8591

86-
```{note}
87-
The title of the solution directive links directly to the referred directive.
88-
```
8992

9093
**Example**
9194

0 commit comments

Comments
 (0)