Skip to main content

Makefile to process all Asciidoctor files in a directory

·260 words·2 mins

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?

Makefile

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

# Call asciidoctor to generate $@ from $^
%.pdf: %.adoc
    asciidoctor-pdf $^ -o build/$@     
  • Wildcard of all files with extension adoc in this directory

  • Map .pdf instead of .adoc

  • Run on all targets

  • $@ 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.

Makefile

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.

Example with our recipe collection
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.

Christoph Stoettner
Author
Christoph Stoettner
I work at Vegard IT GmbH as a senior consultant, focusing on collaboration software, Kubernetes, security, and automation. I primarily work with HCL Connections, WebSphere Application Server, Kubernetes, Ansible, Terraform, and Linux. My daily work occasionally leads to technical talks and blog articles, which I share here more or less regularly.

Related

Automate hugo

·736 words·4 mins
Mid 2018 I switched my blog from Wordpress to Hugo. Main reason was performance and that I can use Asciidoctor to write the posts. What happened the last 18 months? I stayed with the theme I selected 2018, but I tweaked it a little bit. So I added lunr to implement searching, changed all scripts and fonts from CDN to local (privacy and tracking), updated Bootstrap 3 to 4. Working with Bootstrap was quite fun, I haven’t done a lot of HTML or CSS the last years, but the grid and css classes from Bootstrap are working without checking each change on all browsers and are responsive.