rendered MkDocs
This commit is contained in:
58
docforge/renderers/mkdocs.py
Normal file
58
docforge/renderers/mkdocs.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from docforge.model import Project
|
||||
|
||||
|
||||
class MkDocsRenderer:
|
||||
"""MkDocs source generator using mkdocstrings."""
|
||||
|
||||
name = "mkdocs"
|
||||
|
||||
def generate_sources(self, project: Project, out_dir: Path) -> None:
|
||||
"""
|
||||
Generate Markdown files with mkdocstrings directives.
|
||||
|
||||
Structure rules:
|
||||
- Each top-level package gets a directory
|
||||
- Modules become .md files
|
||||
- Packages (__init__) become index.md
|
||||
"""
|
||||
for module in project.get_all_modules():
|
||||
self._write_module(project, module, out_dir)
|
||||
|
||||
# -------------------------
|
||||
# Internal helpers
|
||||
# -------------------------
|
||||
|
||||
def _write_module(self, project: Project, module, out_dir: Path) -> None:
|
||||
parts = module.path.split(".")
|
||||
|
||||
# Root package directory
|
||||
pkg_dir = out_dir / parts[0]
|
||||
pkg_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Package (__init__.py) → index.md
|
||||
if module.path == parts[0]:
|
||||
md_path = pkg_dir / "index.md"
|
||||
title = parts[0].replace("_", " ").title()
|
||||
else:
|
||||
# Submodule → <name>.md
|
||||
md_path = pkg_dir / f"{parts[-1]}.md"
|
||||
title = parts[-1].replace("_", " ").title()
|
||||
|
||||
content = self._render_markdown(title, module.path)
|
||||
|
||||
# Idempotent write
|
||||
if md_path.exists():
|
||||
if md_path.read_text(encoding="utf-8") == content:
|
||||
return
|
||||
|
||||
md_path.write_text(content, encoding="utf-8")
|
||||
|
||||
def _render_markdown(self, title: str, module_path: str) -> str:
|
||||
return (
|
||||
f"# {title}\n\n"
|
||||
f"::: {module_path}\n"
|
||||
)
|
||||
Reference in New Issue
Block a user