Converting absolute links to relative, redirection fix

This commit is contained in:
faildev_mode 2023-05-31 19:43:50 +02:00
parent d828f50faf
commit 5fc8db87d4
No known key found for this signature in database
GPG Key ID: 70845C70C0F5E205
2 changed files with 25 additions and 20 deletions

View File

@ -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)"""

View File

@ -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