namespace_from can derrive from multiple extensions, replacing updating namespace manually
This commit is contained in:
parent
08aaa2f6fa
commit
a7b7b5a33a
|
@ -32,16 +32,24 @@ def template_for(path: str) -> str:
|
|||
|
||||
return template_data
|
||||
|
||||
def namespace_from(extension: type) -> dict:
|
||||
"""Generates global namespace for evaluation from provided class (extension)
|
||||
def namespace_from(*extensions) -> dict:
|
||||
"""Generates global namespace for evaluation derrived from provided
|
||||
extensions, which can be any iterable object or class.
|
||||
__builtins__ is empty by default, but can be overrided by extension
|
||||
"""
|
||||
|
||||
namespace = {'__builtins__': {}}
|
||||
extension = vars(extension)
|
||||
for i in extension.keys():
|
||||
if i.startswith('_') and i != '__builtins__': continue
|
||||
namespace[i] = extension[i]
|
||||
|
||||
for extension in extensions:
|
||||
if type(extension) == type: # extract items from class
|
||||
extension = vars(extension)
|
||||
for i in extension.keys():
|
||||
if i.startswith('_') and i != '__builtins__': continue
|
||||
namespace[i] = extension[i]
|
||||
|
||||
else: # iterables
|
||||
namespace.update(extension)
|
||||
|
||||
return namespace
|
||||
|
||||
def evaluate_this(content: str, global_ns: dict, local_ns: dict = {}) -> str:
|
||||
|
@ -194,20 +202,25 @@ if __name__ == '__main__':
|
|||
item = Item(path)
|
||||
|
||||
# parse content
|
||||
namespace = namespace_from(apis.Content)
|
||||
namespace.update(config['variables'])
|
||||
namespace['path'] = item.path
|
||||
namespace.update(item.frontmatter_data)
|
||||
namespace = namespace_from(
|
||||
{'path': path},
|
||||
apis.Content,
|
||||
config['variables'],
|
||||
item.frontmatter_data
|
||||
)
|
||||
|
||||
content = evaluate_this(item.content, namespace)
|
||||
|
||||
# load template
|
||||
if template := template_for(path):
|
||||
# apply template
|
||||
namespace = namespace_from(apis.Content)
|
||||
namespace.update(config['variables'])
|
||||
namespace['path'] = item.path
|
||||
namespace.update(item.frontmatter_data)
|
||||
namespace['content'] = content
|
||||
# apply template
|
||||
namespace = namespace_from(
|
||||
{'path': path},
|
||||
apis.Content,
|
||||
config['variables'],
|
||||
item.frontmatter_data,
|
||||
{'content': content}
|
||||
)
|
||||
content = evaluate_this(template, namespace)
|
||||
|
||||
# do redirections
|
||||
|
|
|
@ -2,7 +2,7 @@ import os
|
|||
|
||||
cache = {} # globally shared object to store file content
|
||||
|
||||
def read_this(path: str):
|
||||
def read_this(path: str) -> str:
|
||||
"""Reads data from the file
|
||||
This function uses cache to minimize I/O
|
||||
"""
|
||||
|
|
|
@ -6,7 +6,7 @@ from attrdict import AttrDict # from itself
|
|||
|
||||
class Item(AttrDict):
|
||||
"""This class represents single content file
|
||||
It extracts some pre-defined frontmatter fields using python-frontmatter module"""
|
||||
It extracts all frontmatter fields using python-frontmatter module"""
|
||||
|
||||
title = None
|
||||
tags = []
|
||||
|
|
Reference in New Issue