Skip to content

Commit b76d0c9

Browse files
committed
Show the new nav generator from mkdocs-gen-files
1 parent e9a3773 commit b76d0c9

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

docs/quickstart/migrate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ But if you make this change by itself, MkDocs will no longer be able to group th
123123
So the navigation now has to be specified fully explicitly. As we've been generating the stub files, let's also generate the nav file itself. The file `docs/gen_doc_stubs.py` will gain these additions:
124124

125125
???+ example "docs/gen_doc_stubs.py"
126-
```python hl_lines="10 15 25-26"
126+
```python hl_lines="10 15 25 28-29"
127127
--8<-- "examples/migrated/docs/gen_doc_stubs_nav.py"
128128
```
129129

examples/migrated/docs/gen_doc_stubs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import mkdocs_gen_files
55

66
# Get the documentation root object
7-
root = mkdocs_gen_files.config['plugins']['mkdocstrings'].get_handler('crystal').collector.root
7+
root = mkdocs_gen_files.config["plugins"]["mkdocstrings"].get_handler("crystal").collector.root
88

99
# For each type (e.g. "Foo::Bar")
1010
for typ in root.walk_types():
1111
# Use the file name "Foo/Bar/index.md"
12-
filename = '/'.join(typ.abs_id.split('::') + ['index.md'])
12+
filename = "/".join(typ.abs_id.split("::") + ["index.md"])
1313
# Make a file with the content "# ::: Foo::Bar\n"
14-
with mkdocs_gen_files.open(filename, 'w') as f:
15-
print(f'# ::: {typ.abs_id}', file=f)
14+
with mkdocs_gen_files.open(filename, "w") as f:
15+
print(f"# ::: {typ.abs_id}", file=f)
1616

1717
# Link to the type itself when clicking the "edit" button on the page.
1818
if typ.locations:

examples/migrated/docs/gen_doc_stubs_nav.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@
44
import mkdocs_gen_files
55

66
# Get the documentation root object
7-
root = mkdocs_gen_files.config['plugins']['mkdocstrings'].get_handler('crystal').collector.root
7+
root = mkdocs_gen_files.config["plugins"]["mkdocstrings"].get_handler("crystal").collector.root
88

9-
# Start a navigation file (to be filled as we go along)
10-
nav = mkdocs_gen_files.open('SUMMARY.md', 'a')
9+
# Start a navigation collection (to be filled as we go along)
10+
nav = mkdocs_gen_files.Nav()
1111

1212
# For each type (e.g. "Foo::Bar")
1313
for typ in root.walk_types():
1414
# Use the file name "Foo/Bar.md"
15-
filename = '/'.join(typ.abs_id.split('::')) + '.md'
15+
filename = "/".join(typ.abs_id.split("::")) + ".md"
1616
# Make a file with the content "# ::: Foo::Bar\n"
17-
with mkdocs_gen_files.open(filename, 'w') as f:
18-
print(f'# ::: {typ.abs_id}', file=f)
17+
with mkdocs_gen_files.open(filename, "w") as f:
18+
print(f"# ::: {typ.abs_id}", file=f)
1919

2020
# Link to the type itself when clicking the "edit" button on the page
2121
if typ.locations:
2222
mkdocs_gen_files.set_edit_path(filename, typ.locations[0].url)
2323

2424
# Append to the nav: " * [Bar](Foo/Bar.md)"
25-
indent = ' ' * typ.abs_id.count('::')
26-
print(indent + f'* [{typ.name}]({filename})', file=nav)
25+
nav[typ.abs_id.split("::")] = filename
26+
27+
# Append the nav to a "literate nav" file
28+
with mkdocs_gen_files.open("SUMMARY.md", "a") as nav_file:
29+
nav_file.writelines(nav.build_literate_nav())

examples/migrated/docs/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ markupsafe==1.1.1
3939
# mkdocstrings-crystal
4040
mkdocs-autorefs==0.1.1
4141
# via mkdocstrings
42-
mkdocs-gen-files==0.3.1
42+
mkdocs-gen-files==0.3.2
4343
# via -r requirements.in
44-
mkdocs-literate-nav==0.3.0
44+
mkdocs-literate-nav==0.3.1
4545
# via -r requirements.in
4646
mkdocs-material-extensions==1.0.1
4747
# via mkdocs-material

0 commit comments

Comments
 (0)