From d004454192375f2efd474f21547bbf9daf057f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Wed, 14 Dec 2022 23:46:41 +0100 Subject: [PATCH] Add support for attachments and image links. --- scripts/generate.py | 42 +++++++++++++++++++++++++++++++++++++----- static/style.css | 4 ++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/scripts/generate.py b/scripts/generate.py index 8e079fc..2030578 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -12,6 +12,7 @@ from datetime import datetime import traceback import re from itertools import chain +import shutil import inotify.adapters @@ -36,6 +37,13 @@ EXTENSIONS = [ ".org", ".org.txt", ] +IMG_EXTENSIONS = [ + "svg", + "png", + "jpg", + "jpeg", + "gif", +] WATCH = True if os.getenv('WATCH_AND_REBUILD', '1') == '0': @@ -125,6 +133,7 @@ def regen_all(src_top, dest_top, *, docs=None, db=None): raise docs = load_all(src_top) + base_dirs = set() doc_to_headline_remapping = {} os.makedirs(dest_top, exist_ok=True) @@ -143,6 +152,7 @@ def regen_all(src_top, dest_top, *, docs=None, db=None): # print("Skip:", relpath) continue + base_dirs.add(os.path.dirname(relpath)) i = len(headlines) while i > 0: i -= 1 @@ -320,6 +330,19 @@ def regen_all(src_top, dest_top, *, docs=None, db=None): cur.close() db.commit() + logging.info("Copying attachments") + attachments_dir = os.path.join(dest_top, 'attachments') + os.makedirs(attachments_dir, exist_ok=True) + for base in base_dirs: + data_dir = os.path.join(src_top, base, 'data') + if not os.path.exists(data_dir): + continue + for subdir in os.listdir(data_dir): + shutil.copytree(os.path.join(data_dir, subdir), + os.path.join(attachments_dir, subdir), + dirs_exist_ok=True) + + def main(src_top, dest_top): notifier = inotify.adapters.InotifyTrees([src_top, STATIC_PATH]) @@ -539,6 +562,8 @@ def render_text_tokens(tokens, acc, headline, graph): assert_id_exists(graph['main_headlines'][target_path].id, headline, graph) link_target = './' + graph['main_headlines'][target_path].id + '.node.html' elif link_target.startswith('attachment:'): + inner_target = link_target.split(':', 1)[1] + link_target = 'attachments/{}/{}/{}'.format(headline.id[:2], headline.id[2:], inner_target) logging.warning('Not implemented `attachment:` links. Used on {}'.format(link_target)) elif link_target.startswith('* '): target_headline = get_headline_with_name(link_target.lstrip('* '), headline.doc) @@ -557,11 +582,18 @@ def render_text_tokens(tokens, acc, headline, graph): raise NotImplementedError('Unknown link type: {}' .format(link_target)) - acc.append('{}'.format( - html.escape(link_target), - 'internal' if is_internal_link else 'external', - html.escape(description), - )) + if link_target.rsplit('.', 1)[-1].lower() in IMG_EXTENSIONS: + acc.append(''.format( + html.escape(link_target), + 'internal' if is_internal_link else 'external', + html.escape(link_target), + )) + else: + acc.append('{}'.format( + html.escape(link_target), + 'internal' if is_internal_link else 'external', + html.escape(description), + )) except NonExistingLocalNoteError as err: logging.warning(err.get_message()) acc.append(html.escape(description)) diff --git a/static/style.css b/static/style.css index 75427ac..a23065d 100644 --- a/static/style.css +++ b/static/style.css @@ -121,6 +121,10 @@ body nav input { display: none; } +a.img { + display: block; +} + @font-face { font-family: "Atkinson Hyperlegible"; src: url('./fonts/atkinson-hyperlegible/eot/Atkinson-Hyperlegible-Regular-102.eot');