mirror of
https://github.com/venthur/blag.git
synced 2025-11-25 20:52:43 +00:00
added global site variables to environment
This commit is contained in:
38
blag/blag.py
38
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')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{{ title }}{% endblock %}
|
||||
{% block title %}{{ site.title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% for entry in archive %}
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta name="author" content="{{ site.author }}">
|
||||
{%- if description %}
|
||||
<meta name="description" content="{{ description }}">
|
||||
{% endif %}
|
||||
<title>{% block title %}{% endblock %}</title>
|
||||
</head>
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user