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