reorganized templates
[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 # usage: build_page [page_template] [item_template] [display_message]
46 define build_page
47         echo "$(3)"
48         $(export_common)
49         cat $(TEMPLATE_DIR)/$(1) > "$@"
50         $(call loop_article,"$@",$(2))
51         envsubst < "$@" | sponge "$@"
52 endef
53
54 .ONESHELL:
55 $(BUILD_DIR)/rolling.html:
56         $(call build_page,rollingpage.template.html,rollingitem.template.html,building rolling file)
57
58 .ONESHELL:
59 $(BUILD_DIR)/archive.html:
60         $(call build_page,archivepage.template.html,archiveitem.template.html,building archive file)
61
62 .ONESHELL:
63 $(BUILD_DIR)/feed.xml:
64         $(call build_page,feed.template.xml,feeditem.template.xml,building feed file)
65
66 .ONESHELL:
67 $(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
68         echo "parsing $<"
69         $(export_common)
70         $(call export_article,"$<")
71         cat $(TEMPLATE_DIR)/articlepage.template.html > "$@"
72         # module pipeline starts here
73         ./$(MODULE_DIR)/md "$<" | sed -i "/<!-- _MOUNT_ARTICLECONTENT -->/r /dev/stdin" "$@"
74         . ./$(MODULE_DIR)/md-header "$<"
75         . ./$(MODULE_DIR)/readingtime "$<"
76         # module pipeline ends here
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