rolling items
[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 USERVAR1 :=
9 USERVAR2 :=
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 USERVAR1="$(USERVAR1)"
26         export USERVAR2="$(USERVAR2)"
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         # vvv this is sorta ugly vvv
34         export ARTICLE_URL="$(SITEURL)/$(ARTICLE_PATH)/$(subst .md,.html,$(notdir "$(1)"))"
35 endef
36
37 help:
38         echo 'pinopress help|build|clean'
39
40 build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(addprefix $(BUILD_DIR)/$(ARTICLE_PATH)/,$(addsuffix .html,$(ARTICLE_LIST)))
41
42 .ONESHELL:
43 $(BUILD_DIR)/rolling.html:
44         echo 'building rolling file'
45         $(export_common)
46         cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/rollingbody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
47         for article_file in `ls $(SRC_DIR)`; do
48                 $(call export_article,"$(SRC_DIR)/$$article_file")
49                 envsubst < $(TEMPLATE_DIR)/rollingitem.template.html | sed -i "/<!-- ROLLING_ITEMS -->/r /dev/stdin" "$@"
50         done
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         envsubst < "$@" | sponge "$@"
59
60 .ONESHELL:
61 $(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
62         echo "parsing $<"
63         $(export_common)
64         $(call export_article,"$<")
65         cat $(TEMPLATE_DIR)/head.template.html > "$@"
66         # module pipeline starts here
67         ./$(MODULE_DIR)/md "$^" >> "$@"
68         # module pipeline ends here
69         cat $(TEMPLATE_DIR)/foot.template.html >> "$@"
70         envsubst < "$@" | sponge "$@"
71
72 clean:
73         echo 'cleaning...'
74         rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/$(ARTICLE_PATH)/*
75