Add support for attachments and image links.

This commit is contained in:
Sergio Martínez Portela 2022-12-14 23:46:41 +01:00
parent 31450effe7
commit d004454192
2 changed files with 41 additions and 5 deletions

View File

@ -12,6 +12,7 @@ from datetime import datetime
import traceback import traceback
import re import re
from itertools import chain from itertools import chain
import shutil
import inotify.adapters import inotify.adapters
@ -36,6 +37,13 @@ EXTENSIONS = [
".org", ".org",
".org.txt", ".org.txt",
] ]
IMG_EXTENSIONS = [
"svg",
"png",
"jpg",
"jpeg",
"gif",
]
WATCH = True WATCH = True
if os.getenv('WATCH_AND_REBUILD', '1') == '0': if os.getenv('WATCH_AND_REBUILD', '1') == '0':
@ -125,6 +133,7 @@ def regen_all(src_top, dest_top, *, docs=None, db=None):
raise raise
docs = load_all(src_top) docs = load_all(src_top)
base_dirs = set()
doc_to_headline_remapping = {} doc_to_headline_remapping = {}
os.makedirs(dest_top, exist_ok=True) 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) # print("Skip:", relpath)
continue continue
base_dirs.add(os.path.dirname(relpath))
i = len(headlines) i = len(headlines)
while i > 0: while i > 0:
i -= 1 i -= 1
@ -320,6 +330,19 @@ def regen_all(src_top, dest_top, *, docs=None, db=None):
cur.close() cur.close()
db.commit() 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): def main(src_top, dest_top):
notifier = inotify.adapters.InotifyTrees([src_top, STATIC_PATH]) 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) assert_id_exists(graph['main_headlines'][target_path].id, headline, graph)
link_target = './' + graph['main_headlines'][target_path].id + '.node.html' link_target = './' + graph['main_headlines'][target_path].id + '.node.html'
elif link_target.startswith('attachment:'): 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)) logging.warning('Not implemented `attachment:` links. Used on {}'.format(link_target))
elif link_target.startswith('* '): elif link_target.startswith('* '):
target_headline = get_headline_with_name(link_target.lstrip('* '), headline.doc) 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: {}' raise NotImplementedError('Unknown link type: {}'
.format(link_target)) .format(link_target))
acc.append('<a href="{}" class="{}" >{}</a>'.format( if link_target.rsplit('.', 1)[-1].lower() in IMG_EXTENSIONS:
html.escape(link_target), acc.append('<a href="{}" class="img img-{}" ><img src="{}" /></a>'.format(
'internal' if is_internal_link else 'external', html.escape(link_target),
html.escape(description), 'internal' if is_internal_link else 'external',
)) html.escape(link_target),
))
else:
acc.append('<a href="{}" class="{}" >{}</a>'.format(
html.escape(link_target),
'internal' if is_internal_link else 'external',
html.escape(description),
))
except NonExistingLocalNoteError as err: except NonExistingLocalNoteError as err:
logging.warning(err.get_message()) logging.warning(err.get_message())
acc.append(html.escape(description)) acc.append(html.escape(description))

View File

@ -121,6 +121,10 @@ body nav input {
display: none; display: none;
} }
a.img {
display: block;
}
@font-face { @font-face {
font-family: "Atkinson Hyperlegible"; font-family: "Atkinson Hyperlegible";
src: url('./fonts/atkinson-hyperlegible/eot/Atkinson-Hyperlegible-Regular-102.eot'); src: url('./fonts/atkinson-hyperlegible/eot/Atkinson-Hyperlegible-Regular-102.eot');