Converting absolute links to relative, redirection fix
This commit is contained in:
parent
d828f50faf
commit
5fc8db87d4
|
@ -29,12 +29,10 @@ class Content:
|
|||
if not os.path.exists('static/'+path):
|
||||
raise Exception(f'Resource {dest} for {path} not found')
|
||||
|
||||
path = os.path.relpath(path, start=os.path.dirname(os.path.dirname(path)))
|
||||
path = urllib.parse.quote(path)
|
||||
if label:
|
||||
return path+' '+label
|
||||
return '/'+path+' '+label
|
||||
else:
|
||||
return path
|
||||
return '/'+path
|
||||
|
||||
def dir(dir: str):
|
||||
"""Iterator of Item objects representing files in specified directory (like os.scandir)"""
|
||||
|
|
|
@ -125,10 +125,10 @@ def evaluate(match: re.Match, global_ns: dict) -> str:
|
|||
return '\n<a real error occured here>\n'
|
||||
|
||||
def redirect(match: re.Match, domains: dict) -> str:
|
||||
this_domain = match.group(1)
|
||||
this_domain = match.group(2)
|
||||
if this_domain in domains:
|
||||
print('>',f'{this_domain} => {domains[this_domain]}')
|
||||
return domains[this_domain]
|
||||
return match.group(1) + domains[this_domain] + match.group(3)
|
||||
else: return match.group(0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -173,27 +173,34 @@ if __name__ == '__main__':
|
|||
content = evaluate_this(item.content, namespace)
|
||||
|
||||
# load template
|
||||
if template := template_for(path):
|
||||
if template not in template_cache:
|
||||
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
|
||||
namespace.update(item.frontmatter_data)
|
||||
namespace['content'] = content
|
||||
content = evaluate_this(template_data, namespace)
|
||||
if 'templates' in config:
|
||||
if template := template_for(path):
|
||||
if template not in template_cache:
|
||||
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
|
||||
namespace.update(item.frontmatter_data)
|
||||
namespace['content'] = content
|
||||
content = evaluate_this(template_data, namespace)
|
||||
|
||||
# do redirections
|
||||
if 'redirections' in config:
|
||||
content = re.sub(r'https?://([-\w]+[-\w.]*)(/\S*)?', partial(
|
||||
content = re.sub(r'(https?://)([-\w]+[-\w.]*)(/\S*)?', partial(
|
||||
redirect,
|
||||
domains=dict(config['redirections'])
|
||||
), content)
|
||||
|
||||
# convert absolute links to relative
|
||||
if path.endswith('.gmi'):
|
||||
content = re.sub(r'=>(\s+)/(\S+)',
|
||||
lambda a: '=>'+a.group(1)+os.path.relpath(a.group(2), start=os.path.dirname(path)),
|
||||
content)
|
||||
|
||||
# TODO: produce HTML version
|
||||
|
||||
# save results
|
||||
|
|
Reference in New Issue