summaryrefslogtreecommitdiffstats
path: root/g2h/page.py
blob: 44f97ee2e429728c7473e7998e33a41499b0daa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from jinja2 import Environment, FileSystemLoader
import os

class Page:
    def __init__(self, template_file, out_dir, out_file, name):
        self.out_file_path = os.path.join(out_dir, out_file)
        self.name = name
        template_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'templates'))
        self.template = Environment(loader=FileSystemLoader(template_dir)).get_template(template_file)

    def context(self):
        raise NotImplementedError("Must override context")

    def render(self):
        content = self.template.render(self.context())
        os.makedirs(os.path.dirname(self.out_file_path), exist_ok=True)
        with open(self.out_file_path, 'w', encoding='utf-8') as f:
            f.write(content)