Files
doc-forge/docforge/renderers/mkdocs.py
Vishesh 'ironeagle' Bangotra dca19caaf3 fix: make MkDocs generation filesystem-complete and package-aware
- Add filesystem-based module discovery via `discover_module_paths`
- Decouple documentation coverage from Python import behavior
- Ensure GriffeLoader receives a full module list instead of a single root
- Make MkDocs renderer level-agnostic using global package detection
- Emit `index.md` only for true packages, suppress `<package>.md`
- Mirror full dotted module hierarchy into nested docs directories
- Update CLI, exports, and type stubs to expose discovery helper
- Align tests with filesystem-driven module coverage

This fixes missing docs for submodules and removes invalid package `.md` files.
2026-01-20 23:25:56 +05:30

59 lines
1.7 KiB
Python

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:
modules = list(project.get_all_modules())
paths = {m.path for m in modules}
# Package detection (level-agnostic)
packages = {
p for p in paths
if any(other.startswith(p + ".") for other in paths)
}
for module in modules:
self._write_module(module, packages, out_dir)
# -------------------------
# Internal helpers
# -------------------------
def _write_module(self, module, packages: set[str], out_dir: Path) -> None:
parts = module.path.split(".")
if module.path in packages:
# package → index.md
dir_path = out_dir.joinpath(*parts)
dir_path.mkdir(parents=True, exist_ok=True)
md_path = dir_path / "index.md"
title = parts[-1].replace("_", " ").title()
else:
# leaf module → <name>.md
dir_path = out_dir.joinpath(*parts[:-1])
dir_path.mkdir(parents=True, exist_ok=True)
md_path = dir_path / f"{parts[-1]}.md"
title = parts[-1].replace("_", " ").title()
content = self._render_markdown(title, module.path)
if md_path.exists() and 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"
)