This commit is contained in:
50
src/docgen/generators/markdown.py
Normal file
50
src/docgen/generators/markdown.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Markdown documentation generator."""
|
||||
|
||||
from pathlib import Path
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from docgen.models import DocConfig, Endpoint
|
||||
from docgen.generators import BaseGenerator
|
||||
|
||||
|
||||
class MarkdownGenerator(BaseGenerator):
|
||||
"""Generator for Markdown documentation."""
|
||||
|
||||
def __init__(self, config: DocConfig = None):
|
||||
super().__init__(config)
|
||||
template_dir = Path(__file__).parent.parent / "templates"
|
||||
self.env = Environment(loader=FileSystemLoader(template_dir))
|
||||
|
||||
def generate(self, endpoints: list[Endpoint], output_dir: Path) -> Path:
|
||||
"""Generate Markdown documentation."""
|
||||
output_dir = self._ensure_output_dir(output_dir)
|
||||
|
||||
template = self.env.get_template("markdown.md.jinja2")
|
||||
grouped = self._group_endpoints(endpoints)
|
||||
|
||||
rendered = template.render(
|
||||
title=self.config.title,
|
||||
description=self.config.description,
|
||||
version=self.config.version,
|
||||
endpoints=endpoints,
|
||||
grouped_endpoints=grouped,
|
||||
)
|
||||
|
||||
readme_path = output_dir / "README.md"
|
||||
readme_path.write_text(rendered)
|
||||
|
||||
return readme_path
|
||||
|
||||
def _group_endpoints(self, endpoints: list[Endpoint]) -> dict[str, list[Endpoint]]:
|
||||
"""Group endpoints by tags or path prefixes."""
|
||||
grouped = {}
|
||||
for endpoint in endpoints:
|
||||
if endpoint.tags:
|
||||
tag = endpoint.tags[0] if endpoint.tags else "Other"
|
||||
else:
|
||||
parts = endpoint.path.strip("/").split("/")
|
||||
tag = parts[0] if parts else "Other"
|
||||
|
||||
if tag not in grouped:
|
||||
grouped[tag] = []
|
||||
grouped[tag].append(endpoint)
|
||||
return dict(sorted(grouped.items()))
|
||||
Reference in New Issue
Block a user