Skip to content

Commit 90f16b9

Browse files
chore: update mkdocs nav + add tasks.py script for changing mkdocs config file
1 parent ad9f5cd commit 90f16b9

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ nav:
66
- 2. Defining Tables: 2_defining_tables.md
77
- 3. Building Queries: 3_building_queries.md
88
- 4. Relationships: 4_relationships.md
9-
- 5. py4web: 5_py4web.md
9+
- 5. py4web and web2py: 5_py4web.md
1010
- 6. Migrations: 6_migrations.md
1111
- 7. Configuration: 7_configuration.md
1212
- 8. Mixins: 8_mixins.md
13+
- 9. Function Memoization: 9_memoization.md
1314
extra:
1415
version:
1516
default: stable

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ all = [
6363
dev = [
6464
# build:
6565
"hatch",
66+
"ewok",
6667
# test:
6768
"su6[all]>=1.9.0",
6869
"python-semantic-release < 8",

tasks.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Task automation using ewok (invoke-compatible)."""
2+
3+
import re
4+
from pathlib import Path
5+
6+
from ewok import Context, task
7+
8+
# Compiled regex pattern for replacing the nav section in mkdocs.yml
9+
NAV_SECTION_PATTERN = re.compile(r"nav:.*?(?=\n[a-z_]+:|$)", re.DOTALL)
10+
11+
12+
def extract_title(md_file: Path) -> str:
13+
"""Extract the title from a markdown file's first heading."""
14+
first_line = md_file.read_text(encoding="utf-8").split("\n", 1)[0].strip()
15+
# Remove the leading # and any extra whitespace
16+
return first_line.lstrip("#").strip()
17+
18+
19+
def generate_nav_entries() -> list[str]:
20+
"""Generate nav entries from numbered markdown files in docs/."""
21+
docs_dir = Path(__file__).parent / "docs"
22+
23+
# Find all numbered chapter files (supports 1-N digits)
24+
chapters = [f for f in docs_dir.glob("*_*.md") if f.stem.split("_", 1)[0].isdigit()]
25+
26+
return [
27+
f" - {extract_title(chapter)}: {chapter.name}"
28+
for chapter in sorted(
29+
chapters,
30+
key=lambda f: int(f.stem.split("_", 1)[0]),
31+
)
32+
]
33+
34+
35+
@task
36+
def update_docs_nav(ctx: Context) -> None:
37+
"""Update mkdocs.yml nav section from actual markdown files to prevent sync issues."""
38+
mkdocs_file = Path(__file__).parent / "mkdocs.yml"
39+
40+
content = mkdocs_file.read_text(encoding="utf-8")
41+
42+
# Generate new nav entries
43+
nav_entries = generate_nav_entries()
44+
new_nav_section = "nav:\n" + "\n".join(nav_entries)
45+
46+
# Replace the nav section
47+
updated_content = NAV_SECTION_PATTERN.sub(new_nav_section, content)
48+
49+
mkdocs_file.write_text(updated_content, encoding="utf-8")
50+
51+
print(f"✓ Updated mkdocs.yml with {len(nav_entries)} chapters")
52+
print("\nGenerated nav:")
53+
for entry in nav_entries:
54+
print(entry)

0 commit comments

Comments
 (0)