Skip to content

Commit 8e2aaf5

Browse files
authored
Merge pull request #3 from collijk/feature/auto-doc-generation
Feature/auto doc generation
2 parents 95fd6ac + 28a2aca commit 8e2aaf5

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Build and Deploy Docs
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches:
7+
- main
8+
types:
9+
- closed
10+
{% raw %}
11+
jobs:
12+
build-and-deploy-docs:
13+
if: ${{ github.event.pull_request.merged }} or ${{ github.event_name == 'workflow_dispatch' }}
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
token: ${{ secrets.GH_TOKEN }}
19+
- uses: ./.github/actions/python-poetry-env
20+
- name: Deploy docs
21+
run: poetry run mkdocs gh-deploy --force{% endraw %}

{{cookiecutter.project_slug}}/docs/api_docs.md

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

{{cookiecutter.project_slug}}/mkdocs.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ theme:
1414
name: Switch to light mode
1515

1616
nav:
17-
- Introduction: 'index.md'
18-
- api_docs.md
19-
- changelog.md
17+
- Home: 'index.md'
18+
- Code Reference: reference/
19+
- Changelog: changelog.md
2020

2121
watch:
2222
- src/{{ cookiecutter.package_name }}
@@ -61,6 +61,12 @@ markdown_extensions:
6161
plugins:
6262
- table-reader
6363
- search
64+
- gen-files:
65+
scripts:
66+
- scripts/gen_ref_pages.py
67+
- literate-nav:
68+
nav_vile: SUMMARY.md
69+
- section-index
6470
- mkdocstrings:
6571
default_handler: python
6672
handlers:

{{cookiecutter.project_slug}}/pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ click = "*"
3737
mkdocstrings = {version = ">=0.23", extras = ["python"]}
3838
mkdocs-material = "*"
3939
mkdocs-table-reader-plugin = "*"
40+
mkdocs-gen-files = "^0.5.0"
41+
mkdocs-literate-nav = "^0.6.1"
42+
mkdocs-section-index = "^0.3.9"
4043
mypy = "*"
4144
pre-commit = "*"
4245
pymdown-extensions = "*"
@@ -78,6 +81,9 @@ ignore = [
7881
"ARG", # "Unused function argument". Fixtures are often unused.
7982
"S105", # "Possible hardcoded password".
8083
]
84+
"scripts/**" = [
85+
"INP001", # "Scripts are not part of a package."
86+
]
8187

8288
[tool.ruff.lint.mccabe]
8389
max-complexity = 10
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Generate the code reference pages and navigation."""
2+
3+
from pathlib import Path
4+
5+
import mkdocs_gen_files
6+
7+
nav = mkdocs_gen_files.Nav() # type: ignore[attr-defined, no-untyped-call]
8+
9+
root = Path(__file__).parent.parent
10+
src = root / "src"
11+
12+
for path in sorted(src.rglob("*.py")):
13+
module_path = path.relative_to(src).with_suffix("")
14+
doc_path = path.relative_to(src).with_suffix(".md")
15+
full_doc_path = Path("reference", doc_path)
16+
17+
parts = tuple(module_path.parts)
18+
19+
if parts[-1] == "__init__":
20+
parts = parts[:-1]
21+
doc_path = doc_path.with_name("index.md")
22+
full_doc_path = full_doc_path.with_name("index.md")
23+
elif parts[-1] == "__main__":
24+
continue
25+
26+
nav[parts] = doc_path.as_posix()
27+
28+
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
29+
ident = ".".join(parts)
30+
fd.write(f"::: {ident}")
31+
32+
mkdocs_gen_files.set_edit_path(full_doc_path, path.relative_to(root))
33+
34+
with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
35+
nav_file.writelines(nav.build_literate_nav())

0 commit comments

Comments
 (0)