You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This merge commit introduces section numbering styles and improved numbering control to the sphinx-external-toc extension, making it possible to customize section numbers (numerical, roman, alphabetic, etc.) and restart numbering per subtree. It also integrates the sphinx-multitoc-numbering extension directly, disables the built-in Sphinx toctree collector, and adds support for new configuration options. The documentation and codebase have been updated to reflect these enhancements.
Copy file name to clipboardExpand all lines: README.md
+40-14Lines changed: 40 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
[![PyPI][pypi-badge]][pypi-link]
7
7
8
8
A sphinx extension that allows the documentation site-map (a.k.a Table of Contents) to be defined external to the documentation files.
9
-
As used by [Jupyter Book](https://jupyterbook.org)!
9
+
As used by default by [Jupyter Book](https://jupyterbook.org) (no need to manually add this extension to the extensions in `_config.yml` in a JupyterBook)!
10
10
11
11
In normal Sphinx documentation, the documentation site-map is defined *via* a bottom-up approach - adding [`toctree` directives](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#table-of-contents) within pages of the documentation.
Note the `external_toc_path` is always read as a Unix path, and can either be specified relative to the source directory (recommended) or as an absolute path.
32
33
34
+
### Jupyterbook configuration
35
+
36
+
This extension is included in your jupyterbook configuration by default, so there's need to add it to the list of extensions. The other options can still be added:
Note the `external_toc_path` is always read as a Unix path, and can either be specified relative to the source directory (recommended) or as an absolute path.
45
+
33
46
### Basic Structure
34
47
35
48
A minimal ToC defines the top level `root` key, for a single root document file:
@@ -113,11 +126,27 @@ Each subtree can be configured with a number of options (see also [sphinx `toctr
113
126
By default it is appended to the end of the document, but see also the `tableofcontents` directive for positioning of the ToC.
114
127
- `maxdepth` (integer): A maximum nesting depth to use when showing the ToC within the document (default -1, meaning infinite).
115
128
- `numbered` (boolean or integer): Automatically add numbers to all documents within a subtree (default `False`).
116
-
If set to `True`, all sub-trees will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
117
-
or if set to an integer then the numbering will only be applied to that depth.
129
+
If set to `True`, all subtrees will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
130
+
or if set to an integer then the numbering will only be applied until that depth. Warning: This can lead to unexpected results if not carefully managed, for example references created using `numref` may fail. Internally this options is always converted to an integer, with `True` -> `999` (effectively unlimited depth) and `False` -> `0` (no numbering).
118
131
- `reversed` (boolean): If `True` then the entries in the subtree will be listed in reverse order (default `False`).
119
132
This can be useful when using `glob` entries.
120
133
- `titlesonly` (boolean): If `True` then only the first heading in the document will be shown in the ToC, not other headings of the same level (default `False`).
134
+
- `style` (string or list of strings): The section numbering style to use for this subtree (default `numerical`).
135
+
If a single string is given, this will be used for the top level of the subtree.
136
+
If a list of strings is given, then each entry will be used for the corresponding level of section numbering.
137
+
If styles are not given for all levels, then the remaining levels will be `numerical`.
138
+
If too many styles are given, the extra ones will be ignored.
139
+
The first time a style is used at the top level in a subtree, the numbering will start from 1, 'a', 'A', 'I' or 'i' depending on the style.
140
+
Subsequent times the same style is used at the top level in a subtree, the numbering will continue from the last number used for that style, unless `restart_numbering` is set to `True`.
141
+
Available styles:
142
+
- `numerical`: 1, 2, 3, ...
143
+
- `romanlower`: i, ii, iii, iv, v, ...
144
+
- `romanupper`: I, II, III, IV, V, ...
145
+
- `alphalower`: a, b, c, d, e, ..., aa, ab, ...
146
+
- `alphaupper`: A, B, C, D, E, ..., AA, AB, ...
147
+
- `restart_numbering` (boolean): If `True`, the numbering for the top level of this subtree will restart from 1 (or 'a', 'A', 'I' or 'i' depending on the style). If `False` the numbering for the top level of this subtree will continue from the last letter/number/symbol used in a previous subtree with the same style. The default value of this option is `not use_multitoc_numbering`. This means that:
148
+
- if `use_multitoc_numbering` is `True` (the default), the numbering for each part will continue from the last letter/number/symbol used in a previous part with the same style, unless `restart_numbering` is explicitly set to `True`.
149
+
- if `use_multitoc_numbering` is `False`, the numbering of each subtree will restart from 1 (or 'a', 'A', 'I' or 'i' depending on the style), unless `restart_numbering` is explicitly set to `False`.
121
150
122
151
These options can be set at the level of the subtree:
123
152
@@ -130,6 +159,8 @@ subtrees:
130
159
numbered: True
131
160
reversed: False
132
161
titlesonly: True
162
+
style: [alphaupper, romanlower]
163
+
restart_numbering: True
133
164
entries:
134
165
- file: doc1
135
166
subtrees:
@@ -149,6 +180,8 @@ options:
149
180
numbered: True
150
181
reversed: False
151
182
titlesonly: True
183
+
style: [alphaupper, romanlower]
184
+
restart_numbering: True
152
185
entries:
153
186
- file: doc1
154
187
options:
@@ -169,21 +202,14 @@ options:
169
202
maxdepth: 1
170
203
numbered: True
171
204
reversed: False
205
+
style: [alphaupper, romanlower]
206
+
restart_numbering: True
172
207
entries:
173
208
- file: doc1
174
209
entries:
175
210
- file: doc2
176
211
```
177
212
178
-
:::{warning}
179
-
`numbered`should not generally be used as a default, since numbering cannot be changed by nested subtrees, and sphinx will log a warning.
180
-
:::
181
-
182
-
:::{note}
183
-
By default, title numbering restarts for each subtree.
184
-
If you want want this numbering to be continuous, check-out the [sphinx-multitoc-numbering extension](https://github.com/executablebooks/sphinx-multitoc-numbering).
185
-
:::
186
-
187
213
### Using different key-mappings
188
214
189
215
For certain use-cases, it is helpful to map the `subtrees`/`entries` keys to mirror e.g. an output [LaTeX structure](https://www.overleaf.com/learn/latex/sections_and_chapters).
@@ -424,13 +450,13 @@ meta: {}
424
450
425
451
Questions / TODOs:
426
452
427
-
- Add additional top-level keys, e.g. `appendices` (see https://github.com/sphinx-doc/sphinx/issues/2502) and `bibliography`
453
+
- ~~Add additional top-level keys, e.g. `appendices` (see https://github.com/sphinx-doc/sphinx/issues/2502) and `bibliography`.~~ Can be replaced by setting the numbering style and (possibly) restarting the numbering.
428
454
- Using `external_toc_exclude_missing` to exclude a certain file suffix:
429
455
currently if you had files `doc.md` and `doc.rst`, and put `doc.md` in your ToC,
430
456
it will add `doc.rst` to the excluded patterns but then, when looking for `doc.md`,
431
457
will still select `doc.rst` (since it is first in `source_suffix`).
432
458
Maybe open an issue on sphinx, that `doc2path` should respect exclude patterns.
433
-
- Integrate https://github.com/executablebooks/sphinx-multitoc-numbering into this extension? (or upstream PR)
459
+
- ~~Integrate https://github.com/executablebooks/sphinx-multitoc-numbering into this extension? (or upstream PR).~~ Included and enforced in this fork.
@@ -95,11 +96,27 @@ Each subtree can be configured with a number of options (see also [sphinx `toctr
95
96
By default it is appended to the end of the document, but see also the `tableofcontents` directive for positioning of the ToC.
96
97
- `maxdepth` (integer): A maximum nesting depth to use when showing the ToC within the document (default -1, meaning infinite).
97
98
- `numbered` (boolean or integer): Automatically add numbers to all documents within a subtree (default `False`).
98
-
If set to `True`, all sub-trees will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
99
+
If set to `True`, all subtrees will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
99
100
or if set to an integer then the numbering will only be applied to that depth.
100
101
- `reversed` (boolean): If `True` then the entries in the subtree will be listed in reverse order (default `False`).
101
102
This can be useful when using `glob` entries.
102
103
- `titlesonly` (boolean): If `True` then only the first heading in the document will be shown in the ToC, not other headings of the same level (default `False`).
104
+
- `style` (string or list of strings): The section numbering style to use for this subtree (default `numerical`).
105
+
If a single string is given, this will be used for the top level of the subtree.
106
+
If a list of strings is given, then each entry will be used for the corresponding level of section numbering.
107
+
If styles are not given for all levels, then the remaining levels will be `numerical`.
108
+
If too many styles are given, the extra ones will be ignored.
109
+
The first time a style is used at the top level in a subtree, the numbering will start from 1, 'a', 'A', 'I' or 'i' depending on the style.
110
+
Subsequent times the same style is used at the top level in a subtree, the numbering will continue from the last number used for that style, unless `restart_numbering` is set to `True`.
111
+
Available styles:
112
+
- `numerical`: 1, 2, 3, ...
113
+
- `romanlower`: i, ii, iii, iv, v, ...
114
+
- `romanupper`: I, II, III, IV, V, ...
115
+
- `alphalower`: a, b, c, d, e, ..., aa, ab, ...
116
+
- `alphaupper`: A, B, C, D, E, ..., AA, AB, ...
117
+
- `restart_numbering` (boolean): If `True`, the numbering for the top level of this subtree will restart from 1 (or 'a', 'A', 'I' or 'i' depending on the style). If `False` the numbering for the top level of this subtree will continue from the last letter/number/symbol used in a previous subtree with the same style. The default value of this option is `not use_multitoc_numbering`. This means that:
118
+
- if `use_multitoc_numbering` is `True` (the default), the numbering for each part will continue from the last letter/number/symbol used in a previous part with the same style, unless `restart_numbering` is explicitly set to `True`.
119
+
- if `use_multitoc_numbering` is `False`, the numbering of each subtree will restart from 1 (or 'a', 'A', 'I' or 'i' depending on the style), unless `restart_numbering` is explicitly set to `False`.
103
120
104
121
These options can be set at the level of the subtree:
105
122
@@ -112,6 +129,8 @@ subtrees:
112
129
numbered: True
113
130
reversed: False
114
131
titlesonly: True
132
+
style: [alphaupper, romanlower]
133
+
restart_numbering: True
115
134
entries:
116
135
- file: doc1
117
136
subtrees:
@@ -131,6 +150,8 @@ options:
131
150
numbered: True
132
151
reversed: False
133
152
titlesonly: True
153
+
style: [alphaupper, romanlower]
154
+
restart_numbering: True
134
155
entries:
135
156
- file: doc1
136
157
options:
@@ -151,21 +172,14 @@ options:
151
172
maxdepth: 1
152
173
numbered: True
153
174
reversed: False
175
+
style: [alphaupper, romanlower]
176
+
restart_numbering: True
154
177
entries:
155
178
- file: doc1
156
179
entries:
157
180
- file: doc2
158
181
```
159
182
160
-
:::{warning}
161
-
`numbered`should not generally be used as a default, since numbering cannot be changed by nested subtrees, and sphinx will log a warning.
162
-
:::
163
-
164
-
:::{note}
165
-
By default, title numbering restarts for each subtree.
166
-
If you want want this numbering to be continuous, check-out the [sphinx-multitoc-numbering extension](https://github.com/executablebooks/sphinx-multitoc-numbering).
167
-
:::
168
-
169
183
## Using different key-mappings
170
184
171
185
For certain use-cases, it is helpful to map the `subtrees`/`entries` keys to mirror e.g. an output [LaTeX structure](https://www.overleaf.com/learn/latex/sections_and_chapters).
0 commit comments