Some checks failed
CI / test (push) Has been cancelled
- Fixed import sorting in cli.py, __main__.py, detectors/__init__.py, base.py, python.py, rust.py, openapi.py, models/__init__.py - Removed unused imports (sys, asyncio, Observer, Text, Parameter, ParameterIn, HTTPMethod, DocConfig, List, Optional) - Removed trailing whitespace from blank lines - Split lines exceeding 100 characters - Added missing __init__ docstrings in generators and static/templates packages
54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
{"""Markdown documentation generator."""
|
|
|
|
from pathlib import Path
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
from docgen.generators import BaseGenerator
|
|
from docgen.models import DocConfig, Endpoint
|
|
|
|
|
|
class MarkdownGenerator(BaseGenerator):
|
|
"""Generator for Markdown documentation."""
|
|
|
|
def __init__(self, config: DocConfig = None):
|
|
"""Initialize the Markdown generator."""
|
|
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()))
|