Skip to content

Commit f00220b

Browse files
authored
♻️ REFACTOR: doc key -> file (#7)
1 parent 83efd7f commit f00220b

File tree

7 files changed

+83
-89
lines changed

7 files changed

+83
-89
lines changed

README.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,50 @@ external_toc_path = "_toc.yml" # optional
2525

2626
### Basic Structure
2727

28-
A minimal ToC defines the top level `main` key, and a single root document page:
28+
A minimal ToC defines the top level `main` key, and a single root document file:
2929

3030
```yaml
3131
main:
32-
doc: intro
32+
file: intro
3333
```
3434
35-
The value of the `doc` key will be a path to a file (relative to the `conf.py`) with or without the file extension.
35+
The value of the `file` key will be a path to a file (relative to the `conf.py`) with or without the file extension.
3636

3737
```{important}
38-
Each document can only occur once in the ToC!
38+
Each document file can only occur once in the ToC!
3939
```
4040

41-
Document pages 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: `doc`, `url` or `glob`:
41+
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`:
4242

43-
- `doc`: relating to a single document page (as above)
44-
- `glob`: relating to one or more document pages *via* Unix shell-style wildcards (similar to [`fnmatch`](https://docs.python.org/3/library/fnmatch.html), but single stars don't match slashes.)
45-
- `url`: relating to an external URL (`http` or `https`)
43+
- `file`: relating to a single document file
44+
- `glob`: relating 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.)
45+
- `url`: relating to an external URL (e.g. `http` or `https`)
4646

4747
This can proceed recursively to any depth.
4848

4949
```yaml
5050
main:
51-
doc: intro
51+
file: intro
5252
parts:
5353
- sections:
54-
- doc: doc1
54+
- file: doc1
5555
parts:
5656
- sections:
57-
- doc: doc2
57+
- file: doc2
5858
- url: https://example.com
5959
- glob: other*
6060
```
6161

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

6565
```yaml
6666
main:
67-
doc: intro
67+
file: intro
6868
sections:
69-
- doc: doc1
69+
- file: doc1
7070
sections:
71-
- doc: doc2
71+
- file: doc2
7272
- url: https://example.com
7373
- glob: other*
7474
```
@@ -81,12 +81,12 @@ Each part can also have a `caption`, e.g. for use in ToC side-bars:
8181

8282
```yaml
8383
main:
84-
doc: intro
84+
file: intro
8585
title: Introduction
8686
parts:
8787
- caption: Part Caption
8888
sections:
89-
- doc: doc1
89+
- file: doc1
9090
- url: https://example.com
9191
title: Example Site
9292
```
@@ -97,12 +97,12 @@ You can automatically add numbers to all docs with a part by adding the `numbere
9797

9898
```yaml
9999
main:
100-
doc: intro
100+
file: intro
101101
parts:
102102
- numbered: true
103103
sections:
104-
- doc: doc1
105-
- doc: doc2
104+
- file: doc1
105+
- file: doc2
106106
```
107107

108108
You can also **limit the TOC numbering depth** by setting the `numbered` flag to an integer instead of `true`, e.g., `numbered: 3`.
@@ -115,16 +115,46 @@ To have e.g. `numbered` added to all toctrees, set it in under a `defaults` top-
115115
defaults:
116116
numbered: true
117117
main:
118-
doc: intro
118+
file: intro
119119
sections:
120-
- doc: doc1
120+
- file: doc1
121121
sections:
122-
- doc: doc2
122+
- file: doc2
123123
- url: https://example.com
124124
```
125125

126126
Available keys: `numbered`, `titlesonly`, `reversed`
127127

128+
## Command-line
129+
130+
This package comes with the `sphinx-etoc` command-line program, with some additional tools.
131+
132+
To see all options:
133+
134+
```console
135+
$ sphinx-etoc --help
136+
```
137+
138+
To build a template site from only a ToC file:
139+
140+
```console
141+
$ sphinx-etoc create-site -p path/to/site -e rst path/to/_toc.yml
142+
```
143+
144+
Note, when using `glob` you can also add additional files in `meta`/`create_additional`, e.g.
145+
146+
```yaml
147+
main:
148+
file: intro
149+
sections:
150+
- glob: doc*
151+
meta:
152+
create_additional:
153+
- doc1
154+
- doc2
155+
- doc3
156+
```
157+
128158
## API
129159

130160
The ToC file is parsed to a `SiteMap`, which is a `MutableMapping` subclass, with keys representing docnames mapping to a `DocItem` that stores information on the toctrees it should contain:
@@ -157,36 +187,6 @@ intro:
157187
title: Introduction
158188
```
159189

160-
## Command-line
161-
162-
This package comes with the `sphinx-etoc` command-line program, with some additional tools.
163-
164-
To see all options:
165-
166-
```console
167-
$ sphinx-etoc --help
168-
```
169-
170-
To build a template site from only a ToC file:
171-
172-
```console
173-
$ sphinx-etoc create-site -p path/to/site -e rst path/to/_toc.yml
174-
```
175-
176-
Note, when using `glob` you can also add additional files in `meta`/`create_additional`, e.g.
177-
178-
```yaml
179-
main:
180-
doc: intro
181-
sections:
182-
- glob: doc*
183-
meta:
184-
create_additional:
185-
- doc1
186-
- doc2
187-
- doc3
188-
```
189-
190190
## Development Notes
191191

192192
Want to have a built-in CLI including commands:

codecov.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ coverage:
22
status:
33
project:
44
default:
5-
target: 80%
5+
target: 82%
66
threshold: 0.2%
77
patch:
88
default:
9-
target: 80%
9+
target: 75%
1010
threshold: 0.2%

docs/_toc.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
defaults:
2-
# numbered: true
2+
numbered: 3
33
titlesonly: false
44
main:
5-
doc: intro
5+
file: intro
66
title: Introduction
77
sections:
8-
- doc: doc1
9-
- doc: doc2
10-
# sections:
11-
# - doc: subfolder/doc3
12-
# - url: https://example.com
13-
# title: Example Link
14-
parts:
15-
- numbered: true
16-
sections:
17-
- doc: subfolder/doc3
18-
- url: https://example.com
19-
title: Example Link
8+
- file: doc1
9+
- file: doc2
10+
sections:
11+
- file: subfolder/doc3
12+
- url: https://example.com
13+
title: Example Link

sphinx_external_toc/api.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ def _parse_doc_item(
154154
data: Dict[str, Any], defaults: Dict[str, Any], path: str
155155
) -> Tuple[DocItem, Sequence[Dict[str, Any]]]:
156156
"""Parse a single doc item."""
157-
if "doc" not in data:
158-
raise MalformedError(f"'doc' key not found: '{path}'")
157+
if "file" not in data:
158+
raise MalformedError(f"'file' key not found: '{path}'")
159159
if "sections" in data:
160160
# this is a shorthand for defining a single part
161161
if "parts" in data:
@@ -167,7 +167,7 @@ def _parse_doc_item(
167167
if not isinstance(parts_data, Sequence):
168168
raise MalformedError(f"'parts' not a sequence: '{path}'")
169169

170-
_known_link_keys = {"url", "doc", "glob"}
170+
_known_link_keys = {"file", "glob", "url"}
171171

172172
parts = []
173173
for part_idx, part in enumerate(parts_data):
@@ -186,8 +186,8 @@ def _parse_doc_item(
186186
"toctree section contains incompatible keys "
187187
f"{link_keys!r}: {path}{part_idx}/{sect_idx}"
188188
)
189-
if link_keys == {"doc"}:
190-
sections.append(RefItem(section["doc"]))
189+
if link_keys == {"file"}:
190+
sections.append(RefItem(section["file"]))
191191
elif link_keys == {"glob"}:
192192
sections.append(GlobItem(section["glob"]))
193193
elif link_keys == {"url"}:
@@ -214,15 +214,15 @@ def _parse_doc_item(
214214
parts.append(toc_item)
215215

216216
try:
217-
doc_item = DocItem(docname=data["doc"], title=data.get("title"), parts=parts)
217+
doc_item = DocItem(docname=data["file"], title=data.get("title"), parts=parts)
218218
except TypeError as exc:
219219
raise MalformedError(f"doc validation: {path}") from exc
220220

221221
docs_data = [
222222
section
223223
for part in parts_data
224224
for section in part["sections"]
225-
if "doc" in section
225+
if "file" in section
226226
]
227227

228228
return (
@@ -239,9 +239,9 @@ def _parse_docs_list(
239239
):
240240
"""Parse a list of docs."""
241241
for doc_data in docs_list:
242-
docname = doc_data["doc"]
242+
docname = doc_data["file"]
243243
if docname in site_map:
244-
raise MalformedError(f"document used multiple times: {docname}")
244+
raise MalformedError(f"document file used multiple times: {docname}")
245245
child_path = f"{path}{docname}/"
246246
child_item, child_docs_list = _parse_doc_item(doc_data, defaults, child_path)
247247
site_map[docname] = child_item

tests/_toc_files/basic.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
defaults:
22
numbered: true
33
main:
4-
doc: intro
4+
file: intro
55
title: Introduction
66
parts:
77
- caption: Part Caption
88
sections:
9-
- doc: doc1
10-
- doc: doc2
11-
- doc: doc3
9+
- file: doc1
10+
- file: doc2
11+
- file: doc3
1212
parts:
1313
- sections:
14-
- doc: doc4
14+
- file: doc4
1515
- url: https://example.com
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
defaults:
22
numbered: true
33
main:
4-
doc: intro
4+
file: intro
55
title: Introduction
66
sections:
7-
- doc: doc1
8-
- doc: doc2
9-
- doc: doc3
7+
- file: doc1
8+
- file: doc2
9+
- file: doc3
1010
sections:
11-
- doc: doc4
11+
- file: doc4
1212
- url: https://example.com

tests/_toc_files/glob.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
main:
2-
doc: intro
2+
file: intro
33
title: Introduction
44
sections:
55
- glob: doc*

0 commit comments

Comments
 (0)