Skip to content

Commit 766be15

Browse files
committed
📚 DOCS: update docs
1 parent 2351c6a commit 766be15

File tree

3 files changed

+214
-11
lines changed

3 files changed

+214
-11
lines changed

docs/source/index.md

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

1011
[![Documentation Status](https://readthedocs.org/projects/sphinx-exercise/badge/?version=latest)](https://sphinx-exercise.readthedocs.io/en/latest/?badge=latest)
@@ -15,43 +16,50 @@ This package contains a [Sphinx](http://www.sphinx-doc.org/en/master/) extension
1516
for producing exercise and solution directives.
1617

1718
```{warning}
18-
sphinx-exercise `0.0.1` is in a development stage and may change rapidly.
19+
sphinx-exercise `0.1.0` is in a development stage and may change rapidly.
1920
```
2021

2122
**Features**:
2223

23-
1. directives are automatically numbered
24-
2. supports directive options such as `class`, `label`, and `nonumber`
25-
3. can easily be referenced through `ref` role
24+
The **exercise** directive is
25+
26+
1. automatically numbered
27+
2. supports options such as `class`, `label`, and `nonumber`
28+
3. can easily be referenced through `ref` and `numref` roles
29+
30+
The **solution** directive
31+
32+
1. supports options such as `class` and `label`
33+
2. can be referenced through `ref` role
2634

2735
(getting-started)=
2836
## Getting Started
2937

3038
To get started with `sphinx-exercise`, first install it through `pip`:
3139

3240
```bash
33-
pip install sphinx-exercise
41+
pip install -e.
3442
```
3543

3644
### JuputerBook Project
3745

38-
Add `sphinx.exercise` to your [extra_extensions](https://jupyterbook.org/advanced/sphinx.html#custom-sphinx-extensions) config in `_config.yml`
46+
Add `sphinx_exercise` to your [extra_extensions](https://jupyterbook.org/advanced/sphinx.html#custom-sphinx-extensions) config in `_config.yml`
3947

4048
```yaml
4149
sphinx:
4250
extra_extensions:
43-
- sphinx.exercise
51+
- sphinx_exercise
4452
```
4553
4654
you may then use `jb build <project>` and the extension will be used by your `JupyterBook` project.
4755

4856
### Sphinx Project
4957

50-
Add `sphinx.exercise` to your sphinx `extensions` in the `conf.py`
58+
Add `sphinx_exercise` to your sphinx `extensions` in the `conf.py`
5159

5260
```python
5361
...
54-
extensions = ["sphinx.exercise"]
62+
extensions = ["sphinx_exercise"]
5563
...
5664
```
5765

docs/source/syntax.md

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# Syntax Guide
22

3+
34
```{note}
45
This documentation utilized the [Markedly Structured Text (MyST)](https://myst-parser.readthedocs.io/en/latest/index.html) syntax.
56
```
67

7-
## Exercises
8+
## Exercise Directive
89

910
An exercise directive can be included using the `exercise` pattern. The directive is enumerated by default and can take in an optional title argument. The following options are also supported:
1011

1112
* `label` : text
1213

13-
A unique identifier for your exercise that you can use to reference it with `{ref}`. Cannot contain spaces or special characters.
14+
A unique identifier for your exercise that you can use to reference it with `{ref}` and `{numref}`. Cannot contain spaces or special characters.
1415
* `class` : text
1516

1617
Value of the exercise’s class attribute which can be used to add custom CSS or JavaScript.
@@ -51,3 +52,133 @@ for any positive integer $n$.
5152
``````
5253

5354
_Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Exercise-1)
55+
56+
### Referencing Exercises
57+
58+
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`.
59+
60+
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]
61+
62+
63+
64+
<!-- 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. -->
65+
66+
[^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.
67+
68+
69+
## Solution Directive
70+
71+
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:
72+
73+
* `label` : text
74+
75+
A unique identifier for your solution that you can use to reference it with `{ref}`. Cannot contain spaces or special characters.
76+
* `class` : text
77+
78+
Value of the solution’s class attribute which can be used to add custom CSS or JavaScript.
79+
80+
```{note}
81+
The title of the solution directive links directly to the referred directive.
82+
```
83+
84+
**Example**
85+
86+
````{solution} my-exercise
87+
:label: my-solution
88+
89+
Here's one solution.
90+
91+
```{code-block} python
92+
def factorial(n):
93+
k = 1
94+
for i in range(n):
95+
k = k * (i + 1)
96+
return k
97+
98+
factorial(4)
99+
```
100+
````
101+
102+
**MyST Syntax**
103+
104+
``````md
105+
````{solution} my-exercise
106+
:label: my-solution
107+
108+
Here's one solution.
109+
110+
```{code-block} python
111+
def factorial(n):
112+
k = 1
113+
for i in range(n):
114+
k = k * (i + 1)
115+
return k
116+
117+
factorial(4)
118+
```
119+
````
120+
``````
121+
122+
_Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Exercise-1)
123+
124+
125+
### Referencing Solutions
126+
127+
You can refer to a solution using the `{ref}` role like: ```{ref}`my-solution` ``` the output of which depends on the attributes of the linked directive. If the linked directive is enumerable, the role will replace the solution reference with the linked directive type and its appropriate number like so: {ref}`my-solution`.
128+
129+
In the event that the directive being referenced is unenumerable, the reference will display its title: {ref}`nfactorial-solution`. Click the toggle to see the supporting directives.
130+
131+
````{toggle}
132+
133+
```{exercise} $n!$ Factorial
134+
:label: nfactorial
135+
:nonumber:
136+
137+
Write a function `factorial` such that `factorial(int n)` returns $n!$
138+
for any positive integer $n$.
139+
```
140+
141+
```{solution} nfactorial
142+
:label: nfactorial-solution
143+
144+
Here's a solution in Java.
145+
146+
```{code-block} java
147+
static int factorial(int n){
148+
if (n == 0)
149+
return 1;
150+
else {
151+
return(n * factorial(n-1));
152+
}
153+
}
154+
```
155+
````
156+
157+
158+
If the title of the linked directive being reference does not exist, it will default to {ref}`nfactorial-notitle-solution`. Click the toggle to see the supporting directives.
159+
160+
````{toggle}
161+
162+
```{exercise}
163+
:label: nfactorial-notitle
164+
:nonumber:
165+
166+
Write a function `factorial` such that `factorial(int n)` returns $n!$
167+
for any positive integer $n$.
168+
```
169+
170+
```{solution} nfactorial-notitle
171+
:label: nfactorial-notitle-solution
172+
173+
Here's a solution in Java.
174+
175+
```{code-block} java
176+
static int factorial(int n){
177+
if (n == 0)
178+
return 1;
179+
else {
180+
return(n * factorial(n-1));
181+
}
182+
}
183+
```
184+
````

docs/source/testing.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Testing
2+
3+
For code tests, `sphinx-exercise` uses [pytest](https://docs.pytest.org/).
4+
5+
Run the tests with the following command:
6+
7+
```shell
8+
>> cd sphinx-exercise
9+
>> pip install -e .[testing]
10+
>> pytest
11+
```
12+
13+
To run the tests in multiple isolated environments, you can also run [tox](https://tox.readthedocs.io/)
14+
15+
```shell
16+
>> cd sphinx-exercise
17+
>> tox
18+
```
19+
20+
To test the build of documentation run
21+
22+
```shell
23+
>> cd sphinx-exercise
24+
>> tox docs-update
25+
```
26+
27+
or
28+
29+
```shell
30+
>> cd sphinx-exercise/docs
31+
>> make clean
32+
>> make html
33+
```
34+
35+
## Unit Testing
36+
37+
We use [pytest](https://docs.pytest.org/en/latest/) for testing, [pytest-regression](https://pytest-regressions.readthedocs.io/en/latest/) to regenerate expected outcomes of test and [pytest-cov](https://pytest-cov.readthedocs.io/en/latest/) for checking coverage.
38+
39+
To run tests with coverage and an html coverage report:
40+
41+
```bash
42+
pytest -v --cov=sphinx_exercise --cov-report=html
43+
```
44+
45+
## Writing Tests
46+
47+
The module `sphinx.testing` is used to run sphinx builds for tests, in a temporary directory.
48+
49+
If creating a new source folder for test files, folder name should start with `test-`.
50+
Your folder should reside inside the `tests/books` directory, which has been set as the root directory for tests.
51+
52+
The tests should start with:
53+
54+
```python
55+
@pytest.mark.sphinx('html', testroot="mybook")
56+
```
57+
In the above declaration, `html` builder is used. And `mybook` is the source folder which was created with the name `test-mybook` inside `tests/books` folder.
58+
59+
Sphinx Application API is available as a parameter to all the test functions:
60+
61+
```python
62+
@pytest.mark.sphinx('html', testroot="mybook")
63+
def mytest(app):
64+
```

0 commit comments

Comments
 (0)