Skip to content

Instantly share code, notes, and snippets.

@engeir
Created September 15, 2022 00:13
Show Gist options
  • Save engeir/bf64e7c03358df2c94275f89019cc65e to your computer and use it in GitHub Desktop.
Save engeir/bf64e7c03358df2c94275f89019cc65e to your computer and use it in GitHub Desktop.

Generate references in reveal-md and reveal-js

Inspired by this blog post.

Usage

The shell script is what does the work, and what you call with the source file as the first argument. For example, let us name the shell script revealjs-make-reflist.sh and the source file for the slide example.md (can also be .html, or actually anything else as long as it contains html code the the script understands). Then we call the script with

$ revealjs-make-reflist.sh example.md

Shell script

#!/bin/sh
# revealjs-make-reflist.sh

#
# This depends on having a python interpreter with the bs4 library, as well as having
# pandoc installed.
# You also need to provide your own .bib file, and point to it using the --bibliography
# filter of pandoc.
#

# Check if "Added by revealjs-make-reflist" is in file, and delete everything after (and
# including) that comment
sed -i '/<!-- Start adding with revealjs-make-reflist -->/,/<!-- End adding with revealjs-make-reflist -->/d' "$1"

# Save file content to variable for use in the brace below
FILE="$1"
# Append start line, content and end line to file
{
    echo "<!-- Start adding with revealjs-make-reflist -->"
    echo "---"
    /bin/python ~/bin/revealjs-make-reflist.py "$FILE" | pandoc --bibliography ~/science/ref/ref.bib --citeproc | /bin/python ~/bin/revealjs-make-reflist.py
    echo "<!-- End adding with revealjs-make-reflist -->"
} >> "$1"

Python script

# revealjs-make-reflist.py
import sys
from bs4 import BeautifulSoup

# bs4 is a non-standard dependency and must be installed for the python interpreter

try:
    file = sys.argv[1]
except IndexError:
    # If input comes from stdin, we are creating the final HTML code that goes into the
    # slide, therefore we start with ## References, and split up on every fourth
    # reference to go on a new vertical slide
    print("## References")
    soup = BeautifulSoup(sys.stdin, features="lxml")
    div = soup.find("div", id="refs")
    d_list = div.contents
    # All even-indexed elements are newline characters
    del d_list[::2]
    for i, element in enumerate(d_list):
        if i > 0 and i % 4 == 0:
            # Two hyphens, '--', is used as delimiter to create a new vertical slide.
            # Change this to fit your needs
            print("--")
        print(element)
        print('<!-- .element: style="font-size:20pt" -->')
else:
    # If input comes from a file passed as the first argument, we open it and look for
    # a-tags (anchor) with a "data-citation-key" specifier. These are the classic bib
    # tags, i.e., @author2022, etc.
    with open(file, "r") as fp:
        soup = BeautifulSoup(fp, features="lxml")
    for link in soup.find_all('a'):
        print(link.get('data-citation-key'))

Example markdown/HTML file

<!-- example.md -->
# My reveal-md presentation

<a href="https://link-to-publication.com"
data-citation-key="@author2022">Author (2022)</a> is a minimal toy example.

will be converted into

<!-- example.md -->
# My reveal-md presentation

<a href="https://link-to-publication.com"
data-citation-key="@author2022">Author (2022)</a> is a minimal toy example.
<!-- Start adding with revealjs-make-reflist -->
---
## References
<div class="csl-entry" id="ref-author2022" role="doc-biblioentry">
Author, A. T. 2022. <span>“Here Is the Title.”</span> <em>Journal of Stuff</em> volume (number) pagesstart-pagesend. <a href="https://doi.org/doicode">https://doi.org/doicode</a>.
</div>
<!-- .element: style="font-size:20pt" -->
<!-- End adding with revealjs-make-reflist -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment