mount and rss
[pinopress.git] / pinopress
index d056dab..b457b13 100755 (executable)
--- a/pinopress
+++ b/pinopress
@@ -1,21 +1,92 @@
-#!/usr/bin/make -f
+#!/usr/bin/make -sf
 
+## pinopress - the simple static blog generator
+## dependencies: sponge
+
+# user variables
 SITEURL := https://blog.danieliu.xyz
+USERGLOBAL1 :=
+USERGLOBAL2 :=
 
 .PHONY: help build clean
 
 # internal variables
 TEMPLATE_DIR := templates
 BUILD_DIR := build
-ARTICLE_DIR := articles
+SRC_DIR := articles
+MODULE_DIR := modules
+
+ARTICLE_PATH := article
+ARTICLE_LIST := $(basename $(shell ls $(SRC_DIR)))
+
+# some helper functions
+define export_common
+       export SITEURL="$(SITEURL)"
+       export USERGLOBAL1="$(USERGLOBAL1)"
+       export USERGLOBAL2="$(USERGLOBAL2)"
+endef
 
-ARTICLE_LIST := $(basename $(shell ls $(ARTICLE_DIR)))
+# usage: export_article [input md]
+define export_article
+       export ARTICLE_TITLE=`./$(MODULE_DIR)/md-header "$(1)" name`
+       export PUBLISHED_DATE=`./$(MODULE_DIR)/md-header "$(1)" published`
+       export DESCRIPTION=`./$(MODULE_DIR)/md-header "$(1)" description`
+       export USERLOCAL1=`./$(MODULE_DIR)/md-header "$(1)" userlocal1`
+       export USERLOCAL2=`./$(MODULE_DIR)/md-header "$(1)" userlocal2`
+       # vvv this is sorta ugly vvv
+       export ARTICLE_URL="$(SITEURL)/$(ARTICLE_PATH)/$(subst .md,.html,$(notdir "$(1)"))"
+endef
+
+# usage: loop_article [dest] [template_filename]
+define loop_article
+       for article_file in `ls $(SRC_DIR)`; do
+               $(call export_article,"$(SRC_DIR)/$$article_file")
+               envsubst < $(TEMPLATE_DIR)/$(2) | sed -i "/<!-- $(shell echo $(addprefix mount_,$(basename $(basename $(2)))) | tr '[:lower:]' '[:upper:]') -->/r /dev/stdin" "$(1)"
+       done
+endef
 
 help:
-       @echo 'pinopress help|build|clean'
+       echo 'pinopress help|build|clean'
+
+build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(addprefix $(BUILD_DIR)/$(ARTICLE_PATH)/,$(addsuffix .html,$(ARTICLE_LIST)))
 
-build:
-       @echo 'building...'
+.ONESHELL:
+$(BUILD_DIR)/rolling.html:
+       echo 'building rolling file'
+       $(export_common)
+       cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/rollingbody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
+       $(call loop_article,"$@",rollingitem.template.html)
+       envsubst < "$@" | sponge "$@"
+
+.ONESHELL:
+$(BUILD_DIR)/archive.html:
+       echo 'building archive file'
+       $(export_common)
+       cat $(TEMPLATE_DIR)/head.template.html $(TEMPLATE_DIR)/archivebody.template.html $(TEMPLATE_DIR)/foot.template.html > "$@"
+       $(call loop_article,"$@",archiveitem.template.html)
+       envsubst < "$@" | sponge "$@"
+
+.ONESHELL:
+$(BUILD_DIR)/feed.xml:
+       echo 'building feed file'
+       $(export_common)
+       cat $(TEMPLATE_DIR)/feed.template.xml > "$@"
+       $(call loop_article,"$@",feeditem.template.xml)
+       envsubst < "$@" | sponge "$@"
+
+.ONESHELL:
+$(BUILD_DIR)/$(ARTICLE_PATH)/%.html: $(SRC_DIR)/%.md
+       echo "parsing $<"
+       $(export_common)
+       $(call export_article,"$<")
+       cat $(TEMPLATE_DIR)/head.template.html > "$@"
+       # module pipeline starts here
+       ./$(MODULE_DIR)/md "$^" >> "$@"
+       # module pipeline ends here
+       cat $(TEMPLATE_DIR)/foot.template.html >> "$@"
+       envsubst < "$@" | sponge "$@"
 
 clean:
-       @echo 'cleaning...'
+       echo 'cleaning...'
+       rm $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(BUILD_DIR)/feed.xml $(BUILD_DIR)/$(ARTICLE_PATH)/*
+