From fc7a46b7165bff7e2e206b719ed7d2b251cd24ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20G=C3=B3mez?= Date: Fri, 2 Oct 2020 01:06:31 +0200 Subject: [PATCH] Add simpler template2 module and use it in content. --- logya/content.py | 40 +++++++++++++++++----------------------- logya/template2.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 logya/template2.py diff --git a/logya/content.py b/logya/content.py index 07f9bc0..e6e91b6 100644 --- a/logya/content.py +++ b/logya/content.py @@ -6,6 +6,7 @@ from markdown import markdown from logya import allowed_exts +from logya.template import env, render from logya.util import slugify from yaml import load @@ -126,39 +127,32 @@ def read_all(settings): return index -def write_page(path, content, template, settings): - page = '' - +def write_page(path, content, settings): # Make all settings in site section available to templates. - template_vars = settings['site'] + attrs = settings['site'] # Make doc attributes available to templates. - template_vars.update(content['doc']) + attrs.update(content['doc']) # Set additional template variables. - template_vars['canonical'] = settings['site']['base_url'] + template_vars['url'] + attrs['canonical'] = settings['site']['base_url'] + attrs['url'] - body = template_vars.get('body') - if body: - if content_type(content['path']) == 'markdown': - body = markdown(body, extensions=markdown_extensions) - # Pre-render doc body so Jinja2 template tags can be used in content body. - template_vars['body'] = template.env.from_string(body).render(template_vars) + if 'body' in attrs and content_type(content['path']) == 'markdown': + attrs['body'] = markdown(attrs['body'], extensions=markdown_extensions) - if 'template' in template_vars: - page = template.env.get_template(template_vars['template']).render(template_vars) - elif body: - page = template_vars['body'] - path.write_text(page) + if 'template' in attrs: + path.write_text(render(attrs['template'], attrs, pre_render='body')) + else: + path.write_text(attrs.get('body', '')) -def write_collection(path, content, template, settings): +def write_collection(path, content, settings): """Write an auto-generated index.html file.""" - template.vars['docs'] = content['docs'] - template.vars['title'] = content['title'] - template.vars['canonical'] = settings['site']['base_url'] + content['url'] + attrs = settings['site'] + attrs['docs'] = content['docs'] + attrs['title'] = content['title'] + attrs['canonical'] = settings['site']['base_url'] + content['url'] - page = template.env.get_template(content['template']) path.parent.mkdir(exist_ok=True) - path.write_text(page.render(template.vars)) + path.write_text(render(content['template'], attrs)) diff --git a/logya/template2.py b/logya/template2.py new file mode 100644 index 0000000..8f71ae9 --- /dev/null +++ b/logya/template2.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +from jinja2 import Environment, FileSystemLoader, select_autoescape + + +env = Environment( + loader=FileSystemLoader('templates'), + autoescape=select_autoescape(), + lstrip_blocks=True, + trim_blocks=True +) + + +def init(settings, site_index): + # Enable break and continue in templates. + env.add_extension('jinja2.ext.loopcontrols') + # Enable with statement for nested variable scopes. + env.add_extension('jinja2.ext.with_') + # Enable expression-statement extension that adds the do tag. + env.add_extension('jinja2.ext.do') + + # Get a document from its URL. + env.globals['get_doc'] = lambda url: site_index.get(url)['doc'] + + # Filter docs list where the given attribute contains the given value. + env.filters['attr_contains'] = lambda docs, attr, val: [ + doc for doc in docs if attr in doc and val in doc[attr]] + + +def render(tpl, variables, pre_render=None): + # Pre-render enables the use of Jinja2 template tags in the value of the given attribute. + if pre_render and pre_render in variables: + variables[pre_render] = env.from_string(variables[pre_render]).render(variables) + return env.get_template(tpl).render(variables) + + + + + + + + + +