Skip to content

Instantly share code, notes, and snippets.

@nstickney
Forked from kristopherjohnson/Makefile
Last active January 5, 2020 16:36
Show Gist options
  • Save nstickney/e0a85630a471d8f0678d1b9d35a089b1 to your computer and use it in GitHub Desktop.
Save nstickney/e0a85630a471d8f0678d1b9d35a089b1 to your computer and use it in GitHub Desktop.
Makefile that uses Pandoc to generate HTML, PDF, DOCX, etc. from Markdown source files
# Makefile
#
# Credits: https://gist.github.com/kristopherjohnson/7466917
#
# Converts Markdown to other formats using Pandoc (https://pandoc.org/)
#
# Convert a single Markdown file to a specific format:
# $ make <filename>.<format_suffix>
#
# Convert all Markdown files to a specific format:
# $ make <format_suffix>
#
# Convert all Markdown files to all possible formats:
# $ make [all]
#
# Delete all generated files:
# $ make clean
# Convert all files in this directory that have a .md suffix
SOURCE_DOCS = $(wildcard *.md)
# Depend on all images (https://stackoverflow.com/a/14289872)
DEPS = $(shell find images | sed 's/ /\\ /g')
# Depend on bibliography and Citation Style Language format files
# CSL repository: https://github.com/citation-style-language/styles
PDF_DEPS = $(wildcard *.bib) $(wildcard *.csl)
# Possible document types
DOCX = $(SOURCE_DOCS:.md=.docx)
EPUB = $(SOURCE_DOCS:.md=.epub)
HTML = $(SOURCE_DOCS:.md=.html)
ODT = $(SOURCE_DOCS:.md=.odt)
PDF = $(SOURCE_DOCS:.md=.pdf)
RTF = $(SOURCE_DOCS:.md=.rtf)
TEX = $(SOURCE_DOCS:.md=.tex)
# All document types
# Change this line to set only those document types needed
EXPORTED_DOCS = $(DOCX) $(EPUB) $(HTML) $(ODT) $(PDF) $(RTF) $(TEX)
RM=/usr/bin/env rm
PANDOC=/usr/bin/env pandoc
PANDOC_OPTIONS=
PANDOC_DOCX_OPTIONS=
PANDOC_EPUB_OPTIONS=--to epub3
PANDOC_HTML_OPTIONS=--to html5
PANDOC_ODT_OPTIONS=
PANDOC_PDF_OPTIONS=
PANDOC_RTF_OPTIONS=
PANDOC_TEX_OPTIONS=$(PANDOC_PDF_OPTIONS)
# Pattern-matching Rules
%.docx : %.md $(DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_DOCX_OPTIONS) -o $@ $<
%.epub : %.md $(DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_EPUB_OPTIONS) -o $@ $<
%.html : %.md $(DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_HTML_OPTIONS) -o $@ $<
%.odt : %.md $(DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_ODT_OPTIONS) -o $@ $<
%.pdf : %.md $(DEPS) $(PDF_DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_PDF_OPTIONS) -o $@ $<
%.rtf : %.md $(DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_RTF_OPTIONS) -o $@ $<
%.tex : %.md $(DEPS) $(TEX_DEPS)
$(PANDOC) $(PANDOC_OPTIONS) $(PANDOC_TEX_OPTIONS) -o $@ $<
# Targets and dependencies
.PHONY: all docx epub html odt pdf rtf tex clean
all: $(EXPORTED_DOCS)
docx: $(DOCX)
epub: $(EPUB)
html: $(HTML)
odt: $(ODT)
pdf: $(PDF)
rtf: $(RTF)
tex: $(TEX)
clean:
- $(RM) $(EXPORTED_DOCS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment