envsubst
[pinopress.git] / pinopress
1 #!/usr/bin/make -sf
2
3 SITEURL := https://blog.danieliu.xyz
4 USERVAR1 :=
5 USERVAR2 :=
6
7 .PHONY: help build clean
8
9 # internal variables
10 TEMPLATE_DIR := templates
11 BUILD_DIR := build
12 SRC_DIR := articles
13 MODULE_DIR := modules
14
15 ARTICLE_PATH := $(BUILD_DIR)/article
16 ARTICLE_LIST := $(basename $(shell ls $(SRC_DIR)))
17
18 # some helper functions
19 define export_common
20         export SITEURL="$(SITEURL)"
21         export USERVAR1="$(USERVAR1)"
22         export USERVAR2="$(USERVAR2)"
23 endef
24
25 help:
26         echo 'pinopress help|build|clean'
27
28 build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(ARTICLE_PATH)/$(addsuffix .html,$(ARTICLE_LIST))
29
30 .ONESHELL:
31 $(BUILD_DIR)/rolling.html:
32         echo 'building rolling file'
33         $(export_common)
34         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/rollingbody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
35         envsubst < "$@" | sponge "$@"
36
37 .ONESHELL:
38 $(BUILD_DIR)/archive.html:
39         echo 'building archive file'
40         $(export_common)
41         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/archivebody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
42         envsubst < "$@" | sponge "$@"
43
44 .ONESHELL:
45 $(ARTICLE_PATH)/%.html: articles/%.md
46         echo 'parsing $^'
47         $(export_common)
48         export ARTICLE_TITLE="$(shell ./$(MODULE_DIR)/md-header "$<" name)"
49         export PUBLISHED_DATE="$(shell ./$(MODULE_DIR)/md-header "$<" published)"
50         cat $(TEMPLATE_DIR)/head.template.html > "$@"
51         # module pipeline starts here
52         ./$(MODULE_DIR)/md "$^" >> "$@"
53         # module pipeline ends here
54         cat $(TEMPLATE_DIR)/foot.template.html >> "$@"
55         envsubst < "$@" | sponge "$@"
56
57 clean:
58         echo 'cleaning...'
59         rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(ARTICLE_PATH)/*
60