Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: review and refactor package #37

Merged
merged 34 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4914e33
MAINT: rework directives into exercise and solution directives
mmcky Oct 28, 2021
5ac852e
simplify and reduce repeated code
mmcky Oct 28, 2021
4ddd56f
split post_transforms and review
mmcky Oct 29, 2021
f176cb7
introduce LaTeX markup object
mmcky Oct 29, 2021
dc66d0d
reorg nodes.py
mmcky Oct 29, 2021
ca24235
remove functions not required
mmcky Oct 29, 2021
cc835a7
odds and end updates commit before references refactor
mmcky Oct 29, 2021
a3338bf
REF: rework reference post-transforms, in work
mmcky Nov 1, 2021
6fbdeff
Merge branch 'master' into refactor-oct2021
mmcky Nov 1, 2021
8e70340
TST: Updating test solutions for test_exercise
mmcky Nov 2, 2021
5b91146
REFACTOR: update exercise nodes, directives, and post_transforms with…
mmcky Nov 16, 2021
945a322
rework references resolution
mmcky Nov 18, 2021
1eaa9b8
rework references resolution
mmcky Nov 18, 2021
962de98
update fixtures for final review
mmcky Nov 19, 2021
84a3d2a
fixes for updating references
mmcky Nov 22, 2021
ebf67f5
retain parent data when reseting title nodes in exercise and solution…
mmcky Nov 24, 2021
56a96c5
latex styling for exercise and solution titles
AakashGfude Nov 24, 2021
087c967
update LaTeX fixtures
mmcky Nov 24, 2021
1665d17
[rework] Reworked references (round 2) to simplify
mmcky Nov 25, 2021
62a94b2
Reorganise tests
mmcky Nov 26, 2021
16d4ae0
style update for custom titles, ensure mathjax loading if math in tit…
mmcky Nov 29, 2021
8d0d969
adding in empty title for enumerated exercise cells for LaTeX
mmcky Nov 29, 2021
1daa7a7
resolve exercise titles for enumerated nodes when targeting latex
mmcky Nov 29, 2021
20b26d9
Fix LaTeX issue with reference resolution
mmcky Nov 30, 2021
3b9635f
[LaTeX] fix reference resolution for solutions
mmcky Nov 30, 2021
b6520fe
tidy up code and remove unecessary methods
mmcky Nov 30, 2021
cd044bb
docs review
mmcky Nov 30, 2021
a41e286
minor update to docs
mmcky Nov 30, 2021
fc158cb
removing unused functions and unused latex handling
AakashGfude Dec 1, 2021
5664626
final code review
mmcky Dec 5, 2021
f6907ab
update docs with developer notes
mmcky Dec 6, 2021
e3686be
change to EBP developers
mmcky Dec 6, 2021
94fa543
additional final edits of docstrings
mmcky Dec 6, 2021
02d8eff
FIX: fix solution node titles are links to exercise node
mmcky Dec 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ exclude: >
\.vscode/settings\.json|
docs/source/conf\.py|
tests/test_exercise/.*|
tests/test_exercise_references/.*|
tests/test_solution/.*|
tests/test_solution_references/.*|
tests/test_hiddendirective/.*|
)$

Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -- Project information -----------------------------------------------------

project = "sphinx-exercise"
copyright = "2020, QuantEcon Developers"
author = "QuantEcon Developers"
copyright = "2020-2021, Exectuable Book Developers"
author = "Executable Book Developers"
master_doc = "index"


Expand Down
88 changes: 88 additions & 0 deletions docs/source/developer-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Design of the Package

This page documents some design decisions that were made when building this
`sphinx` extension.

`sphinx-exercise` includes:

1. `directives`
2. `nodes`
3. `post_transforms`
4. integrations with `sphinx.env`

## Directives

See [](syntax.md) for futher details on the available directives and options

## Nodes

The nodes support the directives:

