This commit is contained in:
@@ -1,3 +1,22 @@
|
||||
"""
|
||||
# Renderers Layer
|
||||
|
||||
The `docforge.renderers` package handles the transformation of the internal
|
||||
documentation model into physical files formatted for specific documentation
|
||||
engines.
|
||||
|
||||
## Current Implementations
|
||||
|
||||
- **MkDocsRenderer**: Generates Markdown files utilizing the `mkdocstrings`
|
||||
syntax. It automatically handles package/module hierarchy and generates
|
||||
`index.md` files for packages.
|
||||
|
||||
## Extending
|
||||
|
||||
To add a new renderer, implement the `DocRenderer` protocol defined in
|
||||
`docforge.renderers.base`.
|
||||
"""
|
||||
|
||||
from .mkdocs import MkDocsRenderer
|
||||
|
||||
__all__ = [
|
||||
|
||||
@@ -1,11 +1,46 @@
|
||||
"""
|
||||
This module defines the base interfaces and configuration containers for
|
||||
doc-forge renderers. All renderer implementations should adhere to the
|
||||
DocRenderer protocol.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Protocol
|
||||
|
||||
from docforge.model import Project
|
||||
|
||||
|
||||
class RendererConfig:
|
||||
"""Renderer configuration container."""
|
||||
"""
|
||||
Configuration container for documentation renderers.
|
||||
|
||||
Args:
|
||||
out_dir: The directory where documentation files should be written.
|
||||
project: The introspected project model to be rendered.
|
||||
"""
|
||||
|
||||
def __init__(self, out_dir: Path, project: Project) -> None:
|
||||
self.out_dir = out_dir
|
||||
self.project = project
|
||||
|
||||
|
||||
class DocRenderer(Protocol):
|
||||
"""
|
||||
Protocol defining the interface for documentation renderers.
|
||||
"""
|
||||
|
||||
name: str
|
||||
|
||||
def generate_sources(
|
||||
self,
|
||||
project: Project,
|
||||
out_dir: Path,
|
||||
) -> None:
|
||||
"""
|
||||
Generate renderer-specific source files for the given project.
|
||||
|
||||
Args:
|
||||
project: The project model containing modules and objects.
|
||||
out_dir: Target directory for the generated files.
|
||||
"""
|
||||
...
|
||||
|
||||
@@ -1,14 +1,30 @@
|
||||
"""
|
||||
This module implements the MkDocsRenderer, which generates Markdown source files
|
||||
compatible with the MkDocs 'material' theme and 'mkdocstrings' extension.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from docforge.model import Project
|
||||
|
||||
|
||||
class MkDocsRenderer:
|
||||
"""MkDocs source generator using mkdocstrings."""
|
||||
"""
|
||||
Renderer that generates Markdown source files formatted for the MkDocs
|
||||
'mkdocstrings' plugin.
|
||||
"""
|
||||
|
||||
name = "mkdocs"
|
||||
|
||||
def generate_sources(self, project: Project, out_dir: Path) -> None:
|
||||
"""
|
||||
Produce a set of Markdown files in the output directory based on the
|
||||
provided Project model.
|
||||
|
||||
Args:
|
||||
project: The project model to render.
|
||||
out_dir: Target directory for documentation files.
|
||||
"""
|
||||
modules = list(project.get_all_modules())
|
||||
paths = {m.path for m in modules}
|
||||
|
||||
@@ -25,6 +41,15 @@ class MkDocsRenderer:
|
||||
# Internal helpers
|
||||
# -------------------------
|
||||
def _write_module(self, module, packages: set[str], out_dir: Path) -> None:
|
||||
"""
|
||||
Write a single module's documentation file. Packages are written as
|
||||
'index.md' inside their respective directories.
|
||||
|
||||
Args:
|
||||
module: The module to write.
|
||||
packages: A set of module paths that are identified as packages.
|
||||
out_dir: The base output directory.
|
||||
"""
|
||||
parts = module.path.split(".")
|
||||
|
||||
if module.path in packages:
|
||||
@@ -50,6 +75,16 @@ class MkDocsRenderer:
|
||||
md_path.write_text(content, encoding="utf-8")
|
||||
|
||||
def _render_markdown(self, title: str, module_path: str) -> str:
|
||||
"""
|
||||
Generate the Markdown content for a module file.
|
||||
|
||||
Args:
|
||||
title: The display title for the page.
|
||||
module_path: The dotted path of the module to document.
|
||||
|
||||
Returns:
|
||||
A string containing the Markdown source.
|
||||
"""
|
||||
return (
|
||||
f"# {title}\n\n"
|
||||
f"::: {module_path}\n"
|
||||
|
||||
Reference in New Issue
Block a user