Redirections!
This commit is contained in:
parent
a1dfccddfc
commit
d828f50faf
|
@ -105,14 +105,14 @@ def evaluate_this(content: str, global_ns: dict) -> str:
|
|||
return re.sub(r'\{([^\s}]+([^}]*[^\s}])?)\}', partial(evaluate, global_ns=global_ns), content)
|
||||
|
||||
def evaluate(match: re.Match, global_ns: dict) -> str:
|
||||
match = match.group(1)
|
||||
match = match.group(1).split('\n')
|
||||
print('*','\n '.join(match))
|
||||
# this trick let the functions in apis.py access our environment
|
||||
apis.environment = global_ns
|
||||
# now let's execute the command!
|
||||
# unfortunately we can't use 'return' in expressions
|
||||
# instead the last line will be evaluated and returned, the rest – executed
|
||||
local_ns = {}
|
||||
match = match.split('\n')
|
||||
to_exec = '\n'.join(match[:-1])
|
||||
to_eval = match[-1]
|
||||
try:
|
||||
|
@ -124,6 +124,12 @@ def evaluate(match: re.Match, global_ns: dict) -> str:
|
|||
print(traceback.format_exc(), file=sys.stderr)
|
||||
return '\n<a real error occured here>\n'
|
||||
|
||||
def redirect(match: re.Match, domains: dict) -> str:
|
||||
this_domain = match.group(1)
|
||||
if this_domain in domains:
|
||||
print('>',f'{this_domain} => {domains[this_domain]}')
|
||||
return domains[this_domain]
|
||||
else: return match.group(0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.chdir(sys.path[0])
|
||||
|
@ -159,6 +165,7 @@ if __name__ == '__main__':
|
|||
|
||||
item = Item(path)
|
||||
|
||||
# parse content
|
||||
namespace = namespace_from(apis.Content)
|
||||
namespace.update(config['variables'])
|
||||
namespace['path'] = item.path
|
||||
|
@ -171,7 +178,8 @@ if __name__ == '__main__':
|
|||
with open('templates/'+template) as fp:
|
||||
template_cache[template] = fp.read()
|
||||
template_data = template_cache[template]
|
||||
|
||||
|
||||
# apply template
|
||||
namespace = namespace_from(apis.Content)
|
||||
namespace.update(config['variables'])
|
||||
namespace['path'] = item.path
|
||||
|
@ -179,7 +187,13 @@ if __name__ == '__main__':
|
|||
namespace['content'] = content
|
||||
content = evaluate_this(template_data, namespace)
|
||||
|
||||
# TODO: do redirections
|
||||
# do redirections
|
||||
if 'redirections' in config:
|
||||
content = re.sub(r'https?://([-\w]+[-\w.]*)(/\S*)?', partial(
|
||||
redirect,
|
||||
domains=dict(config['redirections'])
|
||||
), content)
|
||||
|
||||
# TODO: produce HTML version
|
||||
|
||||
# save results
|
||||
|
|
Reference in New Issue