b47d785e49e20b4464cdf4e23d49f9ee343c5cb5
[pinopress.git] / pinopress
1 #!/usr/bin/make -sf
2
3 ## pinopress - the simple static blog generator
4 ## dependencies: sponge
5
6 # user variables
7 SITEURL := https://blog.danieliu.xyz
8 USERGLOBAL1 :=
9 USERGLOBAL2 :=
10
11 include config.mk
12
13 .PHONY: help build clean
14
15 # internal variables
16 TEMPLATE_DIR := templates
17 BUILD_DIR := build
18 SRC_DIR := articles
19 MODULE_DIR := modules
20
21 ARTICLE_PATH := article
22 ARTICLE_LIST := $(basename $(shell ls $(SRC_DIR)))
23
24 # some helper functions
25 define export_common
26         export SITEURL="$(SITEURL)"
27         export ARTICLE_PATH="$(ARTICLE_PATH)"
28         export USERGLOBAL1="$(USERGLOBAL1)"
29         export USERGLOBAL2="$(USERGLOBAL2)"
30 endef
31
32 # usage: loop_article [dest] [template_filename]
33 define loop_article
34         for article_file in `ls $(SRC_DIR)`; do
35                 . ./$(MODULE_DIR)/md-header "$(SRC_DIR)/$$article_file"
36                 envsubst < $(TEMPLATE_DIR)/$(2) | sed -i "/<!-- $(shell echo $(addprefix mount_,$(basename $(basename $(2)))) | tr '[:lower:]' '[:upper:]') -->/r /dev/stdin" "$(1)"
37         done
38 endef
39
40 help:
41         echo 'pinopress help|build|clean'
42
43 build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(addprefix $(BUILD_DIR)/$(ARTICLE_PATH)/,$(addsuffix .html,$(ARTICLE_LIST)))
44
45 .ONESHELL:
46 $(BUILD_DIR)/rolling.html:
47         echo 'building rolling file'
48         $(export_common)
49         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/rollingbody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
50         $(call loop_article,"$@",rollingitem.template.html)
51         envsubst < "$@" | sponge "$@"
52
53 .ONESHELL:
54 $(BUILD_DIR)/archive.html:
55         echo 'building archive file'
56         $(export_common)
57         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/archivebody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
58         $(call loop_article,"$@",archiveitem.template.html)
59         envsubst < "$@" | sponge "$@"
60
61 .ONESHELL:
62 $(BUILD_DIR)/feed.xml:
63         echo 'building feed file'
64         $(export_common)
65         cat $(TEMPLATE_DIR)/feed.template.xml > "$@"
66         $(call loop_article,"$@",feeditem.template.xml)
67         envsubst < "$@" | sponge "$@"
68
69 .ONESHELL:
70 $(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
71         echo "parsing $<"
72         $(export_common)
73         $(call export_article,"$<")
74         cat $(TEMPLATE_DIR)/head.template.html > "$@"
75         # module pipeline starts here
76         ./$(MODULE_DIR)/md "$<" >> "$@"
77         . ./$(MODULE_DIR)/md-header "$<"
78         . ./$(MODULE_DIR)/readingtime "$<"
79         # module pipeline ends here
80         cat $(TEMPLATE_DIR)/foot.template.html >> "$@"
81         envsubst < "$@" | sponge "$@"
82
83 clean:
84         echo 'cleaning...'
85         rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(BUILD_DIR)/$(ARTICLE_PATH)/*
86