-
Notifications
You must be signed in to change notification settings - Fork 0
super links.py
Pandoc filter that creates custom links from bracketed spans with a specific ID (#l), written as:
-
with a 'type' attribute
[text]{#l type=wiki}
-
with a 'class' name starting with a dot
[text]{#l .wiki}
The link text is specified inside the square brackets. Its URL is configured in the dictionary LINKS or in the markdown metadata (see later).
The keys of the LINKS dictionary contain the type or class name that will provide the URL mapping for the link. In the simpler case, there will be a string containing the URL. The final URL will be the URL in the dictionary appended by the text inside the brackets.
[text]{#l type=github}
URL => https://github.com/text
The default LINKS dict contains (showing only the github entry):
LINKS = {"github" : "https://github.com/"}
If the attribute "title" is set, then it will replace the link text. The original text in brackets will still be used to create the link URL
[Arabidopsis_thaliana]{#l .wiki title="Thale cress"}
-> <p><a href="https://en.wikipedia.org/wiki/Arabidopsis_thaliana" class="wiki" title="Arabidopsis thaliana">Thale cress</a></p>
Also, the "before' and 'after' attributes can be used to preprend or append text to the link text
[Arabidopsis_thaliana]{#l .wiki title="Thale cress" before="wikipedia:"}
-> <p><a href="https://en.wikipedia.org/wiki/Arabidopsis_thaliana" class="wiki" title="Arabidopsis thaliana">wikipedia:Thale cress</a></p>
Aside from the default cases, additional links can be configured in the pandoc metadata header:
---
super-links:
foo: "https://hello.co"
other:
url: "HTTP://foo.com/"
encode: true
before: "SOMETEXT:"
after: " <--AFTER"
...
Any output format should be supported, and in the case above we will have the following results, using the HTML output format:
[FOO]{#l .foo}
-> <p><a href="https://hello.co/FOO" class="foo">FOO</a></p>
[Some text]{#l type=other}
-> <p><a href="HTTP://foo.com//Some+text" type="other">SOMETEXT:Some text<–AFTER</a></p>
[1233211]{#l type=pubmed}
-> <p><a href="https://pubmed.ncbi.nlm.nih.gov//1233211" type="pubmed">pubmed:1233211</a></p>
[Arabidopsis_thaliana]{#l .wiki title="Arabidopsis thaliana" before="WIKI:"}
-> <p><a href="https://en.wikipedia.org/wiki//Arabidopsis_thaliana" class="wiki" title="Arabidopsis thaliana" data-before="WIKI:">WIKI:Arabidopsis thaliana</a></p>
[10.1007/978-1-4613-8850-0_3]{#l .doi}
-> <p><a href="https://doi.org//10.1007/978-1-4613-8850-0_3" class="doi">DOI:10.1007/978-1-4613-8850-0_3</a></p>
This filter, in some cases, requires less typing to represent a link. For instance:
-
Plain markdown
![thale cress](https://en.wikipedia.org/wiki/Arabidopsis_thaliana)
-
With filter
[Arabidopsis_thaliana]{#l .wiki}
-
Install python 3 (version >3.9) and pandoc
-
Install panflute python libraries
pip3 install panflute
-
Download and place the file in directory:
~/.pandoc/filters
pandoc -s test.md -t beamer --filter super-links.py
Prof. Georgios Pappas Jr
University of Brasilia (UnB) - Brazil
version 0.1