Skip to content

Commit 527db17

Browse files
docs: add automatic code reference generation (#285)
* docs: add automatic code reference generation * fix: fix workflow with new mkdocs plugins --------- Co-authored-by: pommedeterresautee <[email protected]>
1 parent 481a7b5 commit 527db17

File tree

8 files changed

+48
-16
lines changed

8 files changed

+48
-16
lines changed

.github/workflows/deploy-static-site.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
path: .cache
3939
- run: sudo apt-get install libcairo2-dev libfreetype6-dev libffi-dev libjpeg-dev libpng-dev libz-dev
4040
- run: sudo apt-get install pngquant
41-
- run: pip install git+https://${{secrets.GH_TOKEN}}@github.com/squidfunk/[email protected] "mkdocstrings[python]"
41+
- run: pip install git+https://${{secrets.GH_TOKEN}}@github.com/squidfunk/[email protected] "mkdocstrings[python]" mkdocs-gen-files mkdocs-literate-nav mkdocs-section-index
4242
- run: pip install mkdocs-glightbox
4343
- run: pip install pillow cairosvg
4444
- run: mkdocs build

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ COPY ./setup.py ./setup.py
4040
COPY ./setup.cfg ./setup.cfg
4141
COPY ./requirements.txt ./requirements.txt
4242
COPY ./README.md ./README.md
43-
COPY ./src/__init__.py ./src/__init__.py
4443
COPY ./src/kernl/__init__.py ./src/kernl/__init__.py
4544

4645

docs/Dockerfile.mkdocstrings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ RUN pip install --no-cache-dir \
66

77
# mkdocstrings plugin
88
RUN pip install --no-cache-dir \
9-
pip install mkdocstrings[python]
9+
pip install mkdocstrings[python] mkdocs-gen-files mkdocs-literate-nav mkdocs-section-index

docs/gen_ref_pages.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Generate the code reference pages and navigation."""
2+
3+
import os
4+
import sys
5+
from pathlib import Path
6+
7+
import mkdocs_gen_files
8+
9+
10+
# Add the kernl module to python path
11+
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
12+
13+
nav = mkdocs_gen_files.Nav()
14+
15+
for path in sorted(Path("src").rglob("*.py")):
16+
module_path = path.relative_to("src").with_suffix("")
17+
doc_path = path.relative_to("src").with_suffix(".md")
18+
full_doc_path = Path("reference", doc_path)
19+
20+
parts = tuple(module_path.parts)
21+
22+
if parts[-1] == "__init__":
23+
parts = parts[:-1]
24+
doc_path = doc_path.with_name("index.md")
25+
full_doc_path = full_doc_path.with_name("index.md")
26+
elif parts[-1] == "__main__":
27+
continue
28+
29+
nav[parts] = doc_path.as_posix()
30+
31+
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
32+
ident = ".".join(parts)
33+
fd.write(f"::: {ident}")
34+
35+
mkdocs_gen_files.set_edit_path(full_doc_path, path)
36+
37+
with mkdocs_gen_files.open("reference/SUMMARY.txt", "w") as nav_file:
38+
nav_file.writelines(nav.build_literate_nav())

docs/reference/extended_matcher.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/reference/model_optimization.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

mkdocs.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ markdown_extensions:
139139
# Plugins
140140
plugins:
141141
- search
142+
- gen-files:
143+
scripts:
144+
- docs/gen_ref_pages.py
145+
- literate-nav:
146+
nav_file: SUMMARY.txt
142147
- typeset # preserves html formatting in the navigation and t.o.c
143148
- glightbox # image zoom functionality plugin
144149

@@ -160,6 +165,7 @@ plugins:
160165
python:
161166
options:
162167
show_source: false
168+
163169
watch:
164170
- src/kernl
165171

@@ -171,9 +177,8 @@ nav:
171177
- How to support a new model: how-to-guides/support-new-model.md
172178
# - Tutorials:
173179
# - Page 1: tutorials/page.md
174-
- Code Reference:
175-
- Model Optimization: reference/model_optimization.md
176-
- Graph Pattern Matching: reference/extended_matcher.md
180+
# defer to gen-files + literate-nav
181+
- Code Reference: reference/
177182
- Contribution guide:
178183
- Contributing: contribution-guide/contributing.md
179184
- Code of conduct: contribution-guide/code-of-conduct.md

src/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)