Smart log() and article() API function, incomplete html templating implementation

This commit is contained in:
faildev_mode 2023-07-19 22:01:29 +02:00
parent eb9dd047b4
commit 303d70009d
No known key found for this signature in database
GPG Key ID: 70845C70C0F5E205
2 changed files with 26 additions and 9 deletions

View File

@ -3,7 +3,9 @@ from item import Item # from itself
import datetime
class Content:
"""This class contains some functions useful for evaluation of Python code in content files"""
"""This class contains some functions useful for evaluation of Python code
in content files
"""
__builtins__ = {
'sorted': sorted
@ -12,16 +14,19 @@ class Content:
def log(dest: str) -> str:
"""Returns path to specified log"""
return '/gemlog/'+dest+'.gmi'
ext = '.gmi' if environment['mode'] == 'gemini' else '.html'
return '/gemlog/'+dest+ext
def article(dest: str) -> str:
"""Returns path to specified article"""
return '/articles/'+dest+'.gmi'
ext = '.gmi' if environment['mode'] == 'gemini' else '.html'
return '/articles/'+dest+ext
def res(dest: str, label=None) -> str:
"""Returns path to specified resource (usually image), which resides in a
directory of the name of current file"""
"""Returns path to specified resource (usually image), which resides in
a directory of the name of current file
"""
path = os.path.splitext(environment['path'])[0]
path += '/'+dest
@ -35,7 +40,9 @@ class Content:
return '/'+path
def dir(dir: str):
"""Iterator of Item objects representing files in specified directory (like os.scandir)"""
"""Iterator of Item objects representing files in specified directory
(like os.scandir)
"""
for entry in os.scandir('content/'+dir):
item = Item(os.path.relpath(entry.path, start='content'))

View File

@ -134,8 +134,7 @@ def gemtext2html(parser: GemtextParser, rotate_extension=True) -> BeautifulSoup:
if rotate_extension and href.endswith('.gmi') and not REGEX['generic_url'].match(href):
href = os.path.splitext(href)[0] + '.html'
el = soup.new_tag('a', href=href)
if item.label:
el.append(item.label)
el.append(item.label or href)
elif item.type == 'preformatted':
el = soup.new_tag('pre')
@ -302,8 +301,19 @@ if __name__ == '__main__':
if html_path.endswith('.html'):
# TODO: html template
# incomplete, yet should function with some primitive templates
if template := template_for(html_path):
...
tpl_item = Item(template, prefix='templates/')
if not 'mode' in tpl_item or tpl_item.mode == 'www':
namespace = namespace_from(
{'path': path},
apis.Content,
config['variables'],
config['variables.www'],
item.frontmatter_data,
{'mode': 'www', 'content': content}
)
content = evaluate_this(tpl_item.content, namespace)
# redirections, path conversion
soup = BeautifulSoup(content, features='html.parser')