mount and rss
[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 USERGLOBAL1="$(USERGLOBAL1)"
26         export USERGLOBAL2="$(USERGLOBAL2)"
27 endef
28
29 # usage: export_article [input md]
30 define export_article
31         export ARTICLE_TITLE=`./$(MODULE_DIR)/md-header "$(1)" name`
32         export PUBLISHED_DATE=`./$(MODULE_DIR)/md-header "$(1)" published`
33         export DESCRIPTION=`./$(MODULE_DIR)/md-header "$(1)" description`
34         export USERLOCAL1=`./$(MODULE_DIR)/md-header "$(1)" userlocal1`
35         export USERLOCAL2=`./$(MODULE_DIR)/md-header "$(1)" userlocal2`
36         # vvv this is sorta ugly vvv
37         export ARTICLE_URL="$(SITEURL)/$(ARTICLE_PATH)/$(subst .md,.html,$(notdir "$(1)"))"
38 endef
39
40 # usage: loop_article [dest] [template_filename]
41 define loop_article
42         for article_file in `ls $(SRC_DIR)`; do
43                 $(call export_article,"$(SRC_DIR)/$$article_file")
44                 envsubst < $(TEMPLATE_DIR)/$(2) | sed -i "/<!-- $(shell echo $(addprefix mount_,$(basename $(basename $(2)))) | tr '[:lower:]' '[:upper:]') -->/r /dev/stdin" "$(1)"
45         done
46 endef
47
48 help:
49         echo 'pinopress help|build|clean'
50
51 build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(addprefix $(BUILD_DIR)/$(ARTICLE_PATH)/,$(addsuffix .html,$(ARTICLE_LIST)))
52
53 .ONESHELL:
54 $(BUILD_DIR)/rolling.html:
55         echo 'building rolling file'
56         $(export_common)
57         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/rollingbody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
58         $(call loop_article,"$@",rollingitem.template.html)
59         envsubst < "$@" | sponge "$@"
60
61 .ONESHELL:
62 $(BUILD_DIR)/archive.html:
63         echo 'building archive file'
64         $(export_common)
65         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/archivebody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
66         $(call loop_article,"$@",archiveitem.template.html)
67         envsubst < "$@" | sponge "$@"
68
69 .ONESHELL:
70 $(BUILD_DIR)/feed.xml:
71         echo 'building feed file'
72         $(export_common)
73         cat $(TEMPLATE_DIR)/feed.template.xml > "$@"
74         $(call loop_article,"$@",feeditem.template.xml)
75         envsubst < "$@" | sponge "$@"
76
77 .ONESHELL:
78 $(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
79         echo "parsing $<"
80         $(export_common)
81         $(call export_article,"$<")
82         cat $(TEMPLATE_DIR)/head.template.html > "$@"
83         # module pipeline starts here
84         ./$(MODULE_DIR)/md "$^" >> "$@"
85         # module pipeline ends here
86         cat $(TEMPLATE_DIR)/foot.template.html >> "$@"
87         envsubst < "$@" | sponge "$@"
88
89 clean:
90         echo 'cleaning...'
91         rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(BUILD_DIR)/$(ARTICLE_PATH)/*
92