|
| 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. |
0 commit comments