semi passable css
[pinopress.git] / pinopress
index c31657c..5fe147c 100755 (executable)
--- a/pinopress
+++ b/pinopress
@@ -1,6 +1,14 @@
 #!/usr/bin/make -sf
 
+## pinopress - the simple static blog generator
+## dependencies: sponge
+
+# user variables
 SITEURL := https://blog.danieliu.xyz
+USERGLOBAL1 :=
+USERGLOBAL2 :=
+
+include config.mk
 
 .PHONY: help build clean
 
@@ -10,33 +18,68 @@ BUILD_DIR := build
 SRC_DIR := articles
 MODULE_DIR := modules
 
-ARTICLE_PATH := $(BUILD_DIR)/article
-
+ARTICLE_PATH := article
 ARTICLE_LIST := $(basename $(shell ls $(SRC_DIR)))
 
+# some helper functions
+define export_common
+       export SITEURL="$(SITEURL)"
+       export ARTICLE_PATH="$(ARTICLE_PATH)"
+       export USERGLOBAL1="$(USERGLOBAL1)"
+       export USERGLOBAL2="$(USERGLOBAL2)"
+endef
+
+# usage: build_page [page_template] [item_template] [display_message]
+define build_page
+       echo "$(3)"
+       $(export_common)
+       cat $(TEMPLATE_DIR)/$(1) > "$@"
+       for article_file in `ls $(SRC_DIR)`; do
+               . ./$(MODULE_DIR)/md-header "$(SRC_DIR)/$$article_file"
+               envsubst < $(TEMPLATE_DIR)/$(2) | sed -i "/<!-- $(shell echo $(addprefix _mount_,$(basename $(basename $(2)))) | tr '[:lower:]' '[:upper:]') -->/r /dev/stdin" "$(@)"
+       done
+       $(call expand_mount,$@)
+       envsubst < "$@" | sponge "$@"
+endef
+
+# usage: expand_mount [filename]
+define expand_mount
+       for mount in `sed -En 's/<!-- MOUNT_(.+) -->/\1/p' "$(1)"`; do
+               sed -i "/<!-- MOUNT_$$mount -->/r $(TEMPLATE_DIR)/`echo $$mount | tr '[:upper:]' '[:lower:]'`.template.html" "$(1)"
+       done
+endef
+
 help:
        echo 'pinopress help|build|clean'
 
-build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(ARTICLE_PATH)/$(addsuffix .html,$(ARTICLE_LIST))
-       echo 'building...'
+build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(addprefix $(BUILD_DIR)/$(ARTICLE_PATH)/,$(addsuffix .html,$(ARTICLE_LIST)))
 
+.ONESHELL:
 $(BUILD_DIR)/rolling.html:
-       echo 'building rolling file'
-       cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/rollingbody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
+       $(call build_page,rollingpage.template.html,rollingitem.template.html,building rolling file)
 
+.ONESHELL:
 $(BUILD_DIR)/archive.html:
-       echo 'building archive file'
-       cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/archivebody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
+       $(call build_page,archivepage.template.html,archiveitem.template.html,building archive file)
+
+.ONESHELL:
+$(BUILD_DIR)/feed.xml:
+       $(call build_page,feed.template.xml,feeditem.template.xml,building feed file)
 
-$(ARTICLE_PATH)/%.html: articles/%.md
-       echo 'parsing $^'
-       cat $(TEMPLATE_DIR)/head.template.html > "$@"
+.ONESHELL:
+$(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
+       echo "parsing $<"
+       $(export_common)
+       cat $(TEMPLATE_DIR)/articlepage.template.html > "$@"
        # module pipeline starts here
-       ./$(MODULE_DIR)/md "$^" >> "$@"
+       ./$(MODULE_DIR)/md "$<" | sed -i "/<!-- _MOUNT_ARTICLECONTENT -->/r /dev/stdin" "$@"
+       . ./$(MODULE_DIR)/md-header "$<"
+       . ./$(MODULE_DIR)/readingtime "$<"
        # module pipeline ends here
-       cat $(TEMPLATE_DIR)/foot.template.html >> "$@"
+       $(call expand_mount,$@)
+       envsubst < "$@" | sponge "$@"
 
 clean:
        echo 'cleaning...'
-       rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(ARTICLE_PATH)/*
+       rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(BUILD_DIR)/$(ARTICLE_PATH)/*