Skip to content

Commit

Permalink
Add template_attrs function
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Oct 9, 2020
1 parent 4b70695 commit 24bbdb1
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions logya/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 24bbdb1

Please sign in to comment.