From 646eaf2b31bae729116a5cb9ff6915f9c863af6c Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Thu, 23 Jul 2020 22:31:50 +1000 Subject: [PATCH] Reimplement Markdown linkification logic Automatically hide links on screen and show when printing --- wikinote/markup.py | 13 ++++++++----- wikinote/static/css/main.css | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/wikinote/markup.py b/wikinote/markup.py index cdbca9a..46dbbbf 100644 --- a/wikinote/markup.py +++ b/wikinote/markup.py @@ -20,8 +20,6 @@ import markdown.extensions.extra, markdown.extensions.footnotes, markdown.extens import re import xml.etree.ElementTree as ET -from .mdx_urlize import UrlizeExtension - directives = {} roles = {} @@ -29,7 +27,7 @@ class WNMarkdown(markdown.Markdown): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.registerExtensions([FootnoteExtension(), UrlizeExtension(), 'toc', 'tables'], {}) + self.registerExtensions([FootnoteExtension(), 'toc', 'tables'], {}) self.meta = {} @@ -374,7 +372,7 @@ class FootnoteExtension(markdown.extensions.footnotes.FootnoteExtension): md.inlinePatterns.register(FootnoteInlineProcessor(FOOTNOTE_RE, self), 'footnote', 175) md.treeprocessors.register(markdown.extensions.footnotes.FootnoteTreeprocessor(self), 'footnote', 50) - # Override to omit backlinks + # Override to omit backlinks and reformat def makeFootnotesDiv(self, root): if not list(self.footnotes.keys()): return None @@ -387,7 +385,12 @@ class FootnoteExtension(markdown.extensions.footnotes.FootnoteExtension): for index, id in enumerate(self.footnotes.keys(), start=1): li = ET.SubElement(ol, "li") li.set("id", self.makeFootnoteId(id)) - self.parser.parseChunk(surrogate_parent, self.footnotes[id]) + + fn_src = self.footnotes[id] + fn_src = re.sub(r'(https?://[^ ]+)', r'\[URL\]\1', fn_src) + fn_src = re.sub(r'doi: ([^ ]+)', r'doi: \1', fn_src) + + self.parser.parseChunk(surrogate_parent, fn_src) for el in list(surrogate_parent): li.append(el) surrogate_parent.remove(el) diff --git a/wikinote/static/css/main.css b/wikinote/static/css/main.css index 085c43f..3e70b01 100644 --- a/wikinote/static/css/main.css +++ b/wikinote/static/css/main.css @@ -207,6 +207,22 @@ a.redlink { color: #cc0000ff !important; } +a span.url { + word-break: break-all; +} + +a.smart-url > span.url { + display: none; +} +@media print { + a.smart-url > span.url { + display: inline; + } + a.smart-url > span:not(.url) { + display: none; + } +} + div.two-columns { display: flex; }