Skip to content

Commit 124bc33

Browse files
authored
Merge pull request #12 from febus982/autogenerate_docstrings
Autogenerate docstrings
2 parents 76d7a79 + 5ebbad2 commit 124bc33

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ jobs:
4141
- name: Export version for site docs
4242
id: docs-version-step
4343
run: |
44-
./ci-scripts/docs-version.sh
45-
echo "Identified version: $(./ci-scripts/docs-version.sh)"
46-
echo "version=$(./ci-scripts/docs-version.sh)"
47-
echo "version=$(./ci-scripts/docs-version.sh)" >> $GITHUB_OUTPUT
44+
./scripts/docs-version.sh
45+
echo "Identified version: $(./scripts/docs-version.sh)"
46+
echo "version=$(./scripts/docs-version.sh)"
47+
echo "version=$(./scripts/docs-version.sh)" >> $GITHUB_OUTPUT
4848
4949
publish:
5050
runs-on: ubuntu-latest

bootstrap_python_package/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
def some_function() -> str:
2+
"""
3+
Some function docstring
4+
5+
:return: Some string
6+
:rtype: str
7+
"""
28
return "some_variable_to_test"
39

410

mkdocs.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ repo_url: 'https://github.com/febus982/bootstrap-python-package'
1010
plugins:
1111
- search
1212
- mike
13+
- gen-files:
14+
scripts:
15+
- scripts/gen_pages.py # or any other name or path
16+
- mkdocstrings:
17+
handlers:
18+
python:
19+
options:
20+
docstring_style: sphinx
21+
docstring_section_style: spacy
1322

1423
theme:
1524
name: material
@@ -44,9 +53,6 @@ extra:
4453
provider: mike
4554
default: stable
4655

47-
nav:
48-
- Home: index.md
49-
5056
markdown_extensions:
5157
- pymdownx.details
5258
- pymdownx.blocks.admonition

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ coverage = ">=6.5.0"
4545
bandit = ">=1.7.6"
4646
black = ">=22.10.0"
4747
mkdocs = ">=1.4.3"
48+
mkdocstrings = { version = ">=0.24.0", extras = ["python"] }
4849
mkdocs-material = ">=9.1.16"
4950
mike = ">=2.0.0"
5051
mypy = ">=0.990"
@@ -55,6 +56,7 @@ pytest-cov = ">=4.0.0"
5556
pytest-factoryboy = ">=2.5.0"
5657
pytest-xdist = ">=3.0.2"
5758
ruff = ">=0.0.263"
59+
mkdocs-gen-files = "^0.5.0"
5860

5961
[tool.pytest.ini_options]
6062
asyncio_mode = "auto"
File renamed without changes.

scripts/gen_pages.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -----------------------------------------------------#
2+
# Library imports #
3+
# -----------------------------------------------------#
4+
from pathlib import Path
5+
6+
import mkdocs_gen_files
7+
8+
# -----------------------------------------------------#
9+
# Configuration #
10+
# -----------------------------------------------------#
11+
# Package source code relative path
12+
src_dir = "bootstrap_python_package"
13+
# Generated pages will be grouped in this nav folder
14+
nav_pages_path = "API-Reference"
15+
16+
17+
# -----------------------------------------------------#
18+
# Runner #
19+
# -----------------------------------------------------#
20+
""" Generate code reference pages and navigation
21+
22+
Based on the recipe of mkdocstrings:
23+
https://github.com/mkdocstrings/mkdocstrings
24+
https://github.com/mkdocstrings/mkdocstrings/issues/389#issuecomment-1100735216
25+
26+
Credits:
27+
Timothée Mazzucotelli
28+
https://github.com/pawamoy
29+
"""
30+
# Iterate over each Python file
31+
for path in sorted(Path(src_dir).rglob("*.py")):
32+
# Get path in module, documentation and absolute
33+
module_path = path.relative_to(src_dir).with_suffix("")
34+
doc_path = path.relative_to(src_dir).with_suffix(".md")
35+
full_doc_path = Path(nav_pages_path, doc_path)
36+
37+
# Handle edge cases
38+
parts = (src_dir,) + tuple(module_path.parts)
39+
if parts[-1] == "__init__":
40+
parts = parts[:-1]
41+
doc_path = doc_path.with_name("index.md")
42+
full_doc_path = full_doc_path.with_name("index.md")
43+
elif parts[-1] == "__main__":
44+
continue
45+
46+
# Write docstring documentation to disk via parser
47+
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
48+
ident = ".".join(parts)
49+
fd.write(f"::: {ident}")
50+
# Update parser
51+
mkdocs_gen_files.set_edit_path(full_doc_path, path)

0 commit comments

Comments
 (0)