Skip to content

Instantly share code, notes, and snippets.

@riceissa
Last active April 7, 2021 14:50
Show Gist options
  • Save riceissa/d5015a67160ca5c0f8ded99102f84a4b to your computer and use it in GitHub Desktop.
Save riceissa/d5015a67160ca5c0f8ded99102f84a4b to your computer and use it in GitHub Desktop.
for pandoc; like -M links-as-notes:true, but better

Use as e.g.

pandoc -f markdown -t markdown -F ./footnotify.py test --wrap=none

Transforms the file test:

In a [recent blog post](http://blog.givewell.org/2016/07/19/update-givewells-web-traffic-money-moved-q1-2016/ "Tyler Heishman. “Update on GiveWell’s web traffic / money moved: Q1 2016”. July 19, 2016. GiveWell."), ...

into

In a recent blog post[^1], ...

[^1]: Tyler Heishman. “Update on GiveWell’s web traffic / money moved: Q1 2016”. July 19, 2016. GiveWell. <http://blog.givewell.org/2016/07/19/update-givewells-web-traffic-money-moved-q1-2016/>

This is mostly useful when converting from Markdown to LaTeX, where by default Pandoc's LaTeX writer ignores the "title text" of a link (i.e. the part of the link that is displayed when one hovers over the link in a web browser; the penultimate word of this paragraph is an example of a hyperlink with a title text). I sometimes like to insert citation information in the title text (as in the test example above), so displaying it along with the URL in a footnote makes sense. In other words, this is a cheap way to obtain footnote citations when writing in Pandoc Markdown, without having to deal with pandoc-citeproc or any of that BibTeX nonsense. Another benefit of this approach is that when converting from Markdown to HTML, the links work as usual, in an intuitive way.

#!/usr/bin/python3
import sys
from pandocfilters import toJSONFilter, Para, Link, Note, Str, stringify, Space
def footnotify(key, value, format_, meta):
if key == "Link":
_, txt, (url, title_text) = value
# Ignore bare links like <http://example.com>; otherwise we run into a
# recursive problem where Pandoc tries to apply this same filter to the
# new link in the footnote. Also ignore URLs that begin with "#", since
# those point to sections within the document.
if stringify(txt) != url and not url.startswith("#"):
empty_attrs = ["", [], []]
cite_link = Link(empty_attrs, [Str(url)], [url, ""])
if title_text:
lst = [Str(title_text), Space(), cite_link]
else:
# There was no title text, so we do just what links-as-notes
# would do.
lst = [cite_link]
res = txt + [Note([Para(lst)])]
return res
if __name__ == "__main__":
toJSONFilter(footnotify)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
In a [recent blog post](http://blog.givewell.org/2016/07/19/update-givewells-web-traffic-money-moved-q1-2016/ "Tyler Heishman. “Update on GiveWell’s web traffic / money moved: Q1 2016”. July 19, 2016. GiveWell."), ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment