Skip to content

Commit 58ba771

Browse files
authored
♻️ REFACTOR: Rename key: parts -> subtrees (#20)
1 parent 07493ae commit 58ba771

14 files changed

+51
-52
lines changed

README.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The value of the `root` key will be a path to a file, in Unix format (folders sp
3939
This root file will be set as the [`master_doc`](https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-master_doc).
4040
:::
4141

42-
Document files can then have a `parts` key - denoting a list of individual toctrees for that document - and in-turn each part should have a `sections` key - denoting a list of children links, that are one of: `file`, `url` or `glob`:
42+
Document files can then have a `subtrees` key - denoting a list of individual toctrees for that document - and in-turn each subtree should have a `sections` key - denoting a list of children links, that are one of: `file`, `url` or `glob`:
4343

4444
- `file`: path to a single document file in Unix format, with or without the file extension (as for `root`)
4545
- `glob`: path to one or more document files *via* Unix shell-style wildcards (similar to [`fnmatch`](https://docs.python.org/3/library/fnmatch.html), but single stars don't match slashes.)
@@ -53,13 +53,13 @@ This can proceed recursively to any depth.
5353

5454
```yaml
5555
root: intro
56-
parts:
56+
subtrees:
5757
- sections:
5858
- file: doc1
59-
parts:
59+
subtrees:
6060
- sections:
6161
- file: doc2
62-
parts:
62+
subtrees:
6363
- sections:
6464
- file: doc3
6565
- url: https://example.com
@@ -69,7 +69,7 @@ parts:
6969
This is equivalent to having a single `toctree` directive in `intro`, containing `doc1`,
7070
and a single `toctree` directive in `doc1`, with the `:glob:` flag and containing `doc2`, `https://example.com` and `subfolder/other*`.
7171

72-
As a shorthand, the `sections` key can be at the same level as the `file`, which denotes a document with a single `part`.
72+
As a shorthand, the `sections` key can be at the same level as the `file`, which denotes a document with a single subtree.
7373
For example, this file is exactly equivalent to the one above:
7474

7575
```yaml
@@ -91,54 +91,54 @@ With the `title` key you can set an alternative title for a document. and also f
9191

9292
```yaml
9393
root: intro
94-
parts:
94+
subtrees:
9595
- sections:
9696
- file: doc1
9797
title: Document 1 Title
9898
- url: https://example.com
9999
title: Example URL Title
100100
```
101101

102-
### ToC tree (part) options
102+
### ToC tree options
103103

104-
Each part can be configured with a number of options (see also [sphinx `toctree` options](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree)):
104+
Each subtree can be configured with a number of options (see also [sphinx `toctree` options](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-toctree)):
105105

106-
- `caption` (string): A title for the whole the part, e.g. shown above the part in ToCs
106+
- `caption` (string): A title for the whole the subtree, e.g. shown above the subtree in ToCs
107107
- `hidden` (boolean): Whether to show the ToC within (inline of) the document (default `False`).
108108
By default it is appended to the end of the document, but see also the `tableofcontents` directive for positioning of the ToC.
109109
- `maxdepth` (integer): A maximum nesting depth to use when showing the ToC within the document.
110-
- `numbered` (boolean or integer): Automatically add numbers to all documents within a part (default `False`).
111-
If set to `True`, all sub-parts will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
110+
- `numbered` (boolean or integer): Automatically add numbers to all documents within a subtree (default `False`).
111+
If set to `True`, all sub-trees will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
112112
or if set to an integer then the numbering will only be applied to that depth.
113-
- `reversed` (boolean): If `True` then the entries in the part will be listed in reverse order.
113+
- `reversed` (boolean): If `True` then the entries in the subtree will be listed in reverse order.
114114
This can be useful when using `glob` sections.
115115
- `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.
116116

117-
These options can be set at the level of the part:
117+
These options can be set at the level of the subtree:
118118

119119
```yaml
120120
root: intro
121-
parts:
122-
- caption: Part Caption
121+
subtrees:
122+
- caption: Subtree Caption
123123
hidden: False
124124
maxdepth: 1
125125
numbered: True
126126
reversed: False
127127
titlesonly: True
128128
sections:
129129
- file: doc1
130-
parts:
130+
subtrees:
131131
- titlesonly: True
132132
sections:
133133
- file: doc2
134134
```
135135

136-
or, if you are using the shorthand for a single part, set options under an `options` key:
136+
or, if you are using the shorthand for a single subtree, set options under an `options` key:
137137

138138
```yaml
139139
root: intro
140140
options:
141-
caption: Part Caption
141+
caption: Subtree Caption
142142
hidden: False
143143
maxdepth: 1
144144
numbered: True
@@ -152,14 +152,14 @@ sections:
152152
- file: doc2
153153
```
154154

155-
You can also use the top-level `defaults` key, to set default options for all parts:
155+
You can also use the top-level `defaults` key, to set default options for all subtrees:
156156

157157
```yaml
158158
root: intro
159159
defaults:
160160
titlesonly: True
161161
options:
162-
caption: Part Caption
162+
caption: Subtree Caption
163163
hidden: False
164164
maxdepth: 1
165165
numbered: True
@@ -171,17 +171,17 @@ sections:
171171
```
172172

173173
:::{warning}
174-
`numbered` should not generally be used as a default, since numbering cannot be changed by nested parts, and sphinx will log a warning.
174+
`numbered` should not generally be used as a default, since numbering cannot be changed by nested subtrees, and sphinx will log a warning.
175175
:::
176176

177177
:::{note}
178-
By default, section numbering restarts for each `part`.
178+
By default, section numbering restarts for each subtree.
179179
If you want want this numbering to be continuous, check-out the [sphinx-multitoc-numbering extension](https://github.com/executablebooks/sphinx-multitoc-numbering).
180180
:::
181181

182182
## Add a ToC to a page's content
183183

184-
By default, the `toctree` generated per document (one per `part`) are appended to the end of the document and hidden (then, for example, most HTML themes show them in a side-bar).
184+
By default, the `toctree` generated per document (one per subtree) are appended to the end of the document and hidden (then, for example, most HTML themes show them in a side-bar).
185185
But if you would like them to be visible at a certain place within the document body, you may do so by using the `tableofcontents` directive:
186186

187187
ReStructuredText:
@@ -199,7 +199,7 @@ MyST Markdown:
199199

200200
Currently, only one `tableofcontents` should be used per page (all `toctree` will be added here), and only if it is a page with child/descendant documents.
201201

202-
Note, this will override the `hidden` option set for a part.
202+
Note, this will override the `hidden` option set for a subtree.
203203

204204
## Excluding files not in ToC
205205

@@ -343,7 +343,7 @@ documents:
343343
intro:
344344
docname: intro
345345
subtrees:
346-
- caption: Part Caption
346+
- caption: Subtree Caption
347347
numbered: true
348348
reversed: false
349349
items:

docs/_toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
root: intro
22
defaults:
33
titlesonly: false
4-
parts:
4+
subtrees:
55
- caption: Part 1
66
numbered: true
77
sections:

sphinx_external_toc/parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .api import Document, FileItem, GlobItem, SiteMap, TocTree, UrlItem
1010

11-
DEFAULT_SUBTREES_KEY = "parts"
11+
DEFAULT_SUBTREES_KEY = "subtrees"
1212
DEFAULT_ITEMS_KEY = "sections"
1313

1414
FILE_KEY = "file"

sphinx_external_toc/tools.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import yaml
99

1010
from .api import Document, FileItem, SiteMap, TocTree
11-
from .parsing import MalformedError, parse_toc_yaml
11+
from .parsing import DEFAULT_SUBTREES_KEY, MalformedError, parse_toc_yaml
1212

1313

1414
def create_site_from_toc(
@@ -266,7 +266,8 @@ def _strip_suffix(name: str) -> str:
266266

267267

268268
def migrate_jupyter_book(
269-
toc: Union[Path, MutableMapping[str, Any], list]
269+
toc: Union[Path, MutableMapping[str, Any], list],
270+
subtrees_key: str = DEFAULT_SUBTREES_KEY,
270271
) -> MutableMapping[str, Any]:
271272
"""Migrate a jupyter-book v0.10.2 toc."""
272273

@@ -289,10 +290,8 @@ def migrate_jupyter_book(
289290
)
290291
contains_file = any("file" in section for section in first_sections)
291292
if contains_part and contains_file:
292-
raise MalformedError(
293-
"top-level contains mixed parts and individual files"
294-
)
295-
toc_updated["parts" if contains_part else "sections"] = first_sections
293+
raise MalformedError("top-level contains mixed {} and individual files")
294+
toc_updated[subtrees_key if contains_part else "sections"] = first_sections
296295
toc_updated.pop("chapters", None)
297296
toc = toc_updated
298297
elif not isinstance(toc, MutableMapping):
@@ -338,7 +337,7 @@ def migrate_jupyter_book(
338337
numbered = toc.pop("numbered")
339338
if "sections" in toc:
340339
toc["options"] = {"numbered": numbered}
341-
for part in toc.get("parts", []):
340+
for part in toc.get(subtrees_key, []):
342341
if "numbered" not in part:
343342
part["numbered"] = numbered
344343

@@ -347,7 +346,7 @@ def migrate_jupyter_book(
347346
sort_key = {
348347
key: i
349348
for i, key in enumerate(
350-
["root", "title", "defaults", "options", "sections", "parts", "meta"]
349+
["root", "title", "defaults", "options", subtrees_key, "sections", "meta"]
351350
)
352351
}
353352
toc = {key: toc[key] for key in sorted(keys, key=lambda k: sort_key.get(k, 99))}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
root: index
2-
parts:
2+
subtrees:
33
- caption: something
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
root: index
2-
parts: []
2+
subtrees: []

tests/_toc_files/basic.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
defaults:
22
titlesonly: true
33
root: intro
4-
parts:
4+
subtrees:
55
- caption: Part Caption
66
numbered: true
77
sections:
88
- file: doc1
99
- file: doc2
1010
- file: doc3
11-
parts:
11+
subtrees:
1212
- sections:
1313
- file: subfolder/doc4
1414
- url: https://example.com

tests/_toc_files/tableofcontents.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: intro
2-
parts:
2+
subtrees:
33
- sections:
44
- file: doc1
55
- sections:

tests/test_parsing/test_create_toc_dict_tableofcontents_.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ meta:
44
55
'
66
regress: intro
7-
parts:
7+
root: intro
8+
subtrees:
89
- sections:
910
- file: doc1
1011
- sections:
1112
- file: doc2
12-
root: intro

tests/test_tools/test_migrate_jb_chapters_.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defaults:
22
titlesonly: true
3-
parts:
3+
root: index
4+
subtrees:
45
- caption: A section
56
sections:
67
- file: subfolder/index
@@ -25,5 +26,4 @@ parts:
2526
- file: subfolder/asubpage4
2627
title: Asubpage
2728
title: Content3
28-
root: index
2929
title: Toc

0 commit comments

Comments
 (0)