Reimplement Markdown linkification logic

Automatically hide links on screen and show when printing
This commit is contained in:
RunasSudo 2020-07-23 22:31:50 +10:00
parent 6861a6dd97
commit 646eaf2b31
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 24 additions and 5 deletions

View File

@ -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'<a href="\1" class="smart-url"><span>\[URL\]</span><span class="url">\1</span></a>', fn_src)
fn_src = re.sub(r'doi: ([^ ]+)', r'<a href="https://doi-org.ezproxy.lib.monash.edu.au/\1">doi: <span class="url">\1</span></a>', fn_src)
self.parser.parseChunk(surrogate_parent, fn_src)
for el in list(surrogate_parent):
li.append(el)
surrogate_parent.remove(el)

View File

@ -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;
}