From 35a7c14c1a2d1525d825834f16e78c512ed19867 Mon Sep 17 00:00:00 2001 From: faildev_mode Date: Fri, 14 Jul 2023 15:05:49 +0200 Subject: [PATCH] =?UTF-8?q?Parse=20content=20files=20twice=20=E2=80=93?= =?UTF-8?q?=C2=A0for=20gemini=20and=20www=20with=20different=20'mode'=20va?= =?UTF-8?q?riable=20(see=20NOTES.md).=20Fixed=20bug=20(in=20abs2rel)=20cau?= =?UTF-8?q?sing=20lack=20of=20link=20labels.=20Few=20other=20minor=20fixes?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NOTES.md | 6 +-- _src/build.py | 132 ++++++++++++++++++++++++++++++++------------------ _src/item.py | 6 +-- 3 files changed, 92 insertions(+), 52 deletions(-) diff --git a/NOTES.md b/NOTES.md index 7a3a405..e6be818 100644 --- a/NOTES.md +++ b/NOTES.md @@ -2,9 +2,9 @@ build.py # Configs -* **\[defaults\]** – default frontmatter fields values (i.e. `author=faildev_mode`) +* **\[defaults\]** – default frontmatter fields values (i.e. `author=faildev_mode`) * **\[variables\]** – project-wide variables (i.e. `root=rawtext.club/~faildev_mode`) -* **\[variables.gemini\]** – project-wide variables gemini-exclusive +* **\[variables.gemini\]** – project-wide variables gemini-exclusive * **\[variables.www\]** – project-wide variables www-exclusive * **\[redirections\]** – redirection table (i.e. `youtube.com=invidious.citizen4.eu`) * **\[templates\]** – template routing table (i.e. `articles/*.gmi=articles`) @@ -32,7 +32,7 @@ for path in content_files: if item.mode == 'www' or not item.mode: content = evaluate(item.content, {mode='www'}) content = evaluate(item.template, {mode='www', item=item}) - content = gemini2html(content) + content = gemtext2html(content) content = evaluate(item.html_template, {item=item}) save_this(f'html/{item.location}.html', content) ``` diff --git a/_src/build.py b/_src/build.py index 52dec01..01c8268 100755 --- a/_src/build.py +++ b/_src/build.py @@ -12,7 +12,7 @@ from functools import partial import apis # from itself import traceback from item import Item # from itself -from files import read_this, save_this, link_this, scan_dir # from itself +from files import save_this, link_this, scan_dir # from itself from config import config # from itself def template_for(path: str) -> str: @@ -28,9 +28,8 @@ def template_for(path: str) -> str: for pattern in config['templates'].keys(): if fnmatch(path, pattern): template = config['templates'][pattern] - template_data = read_this('templates/'+template) - return template_data + return template def namespace_from(*extensions) -> dict: """Generates global namespace for evaluation derrived from provided @@ -167,9 +166,9 @@ def gemtext2html(gemtext: str) -> str: def abs2rel(match: re.Match, path: str) -> str: """Callable for re.sub, converts absolute link to relative to current document (path argument).""" - url = os.path.relpath(match.group(1), start=os.path.dirname(path)) - text = match.group(2).strip() if len(match.groups()) > 2 else url - return f'=> {url} {text}' + url = os.path.relpath(match.group(2), start=os.path.dirname(path)) + print(' M', url) + return match.group(1) + url if __name__ == '__main__': os.chdir(sys.path[0]) @@ -201,51 +200,92 @@ if __name__ == '__main__': item = Item(path) - # parse content - 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 + # parsing in gemini mode + if not 'mode' in item or item.mode == 'gemini': + # preparing namespace namespace = namespace_from( {'path': path}, apis.Content, config['variables'], + config['variables.gemini'], item.frontmatter_data, - {'content': content} + {'mode': 'gemini'} ) - content = evaluate_this(template, namespace) - - # do redirections - if 'redirections' in config: - 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+)(\s+.*)?', partial( - abs2rel, path=path - ), content) + # evaluation + content = evaluate_this(item.content, namespace) + # determining and applying template (optional) + if template := template_for(path): + tpl_item = Item(template, prefix='templates/') + if not 'mode' in tpl_item or tpl_item.mode == 'gemini': + namespace = namespace_from( + {'path': path}, + apis.Content, + config['variables'], + config['variables.gemini'], + item.frontmatter_data, + {'mode': 'gemini', 'content': content} + ) + content = evaluate_this(tpl_item.content, namespace) - # produce HTML version - html_path = os.path.splitext(path)[0]+'.html' - html_content = gemtext2html(content) - # determine which template to use - if template := template_for(html_path): - ... - save_this('../html/'+html_path, html_content) + # do redirections (http/https only!) + if 'redirections' in config: + content = re.sub(r'(https?://)([-\w]+[-\w.]*)(/\S*)?', partial( + redirect, + domains=dict(config['redirections']) + ), content) + + # convert absolute links to relative (gemtext only!) + if path.endswith('.gmi'): + content = re.sub(r'(=>[ \t]+)/(\S+)', partial( + abs2rel, path=path + ), content) + + # save results + save_this('../'+path, content) + else: + print(' ! skipped for gemini') + # TODO: delete from gemini output - # save results - save_this('../'+path, content) - - if not path.endswith('.gmi'): - link_this('../'+path, '../html/'+path) + # parsing in www mode + if not 'mode' in item or item.mode == 'www': + namespace = namespace_from( + {'path': path}, + apis.Content, + config['variables'], + config['variables.www'], + item.frontmatter_data, + {'mode': 'www'} + ) + + content = evaluate_this(item.content, namespace) + + if template := template_for(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) + + # convert to html + html_path = path + if path.endswith('.gmi'): + content = gemtext2html(content) + html_path = os.path.splitext(path)[0]+'.html' + + if html_path.endswith('.html'): + # TODO: html template + if template := template_for(html_path): + ... + + # TODO: redirections, path conversion + + save_this('../html/'+html_path, content) + else: + print(' ! skipped for www') + # TODO: delete from www output diff --git a/_src/item.py b/_src/item.py index 3d412b9..2bf1f2e 100644 --- a/_src/item.py +++ b/_src/item.py @@ -14,13 +14,13 @@ class Item(AttrDict): source = None author = None - def __init__(self, path: str): + def __init__(self, path: str, prefix: str = 'content/'): # initialize parent super().__init__() - self.path = path + self.path, self.prefix = path, prefix - frontmatter_data, content = frontmatter.parse(read_this('content/'+path)) + frontmatter_data, content = frontmatter.parse(read_this(prefix + path)) self.content = content self.frontmatter_data = frontmatter_data