Makefile to process all Asciidoctor files in a directory

· by Christoph Stoettner · Read in about 2 min · (282 words)

I write most of my documents (blog posts, documentation, recipes and so on) with Asciidoctor. Everything is organized in Git repositories.

During GPN 19 (Gulaschprogrammiernacht) I showed how to build html and pdf with a Gitlab CI/CD pipeline. That’s quite handy, but lots of documents I build, I just need locally.

So today I played with WSL2 and a Makefile to build all Asciidoctor files in a directory.

Commandline to make pdf
asciidoctor-pdf source.adoc -o build/source.pdf

This builds a pdf in the directory build, but how can we create a pdf of some files?

docs = $(wildcard *.adoc)            (1)
pdfs = $(docs:.adoc=.pdf)            (2)
all: $(pdfs)                         (3)
.PHONY: all

# Call asciidoctor to generate $@ from $^
%.pdf: %.adoc
	asciidoctor-pdf $^ -o build/$@     (4)
1Wildcard of all files with extension adoc in this directory
2Map .pdf instead of .adoc
3Run on all targets
4$@ is the PDF-File,
$^ is the Source

So just running make will create all documents converted in pdf.

Extend the Makefile

Create all documents converted to html and pdf in extra folders. Add commandline option variable.

docs := $(wildcard *.adoc)
pdfs := $(docs:.adoc=.pdf)
htmls := $(docs:.adoc=.html)
options := -a toc -a toclevels="1"
all: html pdf
pdf: $(pdfs)
html: $(htmls)
.PHONY: all pdf html

# Call asciidoctor to generate $@ from $^
%.pdf: %.adoc
	asciidoctor-pdf $^ $(options) -o build/pdf/$@

%.html: %.adoc
	asciidoctor $^ $(options) -o build/html/$@

So now running make creates html and pdf targets.

Figure 1. Example with our recipe collection
makefile tree
Figure 2. Tree with source and target files

You can use this Makefile on Linux, Mac OS or Windows (with WSL) to convert a large scale of Asciidoctor files to your target.