Skip to content

Instantly share code, notes, and snippets.

@MLKrisJohnson
Created July 9, 2024 17:29
Show Gist options
  • Save MLKrisJohnson/44ee32d907ecc7b4da9f88e08c67442e to your computer and use it in GitHub Desktop.
Save MLKrisJohnson/44ee32d907ecc7b4da9f88e08c67442e to your computer and use it in GitHub Desktop.
A Makefile which will process all Mermaid and Graphviz files in a directory
# Makefile for Mermaid and Graphviz files in this directory
#
# `make all` - (default target) build all targets
# `make png` - build PNGs for all source files
# `make svg` - build SVGs for all source files
# `make pdf` - build PDFs for all source files
# `make clean` - delete all targets
# `make listsources` - print list of all input files to be processed
# `make listtargets` - print list of all output files
# `npm install -g @mermaid-js/mermaid-cli` to get the `mmdc` utility.
# For more info, see <https://mermaid.js.org/intro/>
MMDC?=mmdc
MMDCFLAGS?=
# `brew install graphviz` to get the `dot` utility.
# For more info, see <https://graphviz.org/>
DOT?=dot
DOTFLAGS?=
# Process all *.mmd files in this directory, producing .png, .svg, and .pdf output.
MMD_SRC_FILES=$(wildcard *.mmd)
MMD_OUT_FILES=$(MMD_SRC_FILES:.mmd=.png) $(MMD_SRC_FILES:.mmd=.svg) $(MMD_SRC_FILES:.mmd=.pdf)
# Process all *.gv files in this directory, producing .png, .svg, and .pdf output.
DOT_SRC_FILES=$(wildcard *.gv)
DOT_OUT_FILES=$(DOT_SRC_FILES:.gv=.png) $(DOT_SRC_FILES:.gv=.svg) $(DOT_SRC_FILES:.gv=.pdf)
SRC_FILES=$(MMD_SRC_FILES) $(DOT_SRC_FILES)
OUT_FILES=$(MMD_OUT_FILES) $(DOT_OUT_FILES)
# mmdc pattern rules
%.png : %.mmd
$(MMDC) --input $< --width 4800 --height 2700 $(MMDCFLAGS) --output $@
%.svg : %.mmd
$(MMDC) --input $< $(MMDCFLAGS) --output $@
%.pdf : %.mmd
$(MMDC) --input $< --pdfFit $(MMDCFLAGS) --output $@
# dot pattern rules
%.png : %.gv
$(DOT) -Tpng $< $(DOTFLAGS) -o $@
%.svg : %.gv
$(DOT) -Tsvg $< $(DOTFLAGS) -o $@
%.pdf : %.gv
$(DOT) -Tpdf $< $(DOTFLAGS) -o $@
all: $(OUT_FILES)
.PHONY: all
png: $(MMD_SRC_FILES:.mmd=.png) $(DOT_SRC_FILES:.gv=.png)
.PHONY: png
svg: $(MMD_SRC_FILES:.mmd=.svg) $(DOT_SRC_FILES:.gv=.svg)
.PHONY: svg
pdf: $(MMD_SRC_FILES:.mmd=.pdf) $(DOT_SRC_FILES:.gv=.pdf)
.PHONY: pdf
listsources:
@ echo $(SRC_FILES)
.PHONY: listsources
listtargets:
@ echo $(OUT_FILES)
.PHONY: listtargets
clean:
- $(RM) $(OUT_FILES)
.PHONY: clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment