summaryrefslogtreecommitdiffstats
path: root/g2h/gen.py
blob: 48840727fc685fcdd4c09883aa91468660556d97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from jinja2 import Environment, FileSystemLoader
import os

class Generator:
    def __init__(self, output_dir):
        self.output_dir = output_dir
        template_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'templates'))
        self.templates = Environment(loader=FileSystemLoader(template_dir))

    def _render(self, template, context, out_path):
        template = self.templates.get_template(template)
        content = template.render(context)
        out_file = os.path.join(self.output_dir, out_path)
        os.makedirs(os.path.dirname(out_file), exist_ok=True)
        with open(out_file, 'w', encoding='utf-8') as f:
            f.write(content)

    def render_overview(self, commits):
        context = {
            'commits': commits
        }
        self._render('overview.md', context, '_index.md')