Skip to content

Instantly share code, notes, and snippets.

@DarwinAwardWinner
Last active February 15, 2019 07:28
Show Gist options
  • Save DarwinAwardWinner/5c3fd8fb82757bb04a0a36bda7f2c73f to your computer and use it in GitHub Desktop.
Save DarwinAwardWinner/5c3fd8fb82757bb04a0a36bda7f2c73f to your computer and use it in GitHub Desktop.
Snakefile for building presentations from Markdown using pandoc
import mistune
from lxml import html
from snakemake.utils import min_version
min_version('3.7.1')
rule all:
input: 'presentation.pdf'
rule pdf_to_png:
input: 'images/{filename}.pdf'
output: 'images/{filename}.png'
shell: '''convert -density 600 {input} {output}'''
rule svg_to_pdf:
input: 'images/{filename}.svg'
output: 'images/{filename}.pdf'
shell: '''inkscape {input} --export-pdf={output} --export-dpi=300'''
def get_mkdn_included_images(fname):
"""Grep the presentation markdown file for images"""
images = []
with open(fname) as f:
tree = html.fromstring(mistune.markdown(f.read()))
return list(map(str, tree.xpath("//img/@src")))
rule build_presentation:
input:
extra_preamble='extra-preamble.latex',
mkdn_file='{basename}.mkdn',
images=lambda wildcards: get_mkdn_included_images('{basename}.mkdn'.format(**wildcards)),
output:
pdf='{basename}.pdf'
params:
theme='Warsaw'
shell: '''
pandoc \
-f markdown -t beamer \
--pdf-engine=xelatex \
-o {output.pdf} \
-H {input.extra_preamble} \
-V theme:{params.theme} \
{input.mkdn_file}
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment