Skip to content

Commit 4297046

Browse files
authored
♻️ REFACTOR: key items -> entries (#32)
1 parent b17f955 commit 4297046

28 files changed

+91
-85
lines changed

README.md

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

46-
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 `items` key - denoting a list of children links, that are one of:
46+
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 `entries` key - denoting a list of children links, that are one of:
4747

4848
- `file`: path to a single document file in Unix format, with or without the file extension (as for `root`)
4949
- `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.)
@@ -58,13 +58,13 @@ This can proceed recursively to any depth.
5858
```yaml
5959
root: intro
6060
subtrees:
61-
- items:
61+
- entries:
6262
- file: doc1
6363
subtrees:
64-
- items:
64+
- entries:
6565
- file: doc2
6666
subtrees:
67-
- items:
67+
- entries:
6868
- file: doc3
6969
- url: https://example.com
7070
- glob: subfolder/other*
@@ -73,16 +73,16 @@ subtrees:
7373
This is equivalent to having a single `toctree` directive in `intro`, containing `doc1`,
7474
and a single `toctree` directive in `doc1`, with the `:glob:` flag and containing `doc2`, `https://example.com` and `subfolder/other*`.
7575

76-
As a shorthand, the `items` key can be at the same level as the `file`, which denotes a document with a single subtree.
76+
As a shorthand, the `entries` key can be at the same level as the `file`, which denotes a document with a single subtree.
7777
For example, this file is exactly equivalent to the one above:
7878

7979
```yaml
8080
root: intro
81-
items:
81+
entries:
8282
- file: doc1
83-
items:
83+
entries:
8484
- file: doc2
85-
items:
85+
entries:
8686
- file: doc3
8787
- url: https://example.com
8888
- glob: subfolder/other*
@@ -96,7 +96,7 @@ With the `title` key you can set an alternative title for a document. and also f
9696
```yaml
9797
root: intro
9898
subtrees:
99-
- items:
99+
- entries:
100100
- file: doc1
101101
title: Document 1 Title
102102
- url: https://example.com
@@ -115,7 +115,7 @@ Each subtree can be configured with a number of options (see also [sphinx `toctr
115115
If set to `True`, all sub-trees will also be numbered based on nesting (e.g. with `1.1` or `1.1.1`),
116116
or if set to an integer then the numbering will only be applied to that depth.
117117
- `reversed` (boolean): If `True` then the entries in the subtree will be listed in reverse order (default `False`).
118-
This can be useful when using `glob` items.
118+
This can be useful when using `glob` entries.
119119
- `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`).
120120

121121
These options can be set at the level of the subtree:
@@ -129,11 +129,11 @@ subtrees:
129129
numbered: True
130130
reversed: False
131131
titlesonly: True
132-
items:
132+
entries:
133133
- file: doc1
134134
subtrees:
135135
- titlesonly: True
136-
items:
136+
entries:
137137
- file: doc2
138138
```
139139

@@ -148,11 +148,11 @@ options:
148148
numbered: True
149149
reversed: False
150150
titlesonly: True
151-
items:
151+
entries:
152152
- file: doc1
153153
options:
154154
titlesonly: True
155-
items:
155+
entries:
156156
- file: doc2
157157
```
158158

@@ -168,9 +168,9 @@ options:
168168
maxdepth: 1
169169
numbered: True
170170
reversed: False
171-
items:
171+
entries:
172172
- file: doc1
173-
items:
173+
entries:
174174
- file: doc2
175175
```
176176

@@ -185,18 +185,18 @@ If you want want this numbering to be continuous, check-out the [sphinx-multitoc
185185

186186
### Using different key-mappings
187187

188-
For certain use-cases, it is helpful to map the `subtrees`/`items` keys to mirror e.g. an output [LaTeX structure](https://www.overleaf.com/learn/latex/sections_and_chapters).
188+
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).
189189

190190
The `format` key can be used to provide such mappings (and also initial defaults).
191191
Currently available:
192192

193193
- `jb-article`:
194-
- Maps `items` -> `sections`
194+
- Maps `entries` -> `sections`
195195
- Sets the default of `titlesonly` to `true`
196196
- `jb-book`:
197197
- Maps the top-level `subtrees` to `parts`
198-
- Maps the top-level `items` to `chapters`
199-
- Maps other levels of `items` to `sections`
198+
- Maps the top-level `entries` to `chapters`
199+
- Maps other levels of `entries` to `sections`
200200
- Sets the default of `titlesonly` to `true`
201201

202202
For example:
@@ -206,9 +206,9 @@ defaults:
206206
titlesonly: true
207207
root: index
208208
subtrees:
209-
- items:
209+
- entries:
210210
- file: doc1
211-
items:
211+
entries:
212212
- file: doc2
213213
```
214214

@@ -266,6 +266,10 @@ external_toc_exclude_missing = True
266266
Note that, for performance, files that are in *hidden folders* (e.g. in `.tox` or `.venv`) will not be added to `exclude_patterns` even if they are not specified in the ToC.
267267
You should exclude these folders explicitly.
268268

269+
:::{important}
270+
This feature is not currently compatible with [orphan files](https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html#metadata).
271+
:::
272+
269273
## Command-line
270274

271275
This package comes with the `sphinx-etoc` command-line program, with some additional tools.
@@ -299,7 +303,7 @@ Note, you can also add additional files in `meta`/`create_files` amd append text
299303

300304
```yaml
301305
root: intro
302-
items:
306+
entries:
303307
- glob: doc*
304308
meta:
305309
create_append:
@@ -354,7 +358,7 @@ will create the ToC:
354358
```console
355359
$ sphinx-etoc from-site path/to/folder -i index -s ".*" -e ".rst" -t
356360
root: index
357-
items:
361+
entries:
358362
- file: 1_a_title
359363
title: A title
360364
- file: 11_another_title
@@ -363,20 +367,20 @@ items:
363367
title: A subfolder
364368
- file: 2_another_subfolder/index
365369
title: Another subfolder
366-
items:
370+
entries:
367371
- file: 2_another_subfolder/other
368372
title: Other
369373
- file: 3_subfolder/1_no_index
370374
title: No index
371-
items:
375+
entries:
372376
- file: 3_subfolder/2_no_index
373377
title: No index
374378
- file: 14_subfolder/index
375379
title: Subfolder
376-
items:
380+
entries:
377381
- file: 14_subfolder/subsubfolder/index
378382
title: Subsubfolder
379-
items:
383+
entries:
380384
- file: 14_subfolder/subsubfolder/other
381385
title: Other
382386
```

docs/_toc.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ defaults:
44
subtrees:
55
- caption: Part 1
66
numbered: true
7-
items:
7+
entries:
88
- file: doc1
99
- file: doc2
10-
items:
10+
entries:
1111
- file: subfolder/doc3
1212
- url: https://example.com
1313
title: Example Link
1414
- caption: Part 2
1515
numbered: true
16-
items:
16+
entries:
1717
- glob: subglobs/glob*

docs/toc_tree.pptx

48.6 KB
Binary file not shown.

sphinx_external_toc/parsing.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .api import Document, FileItem, GlobItem, SiteMap, TocTree, UrlItem
1010

1111
DEFAULT_SUBTREES_KEY = "subtrees"
12-
DEFAULT_ITEMS_KEY = "items"
12+
DEFAULT_ITEMS_KEY = "entries"
1313
FILE_FORMAT_KEY = "format"
1414
ROOT_KEY = "root"
1515
FILE_KEY = "file"
@@ -173,7 +173,7 @@ def _parse_doc_item(
173173

174174
if not (isinstance(toc_data, Mapping) and items_key in toc_data):
175175
raise MalformedError(
176-
f"item not a mapping containing '{items_key}' key @ '{toc_path}'"
176+
f"entry not a mapping containing '{items_key}' key @ '{toc_path}'"
177177
)
178178

179179
items_data = toc_data[items_key]
@@ -187,27 +187,27 @@ def _parse_doc_item(
187187

188188
if not isinstance(item_data, Mapping):
189189
raise MalformedError(
190-
f"item not a mapping type @ '{toc_path}{items_key}/{item_idx}'"
190+
f"entry not a mapping type @ '{toc_path}{items_key}/{item_idx}'"
191191
)
192192

193193
link_keys = _known_link_keys.intersection(item_data)
194194

195195
# validation checks
196196
if not link_keys:
197197
raise MalformedError(
198-
f"item does not contain one of "
198+
f"entry does not contain one of "
199199
f"{_known_link_keys!r} @ '{toc_path}{items_key}/{item_idx}'"
200200
)
201201
if not len(link_keys) == 1:
202202
raise MalformedError(
203-
f"item contains incompatible keys "
203+
f"entry contains incompatible keys "
204204
f"{link_keys!r} @ '{toc_path}{items_key}/{item_idx}'"
205205
)
206206
for item_key in (GLOB_KEY, URL_KEY):
207207
for other_key in (subtrees_key, items_key):
208208
if link_keys == {item_key} and other_key in item_data:
209209
raise MalformedError(
210-
f"item contains incompatible keys "
210+
f"entry contains incompatible keys "
211211
f"'{item_key}' and '{other_key}' @ '{toc_path}{items_key}/{item_idx}'"
212212
)
213213

@@ -221,7 +221,7 @@ def _parse_doc_item(
221221
except (ValueError, TypeError) as exc:
222222
exc_arg = exc.args[0] if exc.args else ""
223223
raise MalformedError(
224-
f"item validation @ '{toc_path}{items_key}/{item_idx}': {exc_arg}"
224+
f"entry validation @ '{toc_path}{items_key}/{item_idx}': {exc_arg}"
225225
) from exc
226226

227227
# generate toc key-word arguments
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: intro
22
options:
33
titlesonly: 2
4-
items:
4+
entries:
55
- file: doc1

tests/_bad_toc_files/bad_url.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
root: index
2-
items:
2+
entries:
33
- url: example.com

tests/_bad_toc_files/empty_items.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
root: index
2-
items: []
2+
entries: []
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
root: index
2-
items:
2+
entries:
33
- file: doc
44
glob: doc*
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: index
2-
items:
2+
entries:
33
- glob: doc*
4-
items:
4+
entries:
55
- file: other

tests/_bad_toc_files/items_in_url.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: index
2-
items:
2+
entries:
33
- url: http://example.com
4-
items:
4+
entries:
55
- file: other
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
root: main
22
subtrees:
33
- file: doc1
4-
items:
4+
entries:
55
- file: doc2
66
- file: doc3
77
unknown: 1

tests/_toc_files/basic.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ root: intro
44
subtrees:
55
- caption: Part Caption
66
numbered: true
7-
items:
7+
entries:
88
- file: doc1
99
- file: doc2
1010
- file: doc3
1111
subtrees:
12-
- items:
12+
- entries:
1313
- file: subfolder/doc4
1414
- url: https://example.com
1515
meta:

tests/_toc_files/basic_compressed.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ defaults:
33
options:
44
numbered: true
55
root: intro
6-
items:
6+
entries:
77
- file: doc1
88
- file: doc2
99
- file: doc3
10-
items:
10+
entries:
1111
- file: doc4
1212
- url: https://example.com
1313
meta:

tests/_toc_files/exclude_missing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: intro
2-
items:
2+
entries:
33
- file: doc1
44
- glob: subfolder/other*
55
meta:

tests/_toc_files/glob.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: intro
2-
items:
2+
entries:
33
- glob: doc*
44
meta:
55
create_files:

tests/_toc_files/nested.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
root: intro
2-
items:
2+
entries:
33
- file: folder/doc1
44
- file: folder/doc2
55
- file: folder/doc3
6-
items:
6+
entries:
77
- file: folder/subfolder/doc4
88
- glob: folder/globfolder/*
99
meta:

tests/_toc_files/tableofcontents.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
root: intro
22
subtrees:
3-
- items:
3+
- entries:
44
- file: doc1
5-
- items:
5+
- entries:
66
- file: doc2
77
meta:
88
create_append:

tests/_warning_toc_files/multiple_tableofcontents.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: intro
2-
items:
2+
entries:
33
- file: doc1
44
meta:
55
create_append:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
root: intro
2-
items:
2+
entries:
33
- glob: doc*
44
meta:
55
expected_warning: toctree glob pattern

0 commit comments

Comments
 (0)