semi passable css
[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: build_page [page_template] [item_template] [display_message]
33 define build_page
34         echo "$(3)"
35         $(export_common)
36         cat $(TEMPLATE_DIR)/$(1) > "$@"
37         for article_file in `ls $(SRC_DIR)`; do
38                 . ./$(MODULE_DIR)/md-header "$(SRC_DIR)/$$article_file"
39                 envsubst < $(TEMPLATE_DIR)/$(2) | sed -i "/<!-- $(shell echo $(addprefix _mount_,$(basename $(basename $(2)))) | tr '[:lower:]' '[:upper:]') -->/r /dev/stdin" "$(@)"
40         done
41         $(call expand_mount,$@)
42         envsubst < "$@" | sponge "$@"
43 endef
44
45 # usage: expand_mount [filename]
46 define expand_mount
47         for mount in `sed -En 's/<!-- MOUNT_(.+) -->/\1/p' "$(1)"`; do
48                 sed -i "/<!-- MOUNT_$$mount -->/r $(TEMPLATE_DIR)/`echo $$mount | tr '[:upper:]' '[:lower:]'`.template.html" "$(1)"
49         done
50 endef
51
52 help:
53         echo 'pinopress help|build|clean'
54
55 build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(addprefix $(BUILD_DIR)/$(ARTICLE_PATH)/,$(addsuffix .html,$(ARTICLE_LIST)))
56
57 .ONESHELL:
58 $(BUILD_DIR)/rolling.html:
59         $(call build_page,rollingpage.template.html,rollingitem.template.html,building rolling file)
60
61 .ONESHELL:
62 $(BUILD_DIR)/archive.html:
63         $(call build_page,archivepage.template.html,archiveitem.template.html,building archive file)
64
65 .ONESHELL:
66 $(BUILD_DIR)/feed.xml:
67         $(call build_page,feed.template.xml,feeditem.template.xml,building feed file)
68
69 .ONESHELL:
70 $(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
71         echo "parsing $<"
72         $(export_common)
73         cat $(TEMPLATE_DIR)/articlepage.template.html > "$@"
74         # module pipeline starts here
75         ./$(MODULE_DIR)/md "$<" | sed -i "/<!-- _MOUNT_ARTICLECONTENT -->/r /dev/stdin" "$@"
76         . ./$(MODULE_DIR)/md-header "$<"
77         . ./$(MODULE_DIR)/readingtime "$<"
78         # module pipeline ends here
79         $(call expand_mount,$@)
80         envsubst < "$@" | sponge "$@"
81
82 clean:
83         echo 'cleaning...'
84         rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(BUILD_DIR)/$(ARTICLE_PATH)/*
85