Skip to content

Commit

Permalink
Add simpler template2 module and use it in content.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Oct 1, 2020
1 parent c38ec0e commit fc7a46b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
40 changes: 17 additions & 23 deletions logya/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
43 changes: 43 additions & 0 deletions logya/template2.py
Original file line number Diff line number Diff line change
@@ -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)










0 comments on commit fc7a46b

Please sign in to comment.