1. `exercise_node`
1. `exercise_enumerable_node`
1. `solution_node`

as well as custom title infrastructure:

1. `exercise_title`
1. `exercise_subtitle`
1. `solution_title`
1. `solution_subtitle`

and support for `:numref:` references to enumerated exercise nodes
in the LaTeX context by resolving the node title numbering:

1. `exercise_latex_number_reference`

The `title` and `reference` nodes are used internally by the
`directives` and are then removed in `post_transforms` negating the need
for any custom `translator` methods. The use
of custom nodes allows for simpler detection of objects by this
extension, rather than catering for additional items that may be added by other
sphinx components.

## Post Transforms

The `post_transforms` run in the following order:

1. UpdateReferencesToEnumerated (priority = 5, before `pending_xref` are resolved)
2. ResolveTitlesInExercises (priority = 20)
3. ResolveTitlesInSolutions (priority = 21)
4. ResolveLinkTextToSolutions (priority = 22)

These `post_transforms` are setup to resolve in `Exercise` -> `Solution` -> `References`
ordering. Any `:ref:` made to an enumerated `exercise` node are converted into `numref`
references prior to `pending_xref` objects are resolved by `sphinx`.

This is aligned with most use cases in a document. In the case of `solutions`
if the target node (title) has not been resolved, this is checked and then resolved
as required.

**Design Decision:** It was decided to integrate with `:ref:` and `:numref:` roles
to support both reference styles to `exercise` and `solution` directives.
The `post_transforms` are required to make adjustments the the `sphinx` abstract
syntax tree (AST) to support `:numref:` and the resolve `titles`
in `exercise` and `solution` admonitions. This is required as components of
`numref` are resolved at the `translator` phase for `html` and is activated
essentially by default for LaTeX but leaves the numbering to the `LaTeX`
builder such as `pdflatex`.

## Additional Notes

### Package Registry `sphinx.env.sphinx_exercise_registry`

This package includes a registry of all `exercise` and `solution`
nodes that are parsed.

This registry includes nodes referenced by their `label`:

```python
self.env.sphinx_exercise_registry[label] = {
"type": self.name,
"docname": self.env.docname,
"node": node,
}
```

and records the `type`, `docname` where the node is parsed, and
the `node` object.
17 changes: 16 additions & 1 deletion docs/source/testing.md → docs/source/developer.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# Testing
# Developers

Development of this package follows the following conventions and styles.

Design notes and considerations can be found in [](developer-design.md)

## Install

To install the package in `develop` mode:

```shell
cd sphinx-exercise
pip install -e .
```

## Testing

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

Expand Down
11 changes: 6 additions & 5 deletions docs/source/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

install
syntax
testing
developer
developer-design
```


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

**Features**:

The **exercise** directive is
The **exercise** directive is:

1. automatically numbered
2. supports options such as `class`, `label`, `nonumber`, and `hidden`
3. can easily be referenced through `ref` and `numref` roles
3. can be referenced through `ref` and `numref` roles

The **solution** directive

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

```bash
pip install -e.
pip install sphinx-exercise
```

### Jupyter-Book Project
Expand All @@ -50,7 +51,7 @@ sphinx:
- sphinx_exercise
```

you may then use `jb build <project>` and the extension will be used by your `JupyterBook` project.
you may then use `jb build <project>` and the extension will be used by your `Jupyter Book` project.

### Sphinx Project

Expand Down
17 changes: 10 additions & 7 deletions docs/source/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ _Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Ex

### Referencing Exercises

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`.
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`.

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]
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.

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

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

<!-- 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. -->
An example ```{numref}`My custom {number} title and {name}` ``` would resolve to {numref}`My custom {number} title and {name} <my-exercise>`

[^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.


## Solution Directive

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:
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.

The following options are also supported:

* `label` : text

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

Removes the directive from the final output.

```{note}
The title of the solution directive links directly to the referred directive.
```

**Example**

Expand Down
Loading