diff --git a/blag/blag.py b/blag/blag.py index a01a6af..86825f8 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -97,6 +97,36 @@ def get_config(configfile): return config['main'] +def environment_factory(template_dir=None, globals_=None): + """Environment factory. + + Creates a Jinja2 Environment with the default templates and + additional templates from `template_dir` loaded. If `globals` are + provided, they are attached to the environment and thus available to + all contexts. + + Parameters + ---------- + template_dir : str + globals_ : dict + + Returns + ------- + jinja2.Environment + + """ + # first we try the custom templates, and fall back the ones provided + # by blag + loaders = [] + if template_dir: + loaders.append(FileSystemLoader([template_dir])) + loaders.append(PackageLoader('blag', 'templates')) + env = Environment(loader=ChoiceLoader(loaders)) + if globals_: + env.globals = globals_ + return env + + def build(args): os.makedirs(f'{args.output_dir}', exist_ok=True) convertibles = [] @@ -124,12 +154,8 @@ def build(args): config = get_config('config.ini') - env = Environment( - loader=ChoiceLoader([ - FileSystemLoader([args.template_dir]), - PackageLoader('blag', 'templates'), - ]) - ) + env = environment_factory(args.template_dir, dict(site=config)) + page_template = env.get_template('page.html') article_template = env.get_template('article.html') archive_template = env.get_template('archive.html') diff --git a/blag/templates/archive.html b/blag/templates/archive.html index 92adb21..c680429 100644 --- a/blag/templates/archive.html +++ b/blag/templates/archive.html @@ -1,6 +1,6 @@ {% extends "base.html" %} -{% block title %}{{ title }}{% endblock %} +{% block title %}{{ site.title }}{% endblock %} {% block content %} {% for entry in archive %} diff --git a/blag/templates/base.html b/blag/templates/base.html index 96dc780..68ab717 100644 --- a/blag/templates/base.html +++ b/blag/templates/base.html @@ -4,6 +4,10 @@ + + {%- if description %} + + {% endif %} {% block title %}{% endblock %} diff --git a/tests/test_blag.py b/tests/test_blag.py index 8f9402a..fc2755f 100644 --- a/tests/test_blag.py +++ b/tests/test_blag.py @@ -99,3 +99,13 @@ author = a. u. thor config_parsed = blag.get_config(configfile) assert config_parsed['base_url'] == 'https://example.com/' + + +def test_environment_factory(): + globals_ = { + 'foo': 'bar', + 'test': 'me' + } + env = blag.environment_factory(globals_=globals_) + assert env.globals['foo'] == 'bar' + assert env.globals['test'] == 'me' diff --git a/tests/test_templates.py b/tests/test_templates.py index 3d91d2b..fa57526 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -1,14 +1,19 @@ import datetime -from jinja2 import Environment, PackageLoader import pytest +from blag import blag + @pytest.fixture def environment(): - env = Environment( - loader=PackageLoader('blag', 'templates') - ) + site = { + 'base_url': 'site base_url', + 'title': 'site title', + 'description': 'site description', + 'author': 'site author', + } + env = blag.environment_factory(globals_=dict(site=site)) yield env @@ -65,11 +70,10 @@ def test_archive(archive_template): } archive = [entry] ctx = { - 'title': 'this is the title', 'archive': archive, } result = archive_template.render(ctx) - assert 'this is the title' in result + assert 'site title' in result assert 'this is a title' in result assert '1980-05-09' in result