I have a directory structure like
edited/
Betrayal/
index.md
Bloodlines/
cover.jpg
index.md
...
and a Makefile like
PANDOC := pandoc
PANDOC_OPTS := -t epub --smart --toc
EBOOK_CONVERT := ebook-convert
MODULES := Betrayal Bloodlines Tempest Exile Sacrifice Inferno Fury Revelation Invincible
SRC_DIR := $(addprefix edited/,$(MODULES))
BUILD_DIR := $(addprefix build/,$(MODULES))
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.md))
EPUB := $(patsubst edited/%.md,build/%.epub,$(SRC))
MOBI := $(patsubst edited/%.md,build/%.mobi,$(SRC))
vpath %.md $(SRC_DIR)
define make-epub
$1/%.epub: %.md
$(PANDOC) $(PANDOC_OPTS) $$< -o $$@
endef
define make-mobi
$1/%.mobi: $1/%.epub
$(EBOOK_CONVERT) $$< $$@ >/dev/null
endef
.PHONY: all epubs mobis checkdirs clean
all: checkdirs mobis
epubs: $(EPUB)
mobis: $(MOBI)
checkdirs: $(BUILD_DIR)
$(BUILD_DIR):
@mkdir -p $@
clean:
@rm -rfv $(BUILD_DIR)
$(foreach bdir,$(BUILD_DIR),$(eval $(call make-epub,$(bdir))))
$(foreach bdir,$(BUILD_DIR),$(eval $(call make-mobi,$(bdir))))
(I still haven't added conditionals to test for a cover and metadata or added emailing via mutt to kindle, but here comes the current issue)
The only problem is how every directory has an index.md and the VPATH method will always pull the index from the first module ex
pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Betrayal/index.epub
pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Bloodlines/index.epub
pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Tempest/index.epub
pandoc -t epub --smart --toc edited/Betrayal/index.md -o build/Exile/index.epub
...
I could rename each index.md to the unique name of each folder like (for d in *; do echo "$d"; cd "$d"; mv index.md "$d".md; cd ..; done)
and everything would work fine, but I want to learn makefile syntax and there must be a more elegant way to handle src directories without tons of recursive makefiles.