chore(release): 0.0.7
Squash-merge lib-standardisation: adopt repo standards across packaging, tooling, docs, and CI. - standardize pyproject.toml (canonical packaging, lint config, extras, GSDFC pydoclint) - add py.typed marker, modernize type hints and Google-style docstrings - nest generated docs under docs/lib and docs/mcp; add hand-written wiki - adopt standard Type A PyPI CI pipeline (tag==version guard + quality gate) - promote generated CHANGELOG.md; bump version to 0.0.7
This commit is contained in:
@@ -22,29 +22,49 @@ pip install doc-forge
|
||||
|
||||
# CLI usage
|
||||
|
||||
## Generate an MkDocs site from a Python package:
|
||||
Each site kind (`lib`, `api`, `wiki`) is built independently into `site/{kind}`.
|
||||
|
||||
## Build the library reference from a Python package:
|
||||
|
||||
```bash
|
||||
doc-forge build --mkdocs --module my_package
|
||||
```
|
||||
|
||||
## Build the API reference from an OpenAPI spec:
|
||||
|
||||
```bash
|
||||
doc-forge build --api --openapi-spec spec.json
|
||||
```
|
||||
|
||||
## Build the hand-written wiki:
|
||||
|
||||
```bash
|
||||
doc-forge build --wiki --site-name my_package
|
||||
```
|
||||
|
||||
## Generate MCP JSON documentation:
|
||||
|
||||
```bash
|
||||
doc-forge build --mcp --module my_package
|
||||
```
|
||||
|
||||
|
||||
## Generate MkDocs site and MCP JSON documentation:
|
||||
## Build several kinds in one pass:
|
||||
|
||||
```bash
|
||||
doc-forge build --mcp --mkdocs --module my_package
|
||||
doc-forge build --mcp --mkdocs --wiki --module my_package
|
||||
```
|
||||
|
||||
## Serve MkDocs locally:
|
||||
Each enabled kind gets its own MkDocs config (`docs/mkdocs.{lib,api,wiki}.yml`)
|
||||
and its own site under `site/`.
|
||||
|
||||
## Serve a site locally:
|
||||
|
||||
```bash
|
||||
doc-forge serve --mkdocs --module my_package
|
||||
doc-forge serve --wiki # preview from docs/mkdocs.wiki.yml
|
||||
doc-forge serve --lib
|
||||
doc-forge serve --api
|
||||
# or any config directly:
|
||||
doc-forge serve --mkdocs --mkdocs-yml docs/mkdocs.wiki.yml
|
||||
```
|
||||
|
||||
## Serve MCP locally:
|
||||
@@ -129,7 +149,9 @@ GSDFC defines how docstrings must be written so they render correctly in MkDocs
|
||||
|
||||
- Use **Markdown headings** at package and module level.
|
||||
- Use **Google-style structured sections** at class, function, and method level.
|
||||
- Use type hints in signatures instead of duplicating types in prose.
|
||||
- Use type hints in signatures.
|
||||
- Use parenthesized types in prose entries (`name (Type):`) that match the
|
||||
signature types. This keeps docstrings self-contained and machine-parseable.
|
||||
- Write summaries in imperative form.
|
||||
- Sections are separated by `---`
|
||||
|
||||
@@ -189,7 +211,10 @@ Example:
|
||||
engine.run()
|
||||
```
|
||||
|
||||
Avoid fenced code blocks inside structured sections.
|
||||
Avoid fenced code blocks inside argument descriptions and other prose lines.
|
||||
|
||||
Inside `Example:` sections, fenced `python` code blocks are allowed and must be
|
||||
indented four spaces, matching the examples below.
|
||||
|
||||
---
|
||||
|
||||
@@ -521,6 +546,20 @@ Example:
|
||||
|
||||
---
|
||||
|
||||
# Type parity (`.pyi` stubs and `py.typed`)
|
||||
|
||||
Documented APIs ship matching type information:
|
||||
|
||||
- Each `.py` module has a synchronized `.pyi` stub in the same package.
|
||||
- Packages expose a `py.typed` marker so type checkers (and consumers)
|
||||
use the authored signatures instead of `Any`.
|
||||
- When signatures change, update the `.py` implementation and its `.pyi`
|
||||
stub together.
|
||||
- Doc-forge documents the docstrings in `.py`; the `.pyi` stub is the
|
||||
machine-consumable signature surface.
|
||||
|
||||
---
|
||||
|
||||
# Parsing guarantees
|
||||
|
||||
GSDFC ensures doc-forge can deterministically extract:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .loaders import GriffeLoader, discover_module_paths
|
||||
from .renderers import MkDocsRenderer, MCPRenderer
|
||||
from .cli import main
|
||||
from . import models
|
||||
from .cli import main
|
||||
from .loaders import GriffeLoader, discover_module_paths
|
||||
from .renderers import MCPRenderer, MkDocsRenderer
|
||||
|
||||
__all__ = [
|
||||
"GriffeLoader",
|
||||
|
||||
@@ -37,6 +37,4 @@ Example:
|
||||
|
||||
from .main import main
|
||||
|
||||
__all__ = [
|
||||
"main"
|
||||
]
|
||||
__all__ = ["main"]
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from .main import main
|
||||
|
||||
__all__ = [
|
||||
"main"
|
||||
]
|
||||
__all__ = ["main"]
|
||||
|
||||
118
docforge/cli/api_utils.py
Normal file
118
docforge/cli/api_utils.py
Normal file
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
# Summary
|
||||
|
||||
Utilities for building API documentation from an OpenAPI specification.
|
||||
"""
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
|
||||
SWAGGER_SPEC_FILENAME = "openapi.json"
|
||||
|
||||
|
||||
@dataclass
|
||||
class OpenAPIMetadata:
|
||||
"""
|
||||
Metadata derived from the ``info`` block of an OpenAPI specification.
|
||||
|
||||
Attributes:
|
||||
site_name: Spec title, used as the MkDocs site name.
|
||||
site_description: Spec description, used as the site description.
|
||||
site_author: Contact name (fallback: contact email), used as the
|
||||
site author.
|
||||
"""
|
||||
|
||||
site_name: str
|
||||
site_description: str | None
|
||||
site_author: str | None
|
||||
|
||||
|
||||
def load_openapi_spec(spec_path: Path) -> dict:
|
||||
"""
|
||||
Load and validate an OpenAPI specification from a JSON file.
|
||||
|
||||
Args:
|
||||
spec_path (Path):
|
||||
Path to the OpenAPI JSON specification file.
|
||||
|
||||
Returns:
|
||||
dict:
|
||||
The parsed OpenAPI specification.
|
||||
|
||||
Raises:
|
||||
click.ClickException:
|
||||
If the file cannot be read or the ``info`` block is invalid.
|
||||
"""
|
||||
if not spec_path.exists():
|
||||
raise click.ClickException(f"OpenAPI spec not found: {spec_path}")
|
||||
|
||||
try:
|
||||
data = json.loads(spec_path.read_text(encoding="utf-8"))
|
||||
except (json.JSONDecodeError, OSError) as exc:
|
||||
raise click.ClickException(f"Unable to read OpenAPI spec: {exc}") from exc
|
||||
|
||||
if not isinstance(data, dict):
|
||||
raise click.ClickException("OpenAPI spec must be a JSON mapping")
|
||||
|
||||
info = data.get("info")
|
||||
if not isinstance(info, dict) or not isinstance(info.get("title"), str):
|
||||
raise click.ClickException("OpenAPI spec missing 'info.title'")
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def derive_metadata(spec: dict) -> OpenAPIMetadata:
|
||||
"""
|
||||
Derive MkDocs site metadata from an OpenAPI spec ``info`` block.
|
||||
|
||||
Args:
|
||||
spec (dict):
|
||||
Parsed OpenAPI specification.
|
||||
|
||||
Returns:
|
||||
OpenAPIMetadata:
|
||||
Site name, description, and author derived from the spec.
|
||||
"""
|
||||
info = spec["info"]
|
||||
|
||||
contact = info.get("contact")
|
||||
if isinstance(contact, dict):
|
||||
author = contact.get("name") or contact.get("email")
|
||||
else:
|
||||
author = None
|
||||
|
||||
return OpenAPIMetadata(
|
||||
site_name=info.get("title", ""),
|
||||
site_description=info.get("description"),
|
||||
site_author=author,
|
||||
)
|
||||
|
||||
|
||||
def generate_api_sources(spec: dict, docs_dir: Path) -> None:
|
||||
"""
|
||||
Generate swagger-enabled Markdown sources and the spec copy.
|
||||
|
||||
The specification is written as ``openapi.json`` inside ``docs_dir`` and
|
||||
an ``index.md`` embedding the swagger UI is generated alongside it.
|
||||
|
||||
Args:
|
||||
spec (dict):
|
||||
Parsed OpenAPI specification.
|
||||
docs_dir (Path):
|
||||
Directory (for example ``docs/api``) where the swagger
|
||||
sources are written.
|
||||
"""
|
||||
docs_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
spec_path = docs_dir / SWAGGER_SPEC_FILENAME
|
||||
spec_json = json.dumps(spec, indent=2)
|
||||
if not spec_path.exists() or spec_path.read_text(encoding="utf-8") != spec_json:
|
||||
spec_path.write_text(spec_json, encoding="utf-8")
|
||||
|
||||
index_path = docs_dir / "index.md"
|
||||
content = "# API Reference\n\n" f'<swagger-ui src="{SWAGGER_SPEC_FILENAME}"/>\n'
|
||||
if not index_path.exists() or index_path.read_text(encoding="utf-8") != content:
|
||||
index_path.write_text(content, encoding="utf-8")
|
||||
13
docforge/cli/api_utils.pyi
Normal file
13
docforge/cli/api_utils.pyi
Normal file
@@ -0,0 +1,13 @@
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@dataclass
|
||||
class OpenAPIMetadata:
|
||||
site_name: str
|
||||
site_description: str | None
|
||||
site_author: str | None
|
||||
|
||||
def load_openapi_spec(spec_path: Path) -> dict[Any, Any]: ...
|
||||
def derive_metadata(spec: dict[Any, Any]) -> OpenAPIMetadata: ...
|
||||
def generate_api_sources(spec: dict[Any, Any], docs_dir: Path) -> None: ...
|
||||
@@ -4,14 +4,26 @@
|
||||
Command definitions for the doc-forge CLI.
|
||||
|
||||
Provides the CLI structure using Click, including build, serve, and tree commands.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- The `build` command validates requested modes before generating anything.
|
||||
- `--mkdocs`, `--api`, and `--wiki` each emit their own MkDocs config and
|
||||
build (`docs/mkdocs.{kind}.yml` into `site/{kind}`); `--mcp` generates a
|
||||
machine-readable bundle independently.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
import click
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Sequence, Optional
|
||||
|
||||
import click
|
||||
|
||||
from docforge.cli import api_utils, mcp_utils, mkdocs_utils
|
||||
from docforge.loaders import GriffeLoader
|
||||
from docforge.cli import mkdocs_utils
|
||||
from docforge.cli import mcp_utils
|
||||
from docforge.models import DocObject
|
||||
|
||||
|
||||
@click.group()
|
||||
@@ -27,75 +39,143 @@ def cli() -> None:
|
||||
|
||||
@cli.command()
|
||||
@click.option("--mcp", is_flag=True, help="Build MCP resources")
|
||||
@click.option("--mkdocs", is_flag=True, help="Build MkDocs site")
|
||||
@click.option("--module-is-source", is_flag=True, help="Module is source folder and to be treated as root folder")
|
||||
@click.option("--mkdocs", is_flag=True, help="Build the lib MkDocs site")
|
||||
@click.option("--api", is_flag=True, help="Build API docs from an OpenAPI spec")
|
||||
@click.option(
|
||||
"--wiki", is_flag=True, help="Build a hand-written wiki as its own MkDocs site"
|
||||
)
|
||||
@click.option(
|
||||
"--refresh",
|
||||
is_flag=True,
|
||||
help="Regenerate existing docs/mkdocs.{kind}.yml configs from templates",
|
||||
)
|
||||
@click.option(
|
||||
"--module-is-source",
|
||||
is_flag=True,
|
||||
help="Module is source folder and to be treated as root folder",
|
||||
)
|
||||
@click.option("--module", help="Python module to document")
|
||||
@click.option(
|
||||
"--openapi-spec",
|
||||
type=click.Path(path_type=Path),
|
||||
help="Path to the OpenAPI JSON specification",
|
||||
)
|
||||
@click.option("--project-name", help="Project name override")
|
||||
@click.option("--site-name", help="MkDocs site name")
|
||||
@click.option("--docs-dir", type=click.Path(path_type=Path), default=Path("docs"), help="Directory for MD sources")
|
||||
@click.option("--nav", "nav_file", type=click.Path(path_type=Path), default=Path("docforge.nav.yml"),
|
||||
help="Nav spec path")
|
||||
@click.option("--template", type=click.Path(path_type=Path), help="MkDocs template path")
|
||||
@click.option("--mkdocs-yml", type=click.Path(path_type=Path), default=Path("mkdocs.yml"), help="Output config path")
|
||||
@click.option("--out-dir", type=click.Path(path_type=Path), default=Path("mcp_docs"), help="MCP output directory")
|
||||
@click.option("--site-name", help="MkDocs site name for lib and wiki sites")
|
||||
@click.option(
|
||||
"--docs-dir",
|
||||
type=click.Path(path_type=Path),
|
||||
default=Path("docs"),
|
||||
help="MkDocs documentation root",
|
||||
)
|
||||
@click.option(
|
||||
"--wiki-dir",
|
||||
type=click.Path(path_type=Path),
|
||||
default=Path("docs/wiki"),
|
||||
help="Hand-written wiki directory built as its own MkDocs site",
|
||||
)
|
||||
@click.option(
|
||||
"--nav",
|
||||
"nav_file",
|
||||
type=click.Path(path_type=Path),
|
||||
default=Path("docforge.nav.yml"),
|
||||
help="Nav spec path",
|
||||
)
|
||||
@click.option(
|
||||
"--template", type=click.Path(path_type=Path), help="MkDocs template path"
|
||||
)
|
||||
@click.option(
|
||||
"--out-dir",
|
||||
type=click.Path(path_type=Path),
|
||||
default=Path("docs/mcp"),
|
||||
help="MCP output directory",
|
||||
)
|
||||
def build(
|
||||
mcp: bool,
|
||||
mkdocs: bool,
|
||||
api: bool,
|
||||
wiki: bool,
|
||||
refresh: bool,
|
||||
module_is_source: bool,
|
||||
module: Optional[str],
|
||||
project_name: Optional[str],
|
||||
site_name: Optional[str],
|
||||
module: str | None,
|
||||
openapi_spec: Path | None,
|
||||
project_name: str | None,
|
||||
site_name: str | None,
|
||||
docs_dir: Path,
|
||||
wiki_dir: Path,
|
||||
nav_file: Path,
|
||||
template: Optional[Path],
|
||||
mkdocs_yml: Path,
|
||||
template: Path | None,
|
||||
out_dir: Path,
|
||||
) -> None:
|
||||
"""
|
||||
Build documentation artifacts.
|
||||
|
||||
This command performs the full documentation build pipeline:
|
||||
|
||||
1. Introspects the Python project using Griffe
|
||||
2. Generates renderer-specific documentation sources
|
||||
3. Optionally builds the final documentation output
|
||||
This command runs the full documentation pipeline: it loads Python
|
||||
modules, generates renderer-specific documentation sources, and
|
||||
optionally builds the final output.
|
||||
|
||||
Depending on the selected options, the build can target:
|
||||
|
||||
- MkDocs static documentation sites
|
||||
- MCP structured documentation resources
|
||||
- A lib MkDocs site (`--mkdocs`) for library reference docs
|
||||
- A swagger-enabled API MkDocs site (`--api`) built from an OpenAPI spec
|
||||
- A wiki MkDocs site (`--wiki`) built from hand-written markdown
|
||||
- MCP structured documentation resources (`--mcp`)
|
||||
|
||||
Each enabled site kind produces its own MkDocs configuration
|
||||
(`docs/mkdocs.{kind}.yml`) and its own build (`site/{kind}`).
|
||||
|
||||
Notes:
|
||||
- At least one of `--mcp`, `--mkdocs`, `--wiki`, or `--api` must be
|
||||
provided.
|
||||
- `--mkdocs`, `--api`, and `--wiki` emit independent MkDocs builds,
|
||||
while `--mcp` emits a machine-readable bundle.
|
||||
- Configuration files are generated only when absent; an existing
|
||||
`docs/mkdocs.{kind}.yml` is used as-is. Pass `--refresh` to
|
||||
rebaseline it from the templates.
|
||||
|
||||
Args:
|
||||
mcp (bool):
|
||||
Enable MCP documentation generation.
|
||||
|
||||
mkdocs (bool):
|
||||
Enable MkDocs documentation generation.
|
||||
Enable the lib MkDocs documentation generation.
|
||||
|
||||
api (bool):
|
||||
Enable API documentation generation from an OpenAPI spec.
|
||||
|
||||
wiki (bool):
|
||||
Build a hand-written wiki directory as its own MkDocs site.
|
||||
|
||||
refresh (bool):
|
||||
Regenerate ``docs/mkdocs.{kind}.yml`` from templates even when
|
||||
it already exists. By default, existing configs are used as-is.
|
||||
|
||||
module_is_source (bool):
|
||||
Treat the specified module directory as the project root.
|
||||
|
||||
module (Optional[str]):
|
||||
module (str | None):
|
||||
Python module import path to document.
|
||||
|
||||
project_name (Optional[str]):
|
||||
openapi_spec (Path | None):
|
||||
Path to the OpenAPI JSON specification used for API docs.
|
||||
|
||||
project_name (str | None):
|
||||
Optional override for the project name.
|
||||
|
||||
site_name (Optional[str]):
|
||||
Display name for the MkDocs site.
|
||||
site_name (str | None):
|
||||
Display name for the lib and wiki MkDocs sites.
|
||||
|
||||
docs_dir (Path):
|
||||
Directory where Markdown documentation sources will be generated.
|
||||
Shared documentation root used for generated sources.
|
||||
wiki_dir (Path):
|
||||
Directory containing hand-written wiki markdown files.
|
||||
|
||||
nav_file (Path):
|
||||
Path to the navigation specification file.
|
||||
|
||||
template (Optional[Path]):
|
||||
template (Path | None):
|
||||
Optional custom MkDocs configuration template.
|
||||
|
||||
mkdocs_yml (Path):
|
||||
Output path for the generated MkDocs configuration.
|
||||
|
||||
out_dir (Path):
|
||||
Output directory for generated MCP resources.
|
||||
|
||||
@@ -103,34 +183,102 @@ def build(
|
||||
click.UsageError:
|
||||
If required options are missing or conflicting.
|
||||
"""
|
||||
if not mcp and not mkdocs:
|
||||
raise click.UsageError("Must specify either --mcp or --mkdocs")
|
||||
if not mcp and not mkdocs and not api and not wiki:
|
||||
raise click.UsageError("Must specify either --mcp, --mkdocs, --wiki, or --api")
|
||||
|
||||
if api:
|
||||
if not openapi_spec:
|
||||
raise click.UsageError("--openapi-spec is required for API build")
|
||||
if site_name and not mkdocs and not wiki:
|
||||
raise click.UsageError(
|
||||
"--site-name cannot be overridden for API build; "
|
||||
"the OpenAPI spec provides the site name"
|
||||
)
|
||||
|
||||
if mkdocs and not module:
|
||||
raise click.UsageError("--module is required for MkDocs build")
|
||||
if mcp and not module:
|
||||
raise click.UsageError("--module is required for MCP build")
|
||||
|
||||
spec: dict | None = None
|
||||
if api:
|
||||
spec = api_utils.load_openapi_spec(openapi_spec)
|
||||
|
||||
kinds: list[str] = []
|
||||
if mkdocs:
|
||||
if not module:
|
||||
raise click.UsageError("--module is required for MkDocs build")
|
||||
if not site_name:
|
||||
site_name = module
|
||||
|
||||
click.echo(f"Generating MkDocs sources in {docs_dir}...")
|
||||
kinds.append("lib")
|
||||
lib_dir = docs_dir / "lib"
|
||||
click.echo(f"Generating MkDocs sources in {lib_dir}...")
|
||||
mkdocs_utils.generate_sources(
|
||||
module,
|
||||
docs_dir,
|
||||
lib_dir,
|
||||
project_name,
|
||||
module_is_source,
|
||||
readme_dir=Path(".").resolve(),
|
||||
)
|
||||
|
||||
click.echo(f"Generating MkDocs config {mkdocs_yml}...")
|
||||
mkdocs_utils.generate_config(docs_dir, nav_file, template, mkdocs_yml, site_name)
|
||||
if api:
|
||||
kinds.append("api")
|
||||
api_dir = docs_dir / "api"
|
||||
click.echo(f"Generating API sources in {api_dir}...")
|
||||
api_utils.generate_api_sources(spec, api_dir)
|
||||
|
||||
click.echo("Running MkDocs build...")
|
||||
mkdocs_utils.build(mkdocs_yml)
|
||||
click.echo("MkDocs build completed.")
|
||||
if wiki:
|
||||
kinds.append("wiki")
|
||||
|
||||
if kinds:
|
||||
api_metadata: api_utils.OpenAPIMetadata | None = None
|
||||
if api:
|
||||
api_metadata = api_utils.derive_metadata(spec)
|
||||
theme_icon = mkdocs_utils.load_spec_icon(nav_file)
|
||||
|
||||
config_paths: list[Path] = []
|
||||
for kind in kinds:
|
||||
out = docs_dir / f"mkdocs.{kind}.yml"
|
||||
if out.exists() and not refresh:
|
||||
click.echo(
|
||||
f"Using existing MkDocs config {out} (run --refresh to rebaseline)..."
|
||||
)
|
||||
config_paths.append(out)
|
||||
continue
|
||||
|
||||
kind_root = wiki_dir if kind == "wiki" else docs_dir / kind
|
||||
|
||||
site_name_kind = site_name or module or Path.cwd().name
|
||||
site_description: str | None = None
|
||||
site_author: str | None = None
|
||||
if kind == "api" and api_metadata:
|
||||
site_name_kind = api_metadata.site_name
|
||||
site_description = api_metadata.site_description
|
||||
site_author = api_metadata.site_author
|
||||
|
||||
if kind == "lib":
|
||||
nav_block, _kind_icon = mkdocs_utils.build_lib_nav(nav_file, docs_dir)
|
||||
elif kind == "wiki":
|
||||
nav_block = mkdocs_utils.build_wiki_nav_block(wiki_dir)
|
||||
else:
|
||||
nav_block = [{"API Reference": "index.md"}]
|
||||
|
||||
click.echo(f"Generating MkDocs config {out}...")
|
||||
mkdocs_utils.generate_site_config(
|
||||
kind,
|
||||
kind_root,
|
||||
nav_block,
|
||||
out,
|
||||
site_name_kind,
|
||||
Path(os.path.relpath(kind_root, docs_dir)).as_posix(),
|
||||
Path(os.path.relpath(Path.cwd() / "site" / kind, docs_dir)).as_posix(),
|
||||
template=template,
|
||||
site_description=site_description,
|
||||
site_author=site_author,
|
||||
theme_icon=theme_icon,
|
||||
)
|
||||
config_paths.append(out)
|
||||
|
||||
click.echo("Running MkDocs builds...")
|
||||
mkdocs_utils.build_configs(config_paths)
|
||||
|
||||
if mcp:
|
||||
if not module:
|
||||
raise click.UsageError("--module is required for MCP build")
|
||||
|
||||
click.echo(f"Generating MCP resources in {out_dir}...")
|
||||
mcp_utils.generate_resources(module, project_name, out_dir)
|
||||
click.echo("MCP build completed.")
|
||||
@@ -138,14 +286,30 @@ def build(
|
||||
|
||||
@cli.command()
|
||||
@click.option("--mcp", is_flag=True, help="Serve MCP documentation")
|
||||
@click.option("--mkdocs", is_flag=True, help="Serve MkDocs site")
|
||||
@click.option("--mkdocs", is_flag=True, help="Serve an MkDocs site from --mkdocs-yml")
|
||||
@click.option("--lib", is_flag=True, help="Serve the lib MkDocs site")
|
||||
@click.option("--api", is_flag=True, help="Serve the API MkDocs site")
|
||||
@click.option("--wiki", is_flag=True, help="Serve the wiki MkDocs site")
|
||||
@click.option("--module", help="Python module to serve")
|
||||
@click.option("--mkdocs-yml", type=click.Path(path_type=Path), default=Path("mkdocs.yml"), help="MkDocs config path")
|
||||
@click.option("--out-dir", type=click.Path(path_type=Path), default=Path("mcp_docs"), help="MCP root directory")
|
||||
@click.option(
|
||||
"--mkdocs-yml",
|
||||
type=click.Path(path_type=Path),
|
||||
default=Path("docs/mkdocs.wiki.yml"),
|
||||
help="MkDocs config path",
|
||||
)
|
||||
@click.option(
|
||||
"--out-dir",
|
||||
type=click.Path(path_type=Path),
|
||||
default=Path("docs/mcp"),
|
||||
help="MCP root directory",
|
||||
)
|
||||
def serve(
|
||||
mcp: bool,
|
||||
mkdocs: bool,
|
||||
module: Optional[str],
|
||||
lib: bool,
|
||||
api: bool,
|
||||
wiki: bool,
|
||||
module: str | None,
|
||||
mkdocs_yml: Path,
|
||||
out_dir: Path,
|
||||
) -> None:
|
||||
@@ -154,17 +318,30 @@ def serve(
|
||||
|
||||
Depending on the selected mode, this command starts either:
|
||||
|
||||
- A MkDocs development server for browsing documentation
|
||||
- A MkDocs development server for browsing a site, or
|
||||
- An MCP server exposing structured documentation resources
|
||||
|
||||
The kind flags (`--lib`, `--api`, `--wiki`) select the generated
|
||||
per-kind config (`docs/mkdocs.{kind}.yml`); `--mkdocs` serves the config
|
||||
passed via `--mkdocs-yml`.
|
||||
|
||||
Args:
|
||||
mcp (bool):
|
||||
Serve documentation using the MCP server.
|
||||
|
||||
mkdocs (bool):
|
||||
Serve the MkDocs development site.
|
||||
Serve the MkDocs development site from ``--mkdocs-yml``.
|
||||
|
||||
module (Optional[str]):
|
||||
lib (bool):
|
||||
Serve the lib MkDocs site.
|
||||
|
||||
api (bool):
|
||||
Serve the API MkDocs site.
|
||||
|
||||
wiki (bool):
|
||||
Serve the wiki MkDocs site.
|
||||
|
||||
module (str | None):
|
||||
Python module import path to serve via MCP.
|
||||
|
||||
mkdocs_yml (Path):
|
||||
@@ -177,17 +354,37 @@ def serve(
|
||||
click.UsageError:
|
||||
If invalid or conflicting options are provided.
|
||||
"""
|
||||
if mcp and mkdocs:
|
||||
raise click.UsageError("Cannot specify both --mcp and --mkdocs")
|
||||
if not mcp and not mkdocs:
|
||||
raise click.UsageError("Must specify either --mcp or --mkdocs")
|
||||
if mcp and not module:
|
||||
raise click.UsageError("--module is required for MCP serve")
|
||||
selected = [
|
||||
name
|
||||
for name, enabled in (
|
||||
("mcp", mcp),
|
||||
("mkdocs", mkdocs),
|
||||
("lib", lib),
|
||||
("api", api),
|
||||
("wiki", wiki),
|
||||
)
|
||||
if enabled
|
||||
]
|
||||
if len(selected) != 1:
|
||||
raise click.UsageError(
|
||||
"Must specify exactly one of --mcp, --mkdocs, --lib, --api, --wiki"
|
||||
)
|
||||
|
||||
if mkdocs:
|
||||
mkdocs_utils.serve(mkdocs_yml)
|
||||
elif mcp:
|
||||
if mcp:
|
||||
if not module:
|
||||
raise click.UsageError("--module is required for MCP serve")
|
||||
mcp_utils.serve(module, out_dir)
|
||||
return
|
||||
|
||||
config = mkdocs_yml
|
||||
if lib:
|
||||
config = Path("docs/mkdocs.lib.yml")
|
||||
elif api:
|
||||
config = Path("docs/mkdocs.api.yml")
|
||||
elif wiki:
|
||||
config = Path("docs/mkdocs.wiki.yml")
|
||||
|
||||
mkdocs_utils.serve(config)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@@ -202,7 +399,7 @@ def serve(
|
||||
)
|
||||
def tree(
|
||||
module: str,
|
||||
project_name: Optional[str],
|
||||
project_name: str | None,
|
||||
) -> None:
|
||||
"""
|
||||
Display the documentation object tree for a module.
|
||||
@@ -215,7 +412,7 @@ def tree(
|
||||
module (str):
|
||||
Python module import path to introspect.
|
||||
|
||||
project_name (Optional[str]):
|
||||
project_name (str | None):
|
||||
Optional name to display as the project root.
|
||||
"""
|
||||
loader = GriffeLoader()
|
||||
@@ -229,7 +426,7 @@ def tree(
|
||||
_print_object(obj, indent="│ ")
|
||||
|
||||
|
||||
def _print_object(obj, indent: str) -> None:
|
||||
def _print_object(obj: DocObject, indent: str) -> None:
|
||||
"""
|
||||
Recursively print a documentation object and its members.
|
||||
|
||||
@@ -237,7 +434,7 @@ def _print_object(obj, indent: str) -> None:
|
||||
and prints each object with indentation to represent hierarchy.
|
||||
|
||||
Args:
|
||||
obj:
|
||||
obj (DocObject):
|
||||
Documentation object to print.
|
||||
|
||||
indent (str):
|
||||
|
||||
@@ -1,34 +1,39 @@
|
||||
from click.core import Group
|
||||
from pathlib import Path
|
||||
from typing import Sequence, Optional, Any
|
||||
|
||||
from click.core import Group
|
||||
|
||||
from docforge.models import DocObject
|
||||
|
||||
cli: Group
|
||||
|
||||
def build(
|
||||
mcp: bool,
|
||||
mkdocs: bool,
|
||||
api: bool,
|
||||
wiki: bool,
|
||||
module_is_source: bool,
|
||||
module: Optional[str],
|
||||
project_name: Optional[str],
|
||||
site_name: Optional[str],
|
||||
module: str | None,
|
||||
openapi_spec: Path | None,
|
||||
project_name: str | None,
|
||||
site_name: str | None,
|
||||
docs_dir: Path,
|
||||
wiki_dir: Path,
|
||||
nav_file: Path,
|
||||
template: Optional[Path],
|
||||
mkdocs_yml: Path,
|
||||
template: Path | None,
|
||||
out_dir: Path,
|
||||
) -> None: ...
|
||||
|
||||
def serve(
|
||||
mcp: bool,
|
||||
mkdocs: bool,
|
||||
module: Optional[str],
|
||||
lib: bool,
|
||||
api: bool,
|
||||
wiki: bool,
|
||||
module: str | None,
|
||||
mkdocs_yml: Path,
|
||||
out_dir: Path,
|
||||
) -> None: ...
|
||||
|
||||
def tree(
|
||||
module: str,
|
||||
project_name: Optional[str],
|
||||
project_name: str | None,
|
||||
) -> None: ...
|
||||
|
||||
def _print_object(obj: Any, indent: str) -> None: ...
|
||||
def _print_object(obj: DocObject, indent: str) -> None: ...
|
||||
|
||||
@@ -22,4 +22,4 @@ def main() -> None:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -2,10 +2,22 @@
|
||||
# Summary
|
||||
|
||||
Utilities for working with MCP in the doc-forge CLI.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- `generate_resources` produces the bundle consumed by `MCPServer`:
|
||||
`index.json`, `nav.json`, and per-module resources under `modules/`.
|
||||
- Resource URIs use the `docs://` scheme: `docs://index`, `docs://nav`,
|
||||
and `docs://modules/{module}`.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
|
||||
from docforge.loaders import GriffeLoader, discover_module_paths
|
||||
from docforge.renderers import MCPRenderer
|
||||
from docforge.servers import MCPServer
|
||||
@@ -24,7 +36,7 @@ def generate_resources(module: str, project_name: str | None, out_dir: Path) ->
|
||||
Python module import path used as the entry point for
|
||||
documentation generation.
|
||||
|
||||
project_name (Optional[str]):
|
||||
project_name (str | None):
|
||||
Optional override for the project name used in generated
|
||||
documentation metadata.
|
||||
|
||||
@@ -61,7 +73,7 @@ def serve(module: str, mcp_root: Path) -> None:
|
||||
If the MCP documentation bundle is missing required files or directories.
|
||||
"""
|
||||
if not mcp_root.exists():
|
||||
raise click.ClickException(f"mcp_docs directory not found: {mcp_root}")
|
||||
raise click.ClickException(f"MCP docs directory not found: {mcp_root}")
|
||||
|
||||
required = [
|
||||
mcp_root / "index.json",
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from pathlib import Path
|
||||
|
||||
def generate_resources(module: str, project_name: str | None, out_dir: Path) -> None: ...
|
||||
def generate_resources(
|
||||
module: str, project_name: str | None, out_dir: Path
|
||||
) -> None: ...
|
||||
def serve(module: str, mcp_root: Path) -> None: ...
|
||||
|
||||
@@ -2,15 +2,35 @@
|
||||
# Summary
|
||||
|
||||
Utilities for working with MkDocs in the doc-forge CLI.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- A separate `mkdocs.{kind}.yml` configuration and build is emitted per
|
||||
enabled kind (lib, api, wiki), each scoped to its own `docs_dir` and
|
||||
written into its own `site_dir` (`site/lib`, `site/api`, `site/wiki`).
|
||||
- Navigation blocks are re-rooted per kind: the wiki navigation drops its
|
||||
leading `wiki/` scope and the resolved nav spec drops its `lib/` scope.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from collections.abc import Iterable
|
||||
from importlib import resources
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
|
||||
import click
|
||||
import yaml
|
||||
|
||||
from docforge.loaders import GriffeLoader, discover_module_paths
|
||||
from docforge.nav import (
|
||||
MkDocsNavEmitter,
|
||||
build_wiki_nav,
|
||||
load_nav_spec,
|
||||
resolve_nav,
|
||||
)
|
||||
from docforge.renderers import MkDocsRenderer
|
||||
from docforge.nav import load_nav_spec, resolve_nav, MkDocsNavEmitter
|
||||
|
||||
|
||||
def generate_sources(
|
||||
@@ -18,6 +38,7 @@ def generate_sources(
|
||||
docs_dir: Path,
|
||||
project_name: str | None = None,
|
||||
module_is_source: bool | None = None,
|
||||
readme_dir: Path | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Generate MkDocs Markdown sources for a Python module.
|
||||
@@ -34,12 +55,16 @@ def generate_sources(
|
||||
docs_dir (Path):
|
||||
Directory where the generated Markdown files will be written.
|
||||
|
||||
project_name (Optional[str]):
|
||||
project_name (str | None):
|
||||
Optional override for the project name used in documentation metadata.
|
||||
|
||||
module_is_source (Optional[bool]):
|
||||
module_is_source (bool | None):
|
||||
If True, treat the specified module directory as the project root
|
||||
rather than a nested module.
|
||||
|
||||
readme_dir (Path | None):
|
||||
Directory where the generated README.md should be written. If not
|
||||
provided, defaults to the parent of ``docs_dir``.
|
||||
"""
|
||||
loader = GriffeLoader()
|
||||
discovered_paths = discover_module_paths(module)
|
||||
@@ -56,92 +81,353 @@ def generate_sources(
|
||||
project,
|
||||
docs_dir,
|
||||
module_is_source,
|
||||
readme_dir,
|
||||
)
|
||||
|
||||
|
||||
def generate_config(
|
||||
docs_dir: Path,
|
||||
def build_lib_nav(
|
||||
nav_file: Path,
|
||||
template: Path | None,
|
||||
out: Path,
|
||||
site_name: str,
|
||||
) -> None:
|
||||
docs_root: Path,
|
||||
) -> tuple[list[dict[str, Any]], dict[str, str] | None]:
|
||||
"""
|
||||
Generate an `mkdocs.yml` configuration file.
|
||||
Build the re-rooted navigation block for a lib site.
|
||||
|
||||
The configuration is created by combining a template configuration
|
||||
with a navigation structure derived from the docforge navigation
|
||||
specification.
|
||||
The navigation specification is resolved against the shared documentation
|
||||
root and every resulting path is re-rooted relative to the ``lib``
|
||||
subdirectory by stripping its leading ``lib/`` scope component.
|
||||
|
||||
Args:
|
||||
docs_dir (Path):
|
||||
Directory containing generated documentation Markdown files.
|
||||
|
||||
nav_file (Path):
|
||||
Path to the `docforge.nav.yml` navigation specification.
|
||||
|
||||
template (Optional[Path]):
|
||||
Optional path to a custom MkDocs configuration template. If not
|
||||
provided, a built-in template will be used.
|
||||
docs_root (Path):
|
||||
Shared documentation root containing the ``lib`` sources.
|
||||
|
||||
out (Path):
|
||||
Destination path where the generated `mkdocs.yml` file will be written.
|
||||
|
||||
site_name (str):
|
||||
Display name for the generated documentation site.
|
||||
Returns:
|
||||
tuple[list[dict[str, Any]], dict[str, str] | None]:
|
||||
The re-rooted navigation block and the optional theme icon
|
||||
mapping from the specification.
|
||||
|
||||
Raises:
|
||||
click.FileError:
|
||||
If the navigation specification or template file cannot be found.
|
||||
If the navigation specification cannot be found.
|
||||
"""
|
||||
if not nav_file.exists():
|
||||
raise click.FileError(str(nav_file), hint="Nav spec not found")
|
||||
|
||||
spec = load_nav_spec(nav_file)
|
||||
resolved = resolve_nav(spec, docs_dir)
|
||||
nav_block = MkDocsNavEmitter().emit(resolved)
|
||||
resolved = resolve_nav(spec, docs_root)
|
||||
block = MkDocsNavEmitter().emit(resolved)
|
||||
return cast(list[dict[str, Any]], _strip_scope(block, "lib")), spec.icon
|
||||
|
||||
# Load template
|
||||
if template is not None:
|
||||
if not template.exists():
|
||||
raise click.FileError(str(template), hint="Template not found")
|
||||
data = yaml.safe_load(template.read_text(encoding="utf-8"))
|
||||
else:
|
||||
text = (
|
||||
resources.files("docforge.templates")
|
||||
.joinpath("mkdocs.sample.yml")
|
||||
.read_text(encoding="utf-8")
|
||||
)
|
||||
data = yaml.safe_load(text)
|
||||
|
||||
def build_wiki_nav_block(wiki_dir: Path) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Build the re-rooted navigation block for a wiki site.
|
||||
|
||||
The wiki navigation derived from the wiki file structure is re-rooted
|
||||
relative to the wiki directory itself by stripping the leading ``wiki/``
|
||||
scope component.
|
||||
|
||||
Args:
|
||||
wiki_dir (Path):
|
||||
Path to the hand-written wiki directory, for example ``docs/wiki``.
|
||||
|
||||
Returns:
|
||||
list[dict[str, Any]]:
|
||||
Navigation entries relative to the wiki directory.
|
||||
|
||||
Raises:
|
||||
click.FileError:
|
||||
If the wiki directory does not exist.
|
||||
"""
|
||||
if not wiki_dir.exists():
|
||||
raise click.FileError(str(wiki_dir), hint="Wiki dir not found")
|
||||
|
||||
return cast(list[dict[str, Any]], _strip_scope(build_wiki_nav(wiki_dir), "wiki"))
|
||||
|
||||
|
||||
def load_spec_icon(nav_file: Path) -> dict[str, str] | None:
|
||||
"""
|
||||
Load the theme icon mapping from a navigation specification.
|
||||
|
||||
Args:
|
||||
nav_file (Path):
|
||||
Path to the navigation specification file.
|
||||
|
||||
Returns:
|
||||
dict[str, str] | None:
|
||||
The icon mapping, or ``None`` when the specification file is
|
||||
absent or cannot be parsed.
|
||||
"""
|
||||
if not nav_file.exists():
|
||||
return None
|
||||
|
||||
try:
|
||||
return load_nav_spec(nav_file).icon
|
||||
except (ValueError, yaml.YAMLError):
|
||||
return None
|
||||
|
||||
|
||||
def _strip_scope(value: object, scope: str) -> object:
|
||||
"""
|
||||
Re-root navigation paths by removing a leading scope component.
|
||||
|
||||
The navigation block is walked recursively and every relative path
|
||||
string that starts with ``{scope}/`` has that prefix removed, so a lib
|
||||
or wiki site scoped to its own ``docs_dir`` can reuse paths that were
|
||||
originally written relative to the shared documentation root.
|
||||
|
||||
Args:
|
||||
value (object):
|
||||
Navigation block, group list, path string, or scalar value.
|
||||
scope (str):
|
||||
Leading path component to strip, for example ``lib`` or ``wiki``.
|
||||
|
||||
Returns:
|
||||
object:
|
||||
The navigation structure with re-rooted paths.
|
||||
"""
|
||||
if isinstance(value, str):
|
||||
prefix = f"{scope}/"
|
||||
return value[len(prefix) :] if value.startswith(prefix) else value
|
||||
if isinstance(value, list):
|
||||
return [_strip_scope(item, scope) for item in value]
|
||||
if isinstance(value, dict):
|
||||
return {key: _strip_scope(val, scope) for key, val in value.items()}
|
||||
return value
|
||||
|
||||
|
||||
def generate_site_config(
|
||||
kind: str,
|
||||
kind_root: Path,
|
||||
nav_block: list[dict[str, Any]],
|
||||
out: Path,
|
||||
site_name: str,
|
||||
docs_dir: str,
|
||||
site_dir: str,
|
||||
template: Path | None = None,
|
||||
site_description: str | None = None,
|
||||
site_author: str | None = None,
|
||||
theme_icon: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Generate a per-kind `mkdocs.{kind}.yml` configuration file.
|
||||
|
||||
The configuration is created by merging the shared ``mkdocs.common.yml``
|
||||
template with the fragment contributed by the kind (``lib``, ``api``, or
|
||||
``wiki``). Both ``docs_dir`` and ``site_dir`` are written relative to the
|
||||
configuration file's directory: the kind's sources when expressed as a
|
||||
sibling path (for example ``lib``) and the per-kind site output (for
|
||||
example ``../site/lib``).
|
||||
|
||||
Args:
|
||||
kind (str):
|
||||
Documentation kind, one of ``lib``, ``api``, or ``wiki``.
|
||||
|
||||
kind_root (Path):
|
||||
Directory scoped to the kind (for example ``docs/lib``) that
|
||||
serves as the MkDocs ``docs_dir``.
|
||||
|
||||
nav_block (list[dict[str, Any]]):
|
||||
Re-rooted navigation entries for the kind's site.
|
||||
|
||||
out (Path):
|
||||
Destination path where the generated ``mkdocs.{kind}.yml`` file
|
||||
is written.
|
||||
|
||||
site_name (str):
|
||||
Display name for the generated documentation site.
|
||||
|
||||
docs_dir (str):
|
||||
MkDocs ``docs_dir`` value, relative to the configuration
|
||||
file's directory.
|
||||
|
||||
site_dir (str):
|
||||
MkDocs ``site_dir`` value, relative to the configuration
|
||||
file's directory.
|
||||
|
||||
template (Path | None):
|
||||
Optional path to a fully custom MkDocs configuration template
|
||||
that replaces the built-in templates entirely.
|
||||
|
||||
site_description (str | None):
|
||||
Optional site description written into the configuration.
|
||||
|
||||
site_author (str | None):
|
||||
Optional site author written into the configuration.
|
||||
|
||||
theme_icon (dict[str, str] | None):
|
||||
Optional mapping of theme icon entries injected as
|
||||
``theme.icon``.
|
||||
"""
|
||||
data = _load_template(template, [kind])
|
||||
|
||||
data["site_name"] = site_name
|
||||
if site_description:
|
||||
data["site_description"] = site_description
|
||||
if site_author:
|
||||
data["site_author"] = site_author
|
||||
data["docs_dir"] = docs_dir
|
||||
data["site_dir"] = site_dir
|
||||
data["nav"] = nav_block
|
||||
|
||||
if theme_icon:
|
||||
theme = data.setdefault("theme", {})
|
||||
if not isinstance(theme, dict):
|
||||
theme = {}
|
||||
theme["icon"] = theme_icon
|
||||
data["theme"] = theme
|
||||
|
||||
out.write_text(yaml.safe_dump(data, sort_keys=False), encoding="utf-8")
|
||||
|
||||
|
||||
def build(mkdocs_yml: Path) -> None:
|
||||
def _load_template(
|
||||
template: Path | None,
|
||||
modes: Iterable[str] | None,
|
||||
) -> dict:
|
||||
"""
|
||||
Build the MkDocs documentation site.
|
||||
Load the MkDocs configuration template.
|
||||
|
||||
This function loads the MkDocs configuration and runs the MkDocs
|
||||
build command to generate the final static documentation site.
|
||||
When a custom template path is provided, it is used as-is. Otherwise the
|
||||
shared ``mkdocs.common.yml`` template is deep-merged with the fragments
|
||||
contributed by each enabled mode (``lib``, ``api``, or ``wiki``).
|
||||
|
||||
Args:
|
||||
mkdocs_yml (Path):
|
||||
Path to the `mkdocs.yml` configuration file.
|
||||
template (Path | None):
|
||||
Optional fully custom template that replaces the built-ins.
|
||||
|
||||
modes (Iterable[str] | None):
|
||||
Documentation modes whose template fragments should be merged.
|
||||
|
||||
Returns:
|
||||
dict:
|
||||
Merged MkDocs configuration mapping.
|
||||
|
||||
Raises:
|
||||
click.FileError:
|
||||
If a referenced template file cannot be found.
|
||||
"""
|
||||
if template is not None:
|
||||
if not template.exists():
|
||||
raise click.FileError(str(template), hint="Template not found")
|
||||
return yaml.safe_load(template.read_text(encoding="utf-8"))
|
||||
|
||||
data: dict = {}
|
||||
active_modes = list(modes) if modes else ["lib"]
|
||||
parts = ["common", *active_modes]
|
||||
|
||||
seen: set[str] = set()
|
||||
for part_name in parts:
|
||||
if part_name in seen:
|
||||
continue
|
||||
seen.add(part_name)
|
||||
|
||||
text = (
|
||||
resources.files("docforge.templates")
|
||||
.joinpath(f"mkdocs.{part_name}.yml")
|
||||
.read_text(encoding="utf-8")
|
||||
)
|
||||
part: dict = yaml.safe_load(text)
|
||||
data = _deep_merge(data, part)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def _item_name(item: object) -> str:
|
||||
"""
|
||||
Return the identifying name of a list entry.
|
||||
|
||||
String entries identify as themselves; mapping entries identify by their
|
||||
first key. This is used to deduplicate plugin and extension lists.
|
||||
|
||||
Args:
|
||||
item (object):
|
||||
List entry, either a string or a single-key mapping.
|
||||
|
||||
Returns:
|
||||
str:
|
||||
The identifying name of the entry.
|
||||
"""
|
||||
if isinstance(item, dict):
|
||||
return next(iter(item.keys()), "")
|
||||
return str(item)
|
||||
|
||||
|
||||
def _merge_list(base: list, added: list) -> list:
|
||||
"""
|
||||
Merge two lists, preserving order and dropping duplicates by name.
|
||||
|
||||
Args:
|
||||
base (list):
|
||||
Existing list entries.
|
||||
added (list):
|
||||
Entries to append when not already present.
|
||||
|
||||
Returns:
|
||||
list:
|
||||
Merged list with duplicates removed.
|
||||
"""
|
||||
result = list(base)
|
||||
names = {_item_name(item) for item in result}
|
||||
for item in added:
|
||||
name = _item_name(item)
|
||||
if name not in names:
|
||||
result.append(item)
|
||||
names.add(name)
|
||||
return result
|
||||
|
||||
|
||||
def _deep_merge(base: dict, part: dict) -> dict:
|
||||
"""
|
||||
Deep merge a template fragment into a base configuration.
|
||||
|
||||
Mappings are merged recursively, while lists are combined by
|
||||
deduplicating entries by their identifying name. Non-container values in
|
||||
the fragment override the base.
|
||||
|
||||
Args:
|
||||
base (dict):
|
||||
Configuration being built up.
|
||||
part (dict):
|
||||
Template fragment to merge into the base.
|
||||
|
||||
Returns:
|
||||
dict:
|
||||
The merged configuration.
|
||||
"""
|
||||
for key, value in part.items():
|
||||
if isinstance(value, dict) and isinstance(base.get(key), dict):
|
||||
base[key] = _deep_merge(base[key], value)
|
||||
elif isinstance(value, list) and isinstance(base.get(key), list):
|
||||
base[key] = _merge_list(base[key], value)
|
||||
else:
|
||||
base[key] = value
|
||||
return base
|
||||
|
||||
|
||||
def build_configs(yml_paths: Iterable[Path]) -> None:
|
||||
"""
|
||||
Build the MkDocs documentation site for every given configuration.
|
||||
|
||||
Each configuration file is loaded and built in turn, producing the
|
||||
per-kind static sites (``site/lib``, ``site/api``, ``site/wiki``).
|
||||
|
||||
Args:
|
||||
yml_paths (Iterable[Path]):
|
||||
Configuration files to build, in order.
|
||||
|
||||
Raises:
|
||||
click.ClickException:
|
||||
If the configuration file does not exist.
|
||||
If a configuration file does not exist.
|
||||
"""
|
||||
if not mkdocs_yml.exists():
|
||||
raise click.ClickException(f"mkdocs.yml not found: {mkdocs_yml}")
|
||||
|
||||
from mkdocs.config import load_config
|
||||
from mkdocs.commands.build import build as mkdocs_build
|
||||
from mkdocs.config import load_config
|
||||
|
||||
mkdocs_build(load_config(str(mkdocs_yml)))
|
||||
for yml in yml_paths:
|
||||
if not yml.exists():
|
||||
raise click.ClickException(f"mkdocs.yml not found: {yml}")
|
||||
mkdocs_build(load_config(str(yml)))
|
||||
|
||||
|
||||
def serve(mkdocs_yml: Path) -> None:
|
||||
@@ -163,4 +449,5 @@ def serve(mkdocs_yml: Path) -> None:
|
||||
raise click.ClickException(f"mkdocs.yml not found: {mkdocs_yml}")
|
||||
|
||||
from mkdocs.commands.serve import serve as mkdocs_serve
|
||||
|
||||
mkdocs_serve(config_file=str(mkdocs_yml))
|
||||
|
||||
@@ -1,11 +1,33 @@
|
||||
from collections.abc import Iterable
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
def generate_sources(
|
||||
module: str,
|
||||
docs_dir: Path,
|
||||
project_name: str | None = None,
|
||||
module_is_source: bool | None = None,
|
||||
readme_dir: Path | None = None,
|
||||
) -> None: ...
|
||||
def generate_config(docs_dir: Path, nav_file: Path, template: Path | None, out: Path, site_name: str) -> None: ...
|
||||
def build(mkdocs_yml: Path) -> None: ...
|
||||
def build_lib_nav(
|
||||
nav_file: Path,
|
||||
docs_root: Path,
|
||||
) -> tuple[list[dict[str, Any]], dict[str, str] | None]: ...
|
||||
def build_wiki_nav_block(wiki_dir: Path) -> list[dict[str, Any]]: ...
|
||||
def load_spec_icon(nav_file: Path) -> dict[str, str] | None: ...
|
||||
def _strip_scope(value: object, scope: str) -> object: ...
|
||||
def generate_site_config(
|
||||
kind: str,
|
||||
kind_root: Path,
|
||||
nav_block: list[dict[str, Any]],
|
||||
out: Path,
|
||||
site_name: str,
|
||||
docs_dir: str,
|
||||
site_dir: str,
|
||||
template: Path | None = None,
|
||||
site_description: str | None = None,
|
||||
site_author: str | None = None,
|
||||
theme_icon: dict[str, str] | None = None,
|
||||
) -> None: ...
|
||||
def build_configs(yml_paths: Iterable[Path]) -> None: ...
|
||||
def serve(mkdocs_yml: Path) -> None: ...
|
||||
|
||||
@@ -6,21 +6,32 @@ Utilities for loading and introspecting Python modules using Griffe.
|
||||
This module provides the `GriffeLoader` class and helper utilities used to
|
||||
discover Python modules, introspect their structure, and convert the results
|
||||
into doc-forge documentation models.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- All analysis is static; analyzed modules are never executed.
|
||||
- Private members (names starting with `_`) are skipped during conversion.
|
||||
- Imported aliases that cannot be resolved (stdlib/third-party names) are
|
||||
skipped; aliases that resolve within the documented project are kept.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
from griffe import (
|
||||
GriffeLoader as _GriffeLoader,
|
||||
ModulesCollection,
|
||||
LinesCollection,
|
||||
Object,
|
||||
AliasResolutionError,
|
||||
LinesCollection,
|
||||
ModulesCollection,
|
||||
Object,
|
||||
)
|
||||
from griffe import (
|
||||
GriffeLoader as _GriffeLoader,
|
||||
)
|
||||
|
||||
from docforge.models import Module, Project, DocObject
|
||||
from docforge.models import DocObject, Module, Project
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -28,7 +39,7 @@ logger = logging.getLogger(__name__)
|
||||
def discover_module_paths(
|
||||
module_name: str,
|
||||
project_root: Path | None = None,
|
||||
) -> List[str]:
|
||||
) -> list[str]:
|
||||
"""
|
||||
Discover Python modules within a package directory.
|
||||
|
||||
@@ -45,12 +56,12 @@ def discover_module_paths(
|
||||
module_name (str):
|
||||
Top-level package name to discover modules from.
|
||||
|
||||
project_root (Path, optional):
|
||||
project_root (Path | None):
|
||||
Root directory used to resolve module paths. If not provided, the
|
||||
current working directory is used.
|
||||
|
||||
Returns:
|
||||
List[str]:
|
||||
list[str]:
|
||||
A sorted list of unique dotted module import paths.
|
||||
|
||||
Raises:
|
||||
@@ -65,7 +76,7 @@ def discover_module_paths(
|
||||
if not pkg_dir.exists():
|
||||
raise FileNotFoundError(f"Package not found: {pkg_dir}")
|
||||
|
||||
module_paths: List[str] = []
|
||||
module_paths: list[str] = []
|
||||
|
||||
for path in pkg_dir.rglob("*.py"):
|
||||
if path.name == "__init__.py":
|
||||
@@ -87,6 +98,10 @@ class GriffeLoader:
|
||||
This loader uses the Griffe introspection engine to analyze Python source
|
||||
code and transform the extracted information into `Project`, `Module`,
|
||||
and `DocObject` instances used by doc-forge.
|
||||
|
||||
Attributes:
|
||||
_loader (_GriffeLoader):
|
||||
Internal Griffe loader with dedicated module and line collections.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
@@ -102,10 +117,10 @@ class GriffeLoader:
|
||||
)
|
||||
|
||||
def load_project(
|
||||
self,
|
||||
module_paths: List[str],
|
||||
project_name: Optional[str] = None,
|
||||
skip_import_errors: bool = None,
|
||||
self,
|
||||
module_paths: list[str],
|
||||
project_name: str | None = None,
|
||||
skip_import_errors: bool | None = None,
|
||||
) -> Project:
|
||||
"""
|
||||
Load multiple modules and assemble them into a Project model.
|
||||
@@ -115,14 +130,14 @@ class GriffeLoader:
|
||||
object.
|
||||
|
||||
Args:
|
||||
module_paths (List[str]):
|
||||
module_paths (list[str]):
|
||||
List of dotted module import paths to load.
|
||||
|
||||
project_name (str, optional):
|
||||
project_name (str | None):
|
||||
Optional override for the project name. Defaults to the top-level
|
||||
name of the first module.
|
||||
|
||||
skip_import_errors (bool, optional):
|
||||
skip_import_errors (bool | None):
|
||||
If True, modules that fail to load will be skipped instead of raising an error.
|
||||
|
||||
Returns:
|
||||
@@ -171,6 +186,21 @@ class GriffeLoader:
|
||||
Returns:
|
||||
Module:
|
||||
A populated `Module` instance.
|
||||
|
||||
Raises:
|
||||
ImportError:
|
||||
If the module cannot be loaded by Griffe.
|
||||
|
||||
KeyError:
|
||||
If the loaded module is missing from the module collection.
|
||||
|
||||
Example:
|
||||
Load a single module:
|
||||
|
||||
```python
|
||||
loader = GriffeLoader()
|
||||
module = loader.load_module("mypackage.submodule")
|
||||
```
|
||||
"""
|
||||
self._loader.load(path)
|
||||
griffe_module = self._loader.modules_collection[path]
|
||||
@@ -204,6 +234,8 @@ class GriffeLoader:
|
||||
for name, member in obj.members.items():
|
||||
if name.startswith("_"):
|
||||
continue
|
||||
if not self._is_resolvable_alias(member):
|
||||
continue
|
||||
|
||||
module.add_object(self._convert_object(member))
|
||||
|
||||
@@ -240,6 +272,8 @@ class GriffeLoader:
|
||||
for name, member in obj.members.items():
|
||||
if name.startswith("_"):
|
||||
continue
|
||||
if not self._is_resolvable_alias(member):
|
||||
continue
|
||||
doc_obj.add_member(self._convert_object(member))
|
||||
except AliasResolutionError:
|
||||
pass
|
||||
@@ -250,7 +284,7 @@ class GriffeLoader:
|
||||
# Safe extractors
|
||||
# -------------------------
|
||||
|
||||
def _safe_docstring(self, obj: Object) -> Optional[str]:
|
||||
def _safe_docstring(self, obj: Object) -> str | None:
|
||||
"""
|
||||
Safely extract a docstring from a Griffe object.
|
||||
|
||||
@@ -259,7 +293,7 @@ class GriffeLoader:
|
||||
Griffe object to inspect.
|
||||
|
||||
Returns:
|
||||
Optional[str]:
|
||||
str | None:
|
||||
The raw docstring text if available, otherwise `None`.
|
||||
"""
|
||||
try:
|
||||
@@ -267,21 +301,57 @@ class GriffeLoader:
|
||||
except AliasResolutionError:
|
||||
return None
|
||||
|
||||
def _safe_signature(self, obj: Object) -> Optional[str]:
|
||||
def _safe_signature(self, obj: Object) -> str | None:
|
||||
"""
|
||||
Safely extract the signature of a Griffe object.
|
||||
|
||||
Griffe exposes signatures as callables (``Object.signature``); the
|
||||
method is invoked and stringified to produce a clean, stable signature.
|
||||
Aliases that point outside the loaded project fail resolution and are
|
||||
reported as `None`.
|
||||
|
||||
Args:
|
||||
obj (Object):
|
||||
Griffe object to inspect.
|
||||
|
||||
Returns:
|
||||
Optional[str]:
|
||||
String representation of the object's signature if available, otherwise `None`.
|
||||
str | None:
|
||||
Clean string representation of the object's signature if
|
||||
available, otherwise `None`.
|
||||
"""
|
||||
try:
|
||||
if hasattr(obj, "signature") and obj.signature:
|
||||
return str(obj.signature)
|
||||
except AliasResolutionError:
|
||||
signature = getattr(obj, "signature", None)
|
||||
if signature is None:
|
||||
return None
|
||||
if callable(signature):
|
||||
signature = signature()
|
||||
if not signature:
|
||||
return None
|
||||
return str(signature).strip() or None
|
||||
except Exception:
|
||||
return None
|
||||
return None
|
||||
|
||||
def _is_resolvable_alias(self, obj: Object) -> bool:
|
||||
"""
|
||||
Report whether an object is a resolvable part of the documented API.
|
||||
|
||||
Aliases that cannot be resolved (stdlib or third-party imports) are
|
||||
treated as noise and excluded from the model. Non-alias objects are
|
||||
always resolvable.
|
||||
|
||||
Args:
|
||||
obj (Object):
|
||||
Griffe object to test.
|
||||
|
||||
Returns:
|
||||
bool:
|
||||
True if the object should be kept, False if it is an
|
||||
unresolvable alias.
|
||||
"""
|
||||
if obj.kind.value != "alias":
|
||||
return True
|
||||
try:
|
||||
_ = obj.canonical_path
|
||||
except AliasResolutionError:
|
||||
return False
|
||||
return True
|
||||
|
||||
@@ -1,29 +1,25 @@
|
||||
from typing import List, Optional
|
||||
from pathlib import Path
|
||||
|
||||
from docforge.models import Module, Project
|
||||
|
||||
|
||||
def discover_module_paths(
|
||||
module_name: str,
|
||||
project_root: Path | None = None,
|
||||
) -> List[str]:
|
||||
...
|
||||
|
||||
) -> list[str]: ...
|
||||
|
||||
class GriffeLoader:
|
||||
"""Griffe-based introspection loaders.
|
||||
|
||||
This is the only supported introspection backend in doc-forge.
|
||||
Converts Griffe results into `Project`, `Module`, and `DocObject` models.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None: ...
|
||||
|
||||
def load_project(
|
||||
self,
|
||||
module_paths: List[str],
|
||||
project_name: Optional[str] = ...,
|
||||
skip_import_errors: bool = ...,
|
||||
module_paths: list[str],
|
||||
project_name: str | None = ...,
|
||||
skip_import_errors: bool | None = ...,
|
||||
) -> Project:
|
||||
"""Load a documentation project from Python modules."""
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from .project import Project
|
||||
from .module import Module
|
||||
from .object import DocObject
|
||||
from .project import Project
|
||||
|
||||
__all__ = [
|
||||
"Project",
|
||||
|
||||
@@ -7,9 +7,16 @@ This module defines the `Module` class used in the doc-forge documentation
|
||||
model. A `Module` acts as a container for top-level documented objects
|
||||
(classes, functions, variables, and other members) discovered during
|
||||
introspection.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- Only public members are stored; private names are filtered by the loader.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from typing import Dict, Iterable, Optional
|
||||
from collections.abc import Iterable
|
||||
|
||||
from docforge.models.object import DocObject
|
||||
|
||||
@@ -26,17 +33,17 @@ class Module:
|
||||
path (str):
|
||||
Dotted import path of the module.
|
||||
|
||||
docstring (Optional[str]):
|
||||
docstring (str | None):
|
||||
Module-level documentation string, if present.
|
||||
|
||||
members (Dict[str, DocObject]):
|
||||
members (dict[str, DocObject]):
|
||||
Mapping of object names to their corresponding `DocObject` representations.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
path: str,
|
||||
docstring: Optional[str] = None,
|
||||
docstring: str | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize a Module instance.
|
||||
@@ -45,12 +52,12 @@ class Module:
|
||||
path (str):
|
||||
Dotted import path identifying the module.
|
||||
|
||||
docstring (Optional[str]):
|
||||
docstring (str | None):
|
||||
Module-level documentation text, if available.
|
||||
"""
|
||||
self.path = path
|
||||
self.docstring = docstring
|
||||
self.members: Dict[str, DocObject] = {}
|
||||
self.members: dict[str, DocObject] = {}
|
||||
|
||||
def add_object(self, obj: DocObject) -> None:
|
||||
"""
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
from typing import Dict, Iterable, Optional
|
||||
from collections.abc import Iterable
|
||||
|
||||
from docforge.models.object import DocObject
|
||||
|
||||
|
||||
class Module:
|
||||
"""Represents a documented Python module."""
|
||||
|
||||
path: str
|
||||
docstring: Optional[str]
|
||||
members: Dict[str, DocObject]
|
||||
docstring: str | None
|
||||
members: dict[str, DocObject]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
path: str,
|
||||
docstring: Optional[str] = ...,
|
||||
docstring: str | None = ...,
|
||||
) -> None: ...
|
||||
|
||||
def add_object(self, obj: DocObject) -> None: ...
|
||||
|
||||
def get_object(self, name: str) -> DocObject: ...
|
||||
|
||||
def get_all_objects(self) -> Iterable[DocObject]: ...
|
||||
|
||||
@@ -7,9 +7,17 @@ This module defines the `DocObject` class, the fundamental recursive unit of
|
||||
the doc-forge documentation model. Each `DocObject` represents a Python
|
||||
entity such as a class, function, method, or attribute, and may contain nested
|
||||
members that form a hierarchical documentation structure.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- `DocObject` instances form a tree mirroring the Python import hierarchy.
|
||||
- Objects are renderer-agnostic and may be consumed by any renderer.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from typing import Dict, Iterable, Optional
|
||||
from collections.abc import Iterable
|
||||
|
||||
|
||||
class DocObject:
|
||||
@@ -30,13 +38,13 @@ class DocObject:
|
||||
path (str):
|
||||
Fully qualified dotted path to the object.
|
||||
|
||||
signature (Optional[str]):
|
||||
signature (str | None):
|
||||
Callable signature if the object represents a callable.
|
||||
|
||||
docstring (Optional[str]):
|
||||
docstring (str | None):
|
||||
Raw docstring text extracted from the source code.
|
||||
|
||||
members (Dict[str, DocObject]):
|
||||
members (dict[str, DocObject]):
|
||||
Mapping of member names to child `DocObject` instances.
|
||||
"""
|
||||
|
||||
@@ -45,8 +53,8 @@ class DocObject:
|
||||
name: str,
|
||||
kind: str,
|
||||
path: str,
|
||||
signature: Optional[str] = None,
|
||||
docstring: Optional[str] = None,
|
||||
signature: str | None = None,
|
||||
docstring: str | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize a DocObject instance.
|
||||
@@ -61,10 +69,10 @@ class DocObject:
|
||||
path (str):
|
||||
Fully qualified dotted path of the object.
|
||||
|
||||
signature (Optional[str]):
|
||||
signature (str | None):
|
||||
Callable signature if applicable.
|
||||
|
||||
docstring (Optional[str]):
|
||||
docstring (str | None):
|
||||
Documentation string associated with the object.
|
||||
"""
|
||||
self.name = name
|
||||
@@ -72,7 +80,7 @@ class DocObject:
|
||||
self.path = path
|
||||
self.signature = signature
|
||||
self.docstring = docstring
|
||||
self.members: Dict[str, "DocObject"] = {}
|
||||
self.members: dict[str, DocObject] = {}
|
||||
|
||||
def add_member(self, obj: "DocObject") -> None:
|
||||
"""
|
||||
@@ -82,7 +90,8 @@ class DocObject:
|
||||
nested objects to their parent containers.
|
||||
|
||||
Args:
|
||||
obj: Documentation object to add as a member.
|
||||
obj (DocObject):
|
||||
Documentation object to add as a member.
|
||||
"""
|
||||
self.members[obj.name] = obj
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
from typing import Dict, Iterable, Optional
|
||||
|
||||
from collections.abc import Iterable
|
||||
|
||||
class DocObject:
|
||||
"""Represents a documented Python object."""
|
||||
@@ -7,21 +6,18 @@ class DocObject:
|
||||
name: str
|
||||
kind: str
|
||||
path: str
|
||||
signature: Optional[str]
|
||||
docstring: Optional[str]
|
||||
members: Dict[str, "DocObject"]
|
||||
signature: str | None
|
||||
docstring: str | None
|
||||
members: dict[str, DocObject]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
kind: str,
|
||||
path: str,
|
||||
signature: Optional[str] = ...,
|
||||
docstring: Optional[str] = ...,
|
||||
signature: str | None = ...,
|
||||
docstring: str | None = ...,
|
||||
) -> None: ...
|
||||
|
||||
def add_member(self, obj: "DocObject") -> None: ...
|
||||
|
||||
def get_member(self, name: str) -> "DocObject": ...
|
||||
|
||||
def get_all_members(self) -> Iterable["DocObject"]: ...
|
||||
def add_member(self, obj: DocObject) -> None: ...
|
||||
def get_member(self, name: str) -> DocObject: ...
|
||||
def get_all_members(self) -> Iterable[DocObject]: ...
|
||||
|
||||
@@ -6,9 +6,17 @@ Documentation model representing a project.
|
||||
This module defines the `Project` class, the top-level container used by
|
||||
doc-forge to represent a documented codebase. A `Project` aggregates multiple
|
||||
modules and provides access to them through a unified interface.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- Modules are keyed by their dotted import path.
|
||||
- Objects are renderer-agnostic; the same model feeds every renderer.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from typing import Dict, Iterable
|
||||
from collections.abc import Iterable
|
||||
|
||||
from docforge.models.module import Module
|
||||
|
||||
@@ -24,7 +32,7 @@ class Project:
|
||||
name (str):
|
||||
Name of the project.
|
||||
|
||||
modules (Dict[str, Module]):
|
||||
modules (dict[str, Module]):
|
||||
Mapping of module paths to `Module` instances.
|
||||
"""
|
||||
|
||||
@@ -35,9 +43,17 @@ class Project:
|
||||
Args:
|
||||
name (str):
|
||||
Name used to identify the documentation project.
|
||||
|
||||
Example:
|
||||
Create a project and register a module:
|
||||
|
||||
```python
|
||||
project = Project("mypackage")
|
||||
project.add_module(module)
|
||||
```
|
||||
"""
|
||||
self.name = name
|
||||
self.modules: Dict[str, Module] = {}
|
||||
self.modules: dict[str, Module] = {}
|
||||
|
||||
def add_module(self, module: Module) -> None:
|
||||
"""
|
||||
@@ -85,4 +101,4 @@ class Project:
|
||||
list[str]:
|
||||
A list containing the dotted paths of all modules in the project.
|
||||
"""
|
||||
return list(self.modules.keys())
|
||||
return list(self.modules.keys())
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
from typing import Dict, Iterable
|
||||
from collections.abc import Iterable
|
||||
|
||||
from docforge.models.module import Module
|
||||
|
||||
|
||||
class Project:
|
||||
"""Represents a documentation project."""
|
||||
|
||||
name: str
|
||||
modules: Dict[str, Module]
|
||||
modules: dict[str, Module]
|
||||
|
||||
def __init__(self, name: str) -> None: ...
|
||||
|
||||
def add_module(self, module: Module) -> None: ...
|
||||
|
||||
def get_module(self, path: str) -> Module: ...
|
||||
|
||||
def get_all_modules(self) -> Iterable[Module]: ...
|
||||
|
||||
def get_module_list(self) -> list[str]: ...
|
||||
|
||||
@@ -26,11 +26,13 @@ independent of module hierarchy.
|
||||
from .spec import NavSpec, load_nav_spec
|
||||
from .resolver import ResolvedNav, resolve_nav
|
||||
from .mkdocs import MkDocsNavEmitter
|
||||
from .wiki import build_wiki_nav
|
||||
|
||||
__all__ = [
|
||||
"NavSpec",
|
||||
"ResolvedNav",
|
||||
"MkDocsNavEmitter",
|
||||
"build_wiki_nav",
|
||||
"resolve_nav",
|
||||
"load_nav_spec",
|
||||
]
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
from .spec import NavSpec, load_nav_spec
|
||||
from .resolver import ResolvedNav, resolve_nav
|
||||
from .mkdocs import MkDocsNavEmitter
|
||||
from .resolver import ResolvedNav, resolve_nav
|
||||
from .spec import NavSpec, load_nav_spec
|
||||
from .wiki import build_wiki_nav
|
||||
|
||||
__all__ = [
|
||||
"NavSpec",
|
||||
"ResolvedNav",
|
||||
"MkDocsNavEmitter",
|
||||
"build_wiki_nav",
|
||||
"resolve_nav",
|
||||
"load_nav_spec",
|
||||
]
|
||||
|
||||
@@ -4,10 +4,18 @@ MkDocs navigation emitter.
|
||||
This module provides the ``MkDocsNavEmitter`` class, which converts a
|
||||
``ResolvedNav`` instance into the navigation structure required by the
|
||||
MkDocs ``nav`` configuration.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- The emitted structure is a list of dictionaries, one per top-level nav
|
||||
entry, matching the MkDocs ``nav`` YAML format.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any
|
||||
from typing import Any
|
||||
|
||||
from docforge.nav.resolver import ResolvedNav
|
||||
|
||||
@@ -20,20 +28,22 @@ class MkDocsNavEmitter:
|
||||
list structure expected by the MkDocs ``nav`` configuration field.
|
||||
"""
|
||||
|
||||
def emit(self, nav: ResolvedNav) -> List[Dict[str, Any]]:
|
||||
def emit(self, nav: ResolvedNav) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Generate a navigation structure for ``mkdocs.yml``.
|
||||
|
||||
Args:
|
||||
nav: Resolved navigation data describing documentation groups
|
||||
nav (ResolvedNav):
|
||||
Resolved navigation data describing documentation groups
|
||||
and their associated Markdown files.
|
||||
|
||||
Returns:
|
||||
A list of dictionaries representing the MkDocs navigation layout.
|
||||
Each dictionary maps a navigation label to a page or a list of
|
||||
pages.
|
||||
list[dict[str, Any]]:
|
||||
A list of dictionaries representing the MkDocs navigation layout.
|
||||
Each dictionary maps a navigation label to a page or a list of
|
||||
pages.
|
||||
"""
|
||||
result: List[Dict[str, Any]] = []
|
||||
result: list[dict[str, Any]] = []
|
||||
|
||||
# Home entry (semantic path)
|
||||
if nav.home:
|
||||
@@ -41,7 +51,7 @@ class MkDocsNavEmitter:
|
||||
|
||||
# Group entries
|
||||
for group, paths in nav.groups.items():
|
||||
entries: List[str] = []
|
||||
entries: list[str] = []
|
||||
for p in paths:
|
||||
# Convert filesystem path back to docs-relative path
|
||||
rel_path = self._to_relative(p, nav._docs_root)
|
||||
@@ -59,11 +69,14 @@ class MkDocsNavEmitter:
|
||||
resulting path is relative to the documentation root.
|
||||
|
||||
Args:
|
||||
path: Filesystem path to convert.
|
||||
docs_root: Root directory of the documentation sources.
|
||||
path (Path):
|
||||
Filesystem path to convert.
|
||||
docs_root (Path | None):
|
||||
Root directory of the documentation sources.
|
||||
|
||||
Returns:
|
||||
POSIX-style path relative to the documentation root.
|
||||
str:
|
||||
POSIX-style path relative to the documentation root.
|
||||
"""
|
||||
if docs_root and path.is_absolute():
|
||||
try:
|
||||
@@ -75,7 +88,7 @@ class MkDocsNavEmitter:
|
||||
path_str = path.as_posix()
|
||||
docs_root_str = docs_root.as_posix()
|
||||
if path_str.startswith(docs_root_str + "/"):
|
||||
return path_str[len(docs_root_str) + 1:]
|
||||
return path_str[len(docs_root_str) + 1 :]
|
||||
|
||||
# Fallback for other cases
|
||||
return path.as_posix().split("/docs/", 1)[-1]
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
from typing import Dict, List, Any
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from docforge.nav.resolver import ResolvedNav
|
||||
|
||||
|
||||
class MkDocsNavEmitter:
|
||||
"""
|
||||
Converts a ResolvedNav into MkDocs-compatible `nav` data.
|
||||
"""
|
||||
|
||||
def emit(self, nav: ResolvedNav) -> List[Dict[str, Any]]:
|
||||
def emit(self, nav: ResolvedNav) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Emit a structure suitable for insertion into mkdocs.yml.
|
||||
|
||||
|
||||
@@ -3,12 +3,19 @@ Navigation resolution utilities.
|
||||
|
||||
This module resolves a ``NavSpec`` against the filesystem by expanding glob
|
||||
patterns and validating that referenced documentation files exist.
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- Glob resolution is recursive and returns paths in sorted order.
|
||||
- Unmatched patterns raise ``FileNotFoundError`` to fail fast on typos.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, List
|
||||
|
||||
import glob
|
||||
from collections.abc import Iterable
|
||||
from pathlib import Path
|
||||
|
||||
from docforge.nav.spec import NavSpec
|
||||
|
||||
@@ -29,16 +36,19 @@ class ResolvedNav:
|
||||
def __init__(
|
||||
self,
|
||||
home: str | None,
|
||||
groups: Dict[str, List[Path]],
|
||||
groups: dict[str, list[Path]],
|
||||
docs_root: Path | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize a ResolvedNav instance.
|
||||
|
||||
Args:
|
||||
home: Relative path to the home page within the documentation root.
|
||||
groups: Mapping of group titles to resolved documentation file paths.
|
||||
docs_root: Root directory of the documentation source files.
|
||||
home (str | None):
|
||||
Relative path to the home page within the documentation root.
|
||||
groups (dict[str, list[Path]]):
|
||||
Mapping of group titles to resolved documentation file paths.
|
||||
docs_root (Path | None):
|
||||
Root directory of the documentation source files.
|
||||
"""
|
||||
self.home = home
|
||||
self.groups = groups
|
||||
@@ -48,8 +58,10 @@ class ResolvedNav:
|
||||
"""
|
||||
Iterate over all files referenced by the navigation structure.
|
||||
|
||||
Returns:
|
||||
An iterable of ``Path`` objects representing documentation files.
|
||||
Yields:
|
||||
Path:
|
||||
A documentation file referenced by the navigation, including
|
||||
the home page when defined.
|
||||
|
||||
Raises:
|
||||
RuntimeError: If the home page is defined but the documentation
|
||||
@@ -61,8 +73,7 @@ class ResolvedNav:
|
||||
yield self._docs_root / self.home
|
||||
|
||||
for paths in self.groups.values():
|
||||
for p in paths:
|
||||
yield p
|
||||
yield from paths
|
||||
|
||||
|
||||
def resolve_nav(
|
||||
@@ -76,11 +87,14 @@ def resolve_nav(
|
||||
that referenced documentation files exist within the documentation root.
|
||||
|
||||
Args:
|
||||
spec: Navigation specification describing documentation layout.
|
||||
docs_root: Root directory containing documentation Markdown files.
|
||||
spec (NavSpec):
|
||||
Navigation specification describing documentation layout.
|
||||
docs_root (Path):
|
||||
Root directory containing documentation Markdown files.
|
||||
|
||||
Returns:
|
||||
A ``ResolvedNav`` instance containing validated navigation paths.
|
||||
ResolvedNav:
|
||||
A `ResolvedNav` instance containing validated navigation paths.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If the documentation root does not exist or a
|
||||
@@ -89,23 +103,23 @@ def resolve_nav(
|
||||
if not docs_root.exists():
|
||||
raise FileNotFoundError(docs_root)
|
||||
|
||||
def resolve_pattern(pattern: str) -> List[Path]:
|
||||
def resolve_pattern(pattern: str) -> list[Path]:
|
||||
"""
|
||||
Resolve a glob pattern relative to the documentation root.
|
||||
|
||||
Args:
|
||||
pattern: Glob pattern used to match documentation files.
|
||||
pattern (str):
|
||||
Glob pattern used to match documentation files.
|
||||
|
||||
Returns:
|
||||
A sorted list of matching ``Path`` objects.
|
||||
list[Path]:
|
||||
A sorted list of matching `Path` objects.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If the pattern does not match any files.
|
||||
"""
|
||||
full = docs_root / pattern
|
||||
matches = sorted(
|
||||
Path(p) for p in glob.glob(str(full), recursive=True)
|
||||
)
|
||||
matches = sorted(Path(p) for p in glob.glob(str(full), recursive=True))
|
||||
|
||||
if not matches:
|
||||
raise FileNotFoundError(pattern)
|
||||
@@ -121,10 +135,10 @@ def resolve_nav(
|
||||
home = spec.home
|
||||
|
||||
# Resolve navigation groups
|
||||
resolved_groups: Dict[str, List[Path]] = {}
|
||||
resolved_groups: dict[str, list[Path]] = {}
|
||||
|
||||
for group, patterns in spec.groups.items():
|
||||
files: List[Path] = []
|
||||
files: list[Path] = []
|
||||
for pattern in patterns:
|
||||
files.extend(resolve_pattern(pattern))
|
||||
resolved_groups[group] = files
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
from collections.abc import Iterable
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Iterable, Optional
|
||||
|
||||
from docforge.nav.spec import NavSpec
|
||||
|
||||
|
||||
class ResolvedNav:
|
||||
"""
|
||||
Fully-resolved navigation tree.
|
||||
@@ -13,17 +12,16 @@ class ResolvedNav:
|
||||
- Order is preserved
|
||||
"""
|
||||
|
||||
home: Optional[str]
|
||||
groups: Dict[str, List[Path]]
|
||||
_docs_root: Optional[Path]
|
||||
home: str | None
|
||||
groups: dict[str, list[Path]]
|
||||
_docs_root: Path | None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
home: str | None,
|
||||
groups: Dict[str, List[Path]],
|
||||
groups: dict[str, list[Path]],
|
||||
docs_root: Path | None = ...,
|
||||
) -> None: ...
|
||||
|
||||
def all_files(self) -> Iterable[Path]:
|
||||
"""
|
||||
Return all resolved documentation files in nav order.
|
||||
@@ -33,7 +31,6 @@ class ResolvedNav:
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def resolve_nav(
|
||||
spec: NavSpec,
|
||||
docs_root: Path,
|
||||
|
||||
@@ -4,10 +4,20 @@ Navigation specification model.
|
||||
This module defines the ``NavSpec`` class, which represents the navigation
|
||||
structure defined by the user in the doc-forge navigation specification
|
||||
(typically ``docforge.nav.yml``).
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- The spec file supports an optional ``icon`` mapping for MkDocs theme
|
||||
customization.
|
||||
- All file references in ``groups`` are relative to the documentation root.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
import yaml
|
||||
|
||||
@@ -24,34 +34,46 @@ class NavSpec:
|
||||
``index.md``).
|
||||
groups: Mapping of navigation group titles to lists of file patterns
|
||||
or glob expressions.
|
||||
icon: Optional mapping of theme icon entries (for example
|
||||
``{"logo": "material/code-tags"}``) injected into the MkDocs
|
||||
theme as ``theme.icon``.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
home: Optional[str],
|
||||
groups: Dict[str, List[str]],
|
||||
home: str | None,
|
||||
groups: dict[str, list[str]],
|
||||
icon: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize a NavSpec instance.
|
||||
|
||||
Args:
|
||||
home: Relative path to the home document.
|
||||
groups: Mapping of group names to lists of path patterns
|
||||
home (str | None):
|
||||
Relative path to the home document.
|
||||
groups (dict[str, list[str]]):
|
||||
Mapping of group names to lists of path patterns
|
||||
(glob expressions).
|
||||
icon (dict[str, str] | None):
|
||||
Optional mapping of theme icon entries applied to the
|
||||
generated MkDocs configuration.
|
||||
"""
|
||||
self.home = home
|
||||
self.groups = groups
|
||||
self.icon = icon
|
||||
|
||||
@classmethod
|
||||
def load(cls, path: Path) -> "NavSpec":
|
||||
def load(cls, path: Path) -> NavSpec:
|
||||
"""
|
||||
Load a navigation specification from a YAML file.
|
||||
|
||||
Args:
|
||||
path: Filesystem path to the navigation specification file.
|
||||
path (Path):
|
||||
Filesystem path to the navigation specification file.
|
||||
|
||||
Returns:
|
||||
A ``NavSpec`` instance representing the parsed configuration.
|
||||
NavSpec:
|
||||
A ``NavSpec`` instance representing the parsed configuration.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If the specified file does not exist.
|
||||
@@ -68,6 +90,7 @@ class NavSpec:
|
||||
|
||||
home = data.get("home")
|
||||
groups = data.get("groups", {})
|
||||
icon = data.get("icon")
|
||||
|
||||
if home is not None and not isinstance(home, str):
|
||||
raise ValueError("home must be a string")
|
||||
@@ -83,17 +106,26 @@ class NavSpec:
|
||||
):
|
||||
raise ValueError(f"group '{key}' must be a list of strings")
|
||||
|
||||
return cls(home=home, groups=groups)
|
||||
if icon is not None and (
|
||||
not isinstance(icon, dict)
|
||||
or not all(
|
||||
isinstance(k, str) and isinstance(v, str) for k, v in icon.items()
|
||||
)
|
||||
):
|
||||
raise ValueError("icon must be a mapping of strings")
|
||||
|
||||
def all_patterns(self) -> List[str]:
|
||||
return cls(home=home, groups=groups, icon=icon)
|
||||
|
||||
def all_patterns(self) -> list[str]:
|
||||
"""
|
||||
Return all path patterns referenced by the specification.
|
||||
|
||||
Returns:
|
||||
A list containing the home document (if defined) and all
|
||||
group pattern entries.
|
||||
list[str]:
|
||||
A list containing the home document (if defined) and all
|
||||
group pattern entries.
|
||||
"""
|
||||
patterns: List[str] = []
|
||||
patterns: list[str] = []
|
||||
|
||||
if self.home:
|
||||
patterns.append(self.home)
|
||||
@@ -112,10 +144,12 @@ def load_nav_spec(path: Path) -> NavSpec:
|
||||
corresponding ``NavSpec`` instance.
|
||||
|
||||
Args:
|
||||
path: Path to the navigation specification file.
|
||||
path (Path):
|
||||
Path to the navigation specification file.
|
||||
|
||||
Returns:
|
||||
A ``NavSpec`` instance representing the parsed specification.
|
||||
NavSpec:
|
||||
A ``NavSpec`` instance representing the parsed specification.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If the specification file does not exist.
|
||||
@@ -132,4 +166,5 @@ def load_nav_spec(path: Path) -> NavSpec:
|
||||
return NavSpec(
|
||||
home=data.get("home"),
|
||||
groups=data.get("groups", {}),
|
||||
icon=data.get("icon"),
|
||||
)
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
|
||||
class NavSpec:
|
||||
"""
|
||||
@@ -10,18 +8,16 @@ class NavSpec:
|
||||
of filesystem structure or MkDocs specifics.
|
||||
"""
|
||||
|
||||
home: Optional[str]
|
||||
groups: Dict[str, List[str]]
|
||||
home: str | None
|
||||
groups: dict[str, list[str]]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
home: Optional[str],
|
||||
groups: Dict[str, List[str]],
|
||||
) -> None:
|
||||
...
|
||||
|
||||
home: str | None,
|
||||
groups: dict[str, list[str]],
|
||||
) -> None: ...
|
||||
@classmethod
|
||||
def load(cls, path: Path) -> "NavSpec":
|
||||
def load(cls, path: Path) -> NavSpec:
|
||||
"""
|
||||
Load and validate a nav specification from YAML.
|
||||
|
||||
@@ -31,12 +27,11 @@ class NavSpec:
|
||||
"""
|
||||
...
|
||||
|
||||
def all_patterns(self) -> List[str]:
|
||||
def all_patterns(self) -> list[str]:
|
||||
"""
|
||||
Return all path patterns referenced by the spec
|
||||
(including home and group entries).
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
def load_nav_spec(path: Path) -> NavSpec: ...
|
||||
|
||||
165
docforge/nav/wiki.py
Normal file
165
docforge/nav/wiki.py
Normal file
@@ -0,0 +1,165 @@
|
||||
"""
|
||||
# Summary
|
||||
|
||||
Wiki navigation derivation.
|
||||
|
||||
This module provides ``build_wiki_nav``, which derives an MkDocs-ready
|
||||
navigation block from the file structure of a hand-written wiki directory
|
||||
(typically ``docs/wiki``). wiki content is authored by hand and is never
|
||||
modified by doc-forge; only the navigation layout is inferred.
|
||||
|
||||
# Notes
|
||||
|
||||
- ``index.md`` at the wiki root becomes the ``Home`` entry.
|
||||
- Page labels are derived from filenames: numeric order prefixes such as
|
||||
``01_`` or ``02-`` are stripped, separators are replaced with spaces, and
|
||||
names are title-cased (``01_overview.md`` becomes ``Overview``).
|
||||
- Subdirectories become nested navigation groups. A nested ``index.md`` is
|
||||
rendered as the section root placed first inside the group.
|
||||
- Only ``.md`` files are considered; hidden entries are ignored.
|
||||
"""
|
||||
|
||||
import re
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
def build_wiki_nav(wiki_dir: Path) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Derive an MkDocs navigation block from a wiki directory.
|
||||
|
||||
Returned paths are relative to the parent of ``wiki_dir`` and carry the
|
||||
wiki directory name as their leading component (for example
|
||||
``wiki/01_overview.md`` when the wiki lives at ``docs/wiki``). This makes
|
||||
the result directly usable in an MkDocs ``nav`` block with
|
||||
|
||||
- ``index.md`` at the wiki root becomes the ``Home`` entry.
|
||||
- Page labels are derived from filenames: numeric order prefixes such as
|
||||
``01_`` or ``02-`` are stripped, separators are replaced with spaces, and
|
||||
names are title-cased (``01_overview.md`` becomes ``Overview``).
|
||||
- Subdirectories become nested navigation groups. A nested ``index.md`` is
|
||||
rendered as the section root placed first inside the group.
|
||||
- Only ``.md`` files are considered; hidden entries are ignored.
|
||||
|
||||
Args:
|
||||
wiki_dir (Path):
|
||||
Path to the hand-written wiki directory, for example ``docs/wiki``.
|
||||
|
||||
Returns:
|
||||
list[dict[str, Any]]:
|
||||
Navigation entries compatible with the MkDocs ``nav`` configuration.
|
||||
The list is empty if the wiki contains no Markdown files.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError:
|
||||
If the wiki directory does not exist.
|
||||
"""
|
||||
if not wiki_dir.exists():
|
||||
raise FileNotFoundError(wiki_dir)
|
||||
|
||||
root = wiki_dir.parent
|
||||
|
||||
def rel(path: Path) -> str:
|
||||
try:
|
||||
return path.relative_to(root).as_posix()
|
||||
except ValueError:
|
||||
return path.as_posix()
|
||||
|
||||
nav: list[dict[str, Any]] = []
|
||||
|
||||
if (wiki_dir / "index.md").exists():
|
||||
nav.append({"Home": rel(wiki_dir / "index.md")})
|
||||
|
||||
nav.extend(_render_entries(wiki_dir, rel))
|
||||
|
||||
return nav
|
||||
|
||||
|
||||
def _render_entries(
|
||||
base_dir: Path,
|
||||
rel: Callable[[Path], str],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Render navigation entries for the children of a wiki directory.
|
||||
|
||||
Markdown pages become labeled entries, subdirectories containing
|
||||
Markdown become nested groups, and a nested ``index.md`` is emitted
|
||||
first as the section root.
|
||||
|
||||
Args:
|
||||
base_dir (Path):
|
||||
Directory whose children are rendered.
|
||||
|
||||
rel (Callable[[Path], str]):
|
||||
Callable converting a wiki file path into a docs-relative path.
|
||||
|
||||
Returns:
|
||||
list[dict[str, Any]]:
|
||||
Navigation entries for ``base_dir`` in natural sort order.
|
||||
"""
|
||||
children = sorted(
|
||||
(child for child in base_dir.iterdir() if not child.name.startswith(".")),
|
||||
key=lambda child: _natural_key(child.name),
|
||||
)
|
||||
|
||||
entries: list[dict[str, Any]] = []
|
||||
|
||||
for child in children:
|
||||
if child.is_dir():
|
||||
if not any(child.rglob("*.md")):
|
||||
continue
|
||||
|
||||
group: list[dict[str, Any]] = []
|
||||
nested_index = child / "index.md"
|
||||
if nested_index.exists():
|
||||
group.append({_prettify(child.name): rel(nested_index)})
|
||||
group.extend(_render_entries(child, rel))
|
||||
entries.append({_prettify(child.name): group})
|
||||
elif child.suffix == ".md":
|
||||
if child.name == "index.md":
|
||||
continue
|
||||
entries.append({_prettify(child.stem): rel(child)})
|
||||
|
||||
return entries
|
||||
|
||||
|
||||
def _prettify(name: str) -> str:
|
||||
"""
|
||||
Convert a filename or directory name into a navigation label.
|
||||
|
||||
Numeric order prefixes (``01_``, ``02-``, ``03.``) are stripped,
|
||||
separators are replaced with spaces, and the result is title-cased.
|
||||
|
||||
Args:
|
||||
name (str):
|
||||
Filename stem or directory name to prettify.
|
||||
|
||||
Returns:
|
||||
str:
|
||||
Title-cased navigation label.
|
||||
"""
|
||||
name = re.sub(r"^\d+[_\-. ]?", "", name)
|
||||
name = re.sub(r"[_-]+", " ", name).strip()
|
||||
return name.title()
|
||||
|
||||
|
||||
def _natural_key(name: str) -> list[object]:
|
||||
"""
|
||||
Build a natural sort key from a filename.
|
||||
|
||||
The key splits ``name`` into alternating non-digit and digit parts so
|
||||
that numeric prefixes sort numerically (``02`` before ``10``).
|
||||
|
||||
Args:
|
||||
name (str):
|
||||
Filename or directory name to key.
|
||||
|
||||
Returns:
|
||||
list[object]:
|
||||
Mixed list of lowercased strings and integers used for sorting.
|
||||
"""
|
||||
return [
|
||||
int(part) if part.isdigit() else part.lower()
|
||||
for part in re.split(r"(\d+)", name)
|
||||
]
|
||||
41
docforge/nav/wiki.pyi
Normal file
41
docforge/nav/wiki.pyi
Normal file
@@ -0,0 +1,41 @@
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
def build_wiki_nav(wiki_dir: Path) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Derive an MkDocs navigation block from a wiki directory.
|
||||
|
||||
Args:
|
||||
wiki_dir (Path):
|
||||
Path to the hand-written wiki directory.
|
||||
|
||||
Returns:
|
||||
list[dict[str, Any]]:
|
||||
Wiki navigation entries compatible with the MkDocs `nav`
|
||||
configuration.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError:
|
||||
If the wiki directory does not exist.
|
||||
"""
|
||||
...
|
||||
|
||||
def _render_entries(
|
||||
base_dir: Path,
|
||||
rel: Callable[[Path], str],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Render navigation entries for the children of a wiki directory.
|
||||
|
||||
Args:
|
||||
base_dir (Path):
|
||||
Directory whose children are rendered.
|
||||
rel (Callable[[Path], str]):
|
||||
Callable converting a wiki file path into a docs-relative path.
|
||||
|
||||
Returns:
|
||||
list[dict[str, Any]]:
|
||||
Navigation entries for `base_dir` in natural sort order.
|
||||
"""
|
||||
...
|
||||
0
docforge/py.typed
Normal file
0
docforge/py.typed
Normal file
@@ -1,5 +1,5 @@
|
||||
from .mkdocs_renderer import MkDocsRenderer
|
||||
from .mcp_renderer import MCPRenderer
|
||||
from .mkdocs_renderer import MkDocsRenderer
|
||||
|
||||
__all__ = [
|
||||
"MkDocsRenderer",
|
||||
|
||||
@@ -3,7 +3,6 @@ from typing import Protocol
|
||||
|
||||
from docforge.models import Project
|
||||
|
||||
|
||||
class RendererConfig:
|
||||
"""Renderer configuration container."""
|
||||
|
||||
@@ -12,7 +11,6 @@ class RendererConfig:
|
||||
|
||||
def __init__(self, out_dir: Path, project: Project) -> None: ...
|
||||
|
||||
|
||||
class DocRenderer(Protocol):
|
||||
"""Renderer interface."""
|
||||
|
||||
|
||||
@@ -9,9 +9,8 @@ resources compatible with the Model Context Protocol (MCP).
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
from docforge.models import Project, Module, DocObject
|
||||
from docforge.models import DocObject, Module, Project
|
||||
|
||||
|
||||
class MCPRenderer:
|
||||
@@ -42,15 +41,17 @@ class MCPRenderer:
|
||||
modules_dir = out_dir / "modules"
|
||||
modules_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
nav: List[Dict[str, str]] = []
|
||||
nav: list[dict[str, str]] = []
|
||||
|
||||
for module in project.get_all_modules():
|
||||
self._write_module(module, modules_dir)
|
||||
|
||||
nav.append({
|
||||
"module": module.path,
|
||||
"resource": f"doc://modules/{module.path}",
|
||||
})
|
||||
nav.append(
|
||||
{
|
||||
"module": module.path,
|
||||
"resource": f"docs://modules/{module.path}",
|
||||
}
|
||||
)
|
||||
|
||||
# Write nav.json
|
||||
(out_dir / "nav.json").write_text(
|
||||
@@ -91,7 +92,7 @@ class MCPRenderer:
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
out.write_text(self._json(payload), encoding="utf-8")
|
||||
|
||||
def _render_module(self, module: Module) -> Dict:
|
||||
def _render_module(self, module: Module) -> dict:
|
||||
"""
|
||||
Convert a Module model into MCP-compatible structured data.
|
||||
|
||||
@@ -100,10 +101,10 @@ class MCPRenderer:
|
||||
Module instance to convert.
|
||||
|
||||
Returns:
|
||||
Dict:
|
||||
dict:
|
||||
Dictionary representing the module and its documented objects.
|
||||
"""
|
||||
data: Dict = {
|
||||
data: dict = {
|
||||
"path": module.path,
|
||||
"docstring": module.docstring,
|
||||
"objects": {},
|
||||
@@ -114,7 +115,7 @@ class MCPRenderer:
|
||||
|
||||
return data
|
||||
|
||||
def _render_object(self, obj: DocObject) -> Dict:
|
||||
def _render_object(self, obj: DocObject) -> dict:
|
||||
"""
|
||||
Recursively convert a DocObject into structured MCP data.
|
||||
|
||||
@@ -123,10 +124,10 @@ class MCPRenderer:
|
||||
Documentation object to convert.
|
||||
|
||||
Returns:
|
||||
Dict:
|
||||
dict:
|
||||
Dictionary describing the object and any nested members.
|
||||
"""
|
||||
data: Dict = {
|
||||
data: dict = {
|
||||
"name": obj.name,
|
||||
"kind": obj.kind,
|
||||
"path": obj.path,
|
||||
@@ -137,19 +138,18 @@ class MCPRenderer:
|
||||
members = list(obj.get_all_members())
|
||||
if members:
|
||||
data["members"] = {
|
||||
member.name: self._render_object(member)
|
||||
for member in members
|
||||
member.name: self._render_object(member) for member in members
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
@staticmethod
|
||||
def _json(data: Dict) -> str:
|
||||
def _json(data: dict) -> str:
|
||||
"""
|
||||
Serialize data to formatted JSON.
|
||||
|
||||
Args:
|
||||
data (Dict):
|
||||
data (dict):
|
||||
Dictionary to serialize.
|
||||
|
||||
Returns:
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
from docforge.models import Project, Module, DocObject
|
||||
from typing import Any
|
||||
|
||||
from docforge.models import DocObject, Module, Project
|
||||
|
||||
class MCPRenderer:
|
||||
"""Renderer that emits MCP-native JSON resources from docforge models."""
|
||||
@@ -15,12 +14,12 @@ class MCPRenderer:
|
||||
def _write_module(self, module: Module, modules_dir: Path) -> None:
|
||||
"""Serialize a module into an MCP JSON resource."""
|
||||
|
||||
def _render_module(self, module: Module) -> Dict:
|
||||
def _render_module(self, module: Module) -> dict[str, Any]:
|
||||
"""Render a Module into MCP-friendly structured data."""
|
||||
|
||||
def _render_object(self, obj: DocObject) -> Dict:
|
||||
def _render_object(self, obj: DocObject) -> dict[str, Any]:
|
||||
"""Recursively render a DocObject into structured MCP data."""
|
||||
|
||||
@staticmethod
|
||||
def _json(data: Dict) -> str:
|
||||
def _json(data: dict[str, Any]) -> str:
|
||||
"""Serialize structured data to formatted JSON."""
|
||||
|
||||
@@ -16,7 +16,8 @@ The renderer ensures a consistent documentation structure by:
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from docforge.models import Project, Module
|
||||
|
||||
from docforge.models import Module, Project
|
||||
|
||||
|
||||
class MkDocsRenderer:
|
||||
@@ -53,7 +54,7 @@ class MkDocsRenderer:
|
||||
out_dir (Path):
|
||||
Directory where generated Markdown files will be written.
|
||||
|
||||
module_is_source (bool, optional):
|
||||
module_is_source (bool | None):
|
||||
If True, treat the specified module as the documentation root
|
||||
rather than nesting it inside a folder.
|
||||
"""
|
||||
@@ -65,8 +66,7 @@ class MkDocsRenderer:
|
||||
|
||||
# Detect packages (modules with children)
|
||||
packages = {
|
||||
p for p in paths
|
||||
if any(other.startswith(p + ".") for other in paths)
|
||||
p for p in paths if any(other.startswith(p + ".") for other in paths)
|
||||
}
|
||||
|
||||
for module in modules:
|
||||
@@ -82,15 +82,15 @@ class MkDocsRenderer:
|
||||
project: Project,
|
||||
docs_dir: Path,
|
||||
module_is_source: bool | None = None,
|
||||
readme_dir: Path | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Generate a `README.md` file from the root module docstring.
|
||||
|
||||
Behavior:
|
||||
|
||||
- If `module_is_source` is True, `README.md` is written to the project
|
||||
root directory.
|
||||
- If False, README generation is currently not implemented.
|
||||
Notes:
|
||||
- If `module_is_source` is True, `README.md` is written to the
|
||||
project root directory.
|
||||
- If False, README generation is currently not implemented.
|
||||
|
||||
Args:
|
||||
project (Project):
|
||||
@@ -99,15 +99,20 @@ class MkDocsRenderer:
|
||||
docs_dir (Path):
|
||||
Directory containing generated documentation sources.
|
||||
|
||||
module_is_source (bool, optional):
|
||||
module_is_source (bool | None):
|
||||
Whether the module is treated as the project source root.
|
||||
|
||||
readme_dir (Path | None):
|
||||
Directory where the generated README.md should be written.
|
||||
Defaults to the parent of `docs_dir`.
|
||||
"""
|
||||
|
||||
if not module_is_source:
|
||||
# Future: support README generation per module
|
||||
return
|
||||
|
||||
readme_path = docs_dir.parent / "README.md"
|
||||
readme_root = readme_dir if readme_dir is not None else docs_dir.parent
|
||||
readme_path = readme_root / "README.md"
|
||||
|
||||
root_module = None
|
||||
for module in project.get_all_modules():
|
||||
@@ -127,12 +132,12 @@ class MkDocsRenderer:
|
||||
str(root_module.docstring),
|
||||
)
|
||||
|
||||
content = (
|
||||
f"# {project.name}\n\n"
|
||||
f"{doc.strip()}\n"
|
||||
)
|
||||
content = f"# {project.name}\n\n" f"{doc.strip()}\n"
|
||||
|
||||
if not readme_path.exists() or readme_path.read_text(encoding="utf-8") != content:
|
||||
if (
|
||||
not readme_path.exists()
|
||||
or readme_path.read_text(encoding="utf-8") != content
|
||||
):
|
||||
readme_path.write_text(
|
||||
content,
|
||||
encoding="utf-8",
|
||||
@@ -151,7 +156,7 @@ class MkDocsRenderer:
|
||||
Project model to inspect.
|
||||
|
||||
Returns:
|
||||
Optional[Module]:
|
||||
Module | None:
|
||||
The root `Module` if found, otherwise `None`.
|
||||
"""
|
||||
for module in project.get_all_modules():
|
||||
@@ -183,7 +188,7 @@ class MkDocsRenderer:
|
||||
out_dir (Path):
|
||||
Base directory for generated documentation files.
|
||||
|
||||
module_is_source (bool, optional):
|
||||
module_is_source (bool | None):
|
||||
Whether the module acts as the documentation root directory.
|
||||
"""
|
||||
|
||||
@@ -231,10 +236,7 @@ class MkDocsRenderer:
|
||||
str:
|
||||
Markdown source containing a mkdocstrings directive.
|
||||
"""
|
||||
return (
|
||||
f"# {title}\n\n"
|
||||
f"::: {module_path}\n"
|
||||
)
|
||||
return f"# {title}\n\n" f"::: {module_path}\n"
|
||||
|
||||
def _ensure_root_index(
|
||||
self,
|
||||
@@ -255,8 +257,7 @@ class MkDocsRenderer:
|
||||
|
||||
if not root_index.exists():
|
||||
root_index.write_text(
|
||||
f"# {project.name}\n\n"
|
||||
"## Modules\n\n",
|
||||
f"# {project.name}\n\n" "## Modules\n\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from pathlib import Path
|
||||
from docforge.models import Project, Module
|
||||
|
||||
from docforge.models import Module, Project
|
||||
|
||||
class MkDocsRenderer:
|
||||
name: str
|
||||
@@ -11,14 +11,12 @@ class MkDocsRenderer:
|
||||
out_dir: Path,
|
||||
module_is_source: bool | None = None,
|
||||
) -> None: ...
|
||||
|
||||
def generate_readme(
|
||||
self,
|
||||
project: Project,
|
||||
docs_dir: Path,
|
||||
module_is_source: bool | None = None,
|
||||
) -> None:
|
||||
|
||||
) -> None: ...
|
||||
def _write_module(
|
||||
self,
|
||||
module: Module,
|
||||
@@ -26,9 +24,8 @@ class MkDocsRenderer:
|
||||
out_dir: Path,
|
||||
module_is_source: bool | None = None,
|
||||
) -> None: ...
|
||||
|
||||
def _render_markdown(self, title: str, module_path: str) -> str: ...
|
||||
|
||||
def _ensure_root_index(self, project, out_dir) -> None: ...
|
||||
|
||||
def _ensure_parent_index(self, parts, out_dir, link_target, title) -> None: ...
|
||||
def _ensure_root_index(self, project: Project, out_dir: Path) -> None: ...
|
||||
def _ensure_parent_index(
|
||||
self, parts: list[str], out_dir: Path, link_target: str, title: str
|
||||
) -> None: ...
|
||||
|
||||
@@ -5,6 +5,16 @@ MCP server implementation.
|
||||
|
||||
This module defines the `MCPServer` class, which serves pre-generated
|
||||
documentation bundles through the Model Context Protocol (MCP).
|
||||
|
||||
---
|
||||
|
||||
Notes:
|
||||
- The served bundle is generated offline by `MCPRenderer`.
|
||||
- Missing resources are reported as structured error dictionaries rather
|
||||
than raising exceptions.
|
||||
- The server exposes read-only resources and a single health-check tool.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -22,6 +32,14 @@ class MCPServer:
|
||||
|
||||
The server exposes documentation resources and diagnostic tools through
|
||||
MCP endpoints backed by JSON files generated by the MCP renderer.
|
||||
|
||||
Attributes:
|
||||
mcp_root (Path):
|
||||
Directory containing the generated MCP documentation bundle.
|
||||
|
||||
app (FastMCP):
|
||||
Underlying FastMCP application instance that registers resources
|
||||
and tools.
|
||||
"""
|
||||
|
||||
def __init__(self, mcp_root: Path, name: str) -> None:
|
||||
@@ -96,9 +114,7 @@ class MCPServer:
|
||||
|
||||
@self.app.resource("docs://modules/{module}")
|
||||
def module(module: str):
|
||||
return self._read_json(
|
||||
self.mcp_root / "modules" / f"{module}.json"
|
||||
)
|
||||
return self._read_json(self.mcp_root / "modules" / f"{module}.json")
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# MCP tools
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
from pathlib import Path
|
||||
from typing import Literal, Any
|
||||
from typing import Any, Literal
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
|
||||
class MCPServer:
|
||||
"""MCP server for serving documentation."""
|
||||
"""MCP server serving a pre-generated documentation bundle.
|
||||
|
||||
Exposes read-only documentation resources and a health-check tool.
|
||||
"""
|
||||
|
||||
mcp_root: Path
|
||||
app: FastMCP
|
||||
|
||||
def __init__(self, mcp_root: Path, name: str) -> None: ...
|
||||
|
||||
def _read_json(self, path: Path) -> Any: ...
|
||||
|
||||
def _register_resources(self) -> None: ...
|
||||
|
||||
def _register_tools(self) -> None: ...
|
||||
|
||||
def run(self, transport: Literal["stdio", "sse", "streamable-http"] = ...) -> None:
|
||||
"""Start the MCP server."""
|
||||
|
||||
16
docforge/templates/mkdocs.api.yml
Normal file
16
docforge/templates/mkdocs.api.yml
Normal file
@@ -0,0 +1,16 @@
|
||||
theme:
|
||||
features:
|
||||
- toc.integrate
|
||||
- header.autohide
|
||||
- announce.dismiss
|
||||
- footer.social
|
||||
|
||||
- content.code.select
|
||||
- content.code.line_numbers
|
||||
- content.tooltips
|
||||
|
||||
plugins:
|
||||
- search
|
||||
- swagger-ui-tag
|
||||
- neoteroi.mkdocsoad:
|
||||
use_pymdownx: true
|
||||
@@ -29,26 +29,6 @@ theme:
|
||||
- search.share
|
||||
- search.suggest
|
||||
|
||||
plugins:
|
||||
- search
|
||||
- mkdocstrings:
|
||||
handlers:
|
||||
python:
|
||||
paths: ["."]
|
||||
options:
|
||||
docstring_style: google
|
||||
show_source: false
|
||||
show_signature_annotations: true
|
||||
separate_signature: true
|
||||
merge_init_into_class: true
|
||||
inherited_members: true
|
||||
annotations_path: brief
|
||||
show_root_heading: true
|
||||
group_by_category: true
|
||||
show_category_heading: true
|
||||
show_object_full_path: false
|
||||
show_symbol_type_heading: true
|
||||
|
||||
markdown_extensions:
|
||||
- pymdownx.superfences
|
||||
- pymdownx.inlinehilite
|
||||
@@ -57,7 +37,6 @@ markdown_extensions:
|
||||
- admonition
|
||||
- pymdownx.details
|
||||
|
||||
- pymdownx.superfences
|
||||
- pymdownx.highlight:
|
||||
linenums: true
|
||||
anchor_linenums: true
|
||||
@@ -76,3 +55,6 @@ markdown_extensions:
|
||||
- pymdownx.caret
|
||||
- pymdownx.tilde
|
||||
- pymdownx.mark
|
||||
|
||||
extra_css:
|
||||
- https://unpkg.com/dracula-prism/dist/css/dracula-prism.css
|
||||
19
docforge/templates/mkdocs.lib.yml
Normal file
19
docforge/templates/mkdocs.lib.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
plugins:
|
||||
- search
|
||||
- mkdocstrings:
|
||||
handlers:
|
||||
python:
|
||||
paths: [".."]
|
||||
options:
|
||||
docstring_style: google
|
||||
show_source: false
|
||||
show_signature_annotations: true
|
||||
separate_signature: true
|
||||
merge_init_into_class: true
|
||||
inherited_members: true
|
||||
annotations_path: brief
|
||||
show_root_heading: true
|
||||
group_by_category: true
|
||||
show_category_heading: true
|
||||
show_object_full_path: false
|
||||
show_symbol_type_heading: true
|
||||
25
docforge/templates/mkdocs.wiki.yml
Normal file
25
docforge/templates/mkdocs.wiki.yml
Normal file
@@ -0,0 +1,25 @@
|
||||
theme:
|
||||
features:
|
||||
# Horizontal tab navigation (mongo-ops style)
|
||||
- navigation.tabs
|
||||
# Navigation UX
|
||||
- toc.integrate
|
||||
- header.autohide
|
||||
- announce.dismiss
|
||||
- footer.social
|
||||
# Content & Code
|
||||
- content.code.select
|
||||
- content.code.line_numbers
|
||||
- content.tooltips
|
||||
palette:
|
||||
scheme: slate
|
||||
primary: blue grey
|
||||
accent: teal
|
||||
font:
|
||||
text: Roboto
|
||||
code: JetBrains Mono
|
||||
icon:
|
||||
logo: material/database
|
||||
repo: fontawesome/brands/github
|
||||
plugins:
|
||||
- search
|
||||
Reference in New Issue
Block a user