Skip to content

Generator

Plugin

Plugin(generator)

Base class for static site generator plugins

Initialize plugin with generator instance

Source code in src/air/generator.py
12
13
14
def __init__(self, generator) -> None:
    """Initialize plugin with generator instance"""
    self.generator = generator

run

run()

Execute plugin functionality

Source code in src/air/generator.py
16
17
18
def run(self) -> None:
    """Execute plugin functionality"""
    pass

StaticSiteGenerator

StaticSiteGenerator(source_dir, output_dir)

Generates static sites from HTML templates

Initialize generator with source and output directories

Source code in src/air/generator.py
24
25
26
27
28
29
30
31
32
def __init__(self, source_dir: Path, output_dir: Path) -> None:
    """Initialize generator with source and output directories"""
    self.source_dir = source_dir
    self.output_dir = output_dir
    self.env = Environment(
        loader=FileSystemLoader(str(self.source_dir)),
        autoescape=select_autoescape(["html"]),
    )
    self.plugins: List[Plugin] = []

register_plugin

register_plugin(plugin)

Register a plugin class with the generator

Source code in src/air/generator.py
34
35
36
37
def register_plugin(self, plugin: type) -> None:
    """Register a plugin class with the generator"""
    plugin_instance = plugin(self)
    self.plugins.append(plugin_instance)

build

build()

Build the static site by rendering templates

Source code in src/air/generator.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def build(self) -> None:
    """Build the static site by rendering templates"""
    if self.output_dir.exists():
        shutil.rmtree(self.output_dir)
    self.output_dir.mkdir(parents=True)

    base_pattern = re.compile(r"^base.*\.html$")

    for path in self.source_dir.rglob("*.html"):
        relative_path = path.relative_to(self.source_dir)
        output_path = self.output_dir / relative_path

        # Skip rendering templates that start with "base"
        if base_pattern.match(path.name):
            continue

        output_path.parent.mkdir(parents=True, exist_ok=True)

        with open(output_path, "w", encoding="utf-8") as f:
            template = self.env.get_template(str(relative_path))
            f.write(template.render())

    for plugin in self.plugins:
        plugin.run()