From 24bbdb14e6745099cbdca841ea48f828124a379c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramiro=20G=C3=B3mez?= Date: Fri, 9 Oct 2020 23:32:21 +0200 Subject: [PATCH] Add template_attrs function --- logya/content.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/logya/content.py b/logya/content.py index 0da6a65..d93c79c 100644 --- a/logya/content.py +++ b/logya/content.py @@ -104,35 +104,36 @@ def read(path: Path, path_rel: Path) -> str: return doc -def write_page(path, content, settings): - path.parent.mkdir(parents=True, exist_ok=True) +def template_attrs(content: dict, settings: dict) -> dict: + """Return dictionary created from site settings and content. - # Make all settings in site section available to templates. - attrs = settings['site'] + Site settings may be overridden by content.""" - # Make doc attributes available to templates. - attrs.update(content['doc']) + # Make all settings in site section available to templates. + attrs = settings['site'].copy() - # Set additional template variables. - attrs['canonical'] = settings['site']['base_url'] + attrs['url'] + # Set canonical URL, that can be overridden in content. + attrs['canonical'] = attrs['base_url'] + content['url'] - if 'body' in attrs and content_type(content['path']) == 'markdown': - attrs['body'] = markdown(attrs['body'], extensions=markdown_extensions) + # Make content attributes available to templates. + attrs.update(content) - if 'template' in attrs: - path.write_text(render(attrs['template'], attrs, pre_render='body')) - else: - path.write_text(attrs.get('body', '')) + return attrs -def write_collection(path, content, settings): - """Write an auto-generated index.html file.""" +def write_page(path: Path, content: dict, settings: dict): + """Write a content page.""" path.parent.mkdir(parents=True, exist_ok=True) + attrs = template_attrs(content['doc'], settings) + if 'body' in attrs and content_type(content['path']) == 'markdown': + attrs['body'] = markdown(attrs['body'], extensions=markdown_extensions) + path.write_text(render(attrs['template'], attrs, pre_render='body')) - attrs = settings['site'] - attrs['docs'] = content['docs'] - attrs['title'] = content['title'] - attrs['canonical'] = settings['site']['base_url'] + content['url'] - path.write_text(render(content['template'], attrs)) +def write_collection(path: Path, content: dict, settings: dict): + """Write a collection page.""" + + path.parent.mkdir(parents=True, exist_ok=True) + attrs = template_attrs(content, settings) + path.write_text(render(attrs['template'], attrs))