Skip to main content

Overview

The template engine module provides a flexible Jinja2-based rendering system for generating README files and other documentation. It supports custom template directories and provides a convenient helper function for README generation. Source: docugen/templates/engine.py

TemplateEngine

class TemplateEngine:
    def __init__(
        self,
        template_dir: str | Path | None = None
    ) -> None
Core template rendering engine using Jinja2.

Parameters

template_dir
str | Path | None
default:"None"
Custom directory containing template files. If None, uses the built-in templates directory (docugen/templates/).

Attributes

environment
jinja2.Environment
Configured Jinja2 environment with:
  • autoescape=False - No automatic HTML escaping
  • trim_blocks=True - Remove first newline after block
  • lstrip_blocks=True - Strip leading whitespace from blocks

Methods

render()

def render(
    self,
    template_name: str,
    context: dict[str, Any]
) -> str
Render a template with the given context variables.
template_name
str
required
Name of the template file to render (e.g., "default_readme.md.j2").
context
dict[str, Any]
required
Dictionary of variables to pass to the template.
return
str
Rendered template content with trailing newline.

Example

from docugen.templates.engine import TemplateEngine
from pathlib import Path

# Use default templates
engine = TemplateEngine()
readme = engine.render("default_readme.md.j2", {
    "project_name": "MyProject",
    "ai_content": "Generated documentation...",
    "summary": {"files": 42, "lines": 1337}
})

# Use custom template directory
engine = TemplateEngine(template_dir="./custom_templates")
output = engine.render("my_template.j2", {"version": "1.0.0"})

render_readme()

def render_readme(
    ai_content: str,
    project_name: str,
    summary: dict[str, Any] | None = None
) -> str
Convenience function to render a README using the default template. This function creates a TemplateEngine instance and renders the default_readme.md.j2 template with standardized context variables.

Parameters

ai_content
str
required
AI-generated documentation content. Automatically stripped of leading/trailing whitespace.
project_name
str
required
Name of the project being documented.
summary
dict[str, Any] | None
default:"None"
Optional metadata dictionary (e.g., file counts, statistics). Defaults to empty dict if not provided.

Returns

return
str
Rendered README content with trailing newline.

Example

from docugen.templates.engine import render_readme

# Basic usage
readme = render_readme(
    ai_content="This is a Python package for...",
    project_name="docugen-ai"
)

# With summary metadata
readme = render_readme(
    ai_content="Comprehensive documentation...",
    project_name="my-app",
    summary={
        "total_files": 50,
        "total_lines": 2500,
        "languages": ["Python", "TypeScript"]
    }
)

print(readme)

Constants

DEFAULT_TEMPLATE
str
default:"default_readme.md.j2"
Default template filename used by render_readme().

Template Context

When using render_readme(), the following variables are available in templates:
VariableTypeDescription
project_namestrProject name
ai_contentstrGenerated documentation (trimmed)
summarydictMetadata dictionary

Build docs developers (and LLMs) love