From d9e98960c517be5806c756518f46827cf59d0471 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Mon, 12 Jul 2021 22:58:32 -0400 Subject: [PATCH] envsubst --- README.md | 21 ++++++++++++++++++++- modules/md | 3 ++- modules/md-header | 17 +++++++++++++++++ modules/md-test | 4 ++++ pinopress | 22 ++++++++++++++++++++-- 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100755 modules/md-header diff --git a/README.md b/README.md index aaa54ad..4eaa2c2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ built in features include markdown generated blog articles, and a rss feed. ## templates -templates are html snippets that you can customize. these templates are then pieced together to form the final html page. **pinopress** makes extensive use of **gnu envsubst** so in each template, you can use variables that will be substituted in on build. +templates are html snippets that you can customize. these templates are then pieced together to form the final html page. edit them to your liking. they can be found in the `templates/` directory. ## modules @@ -20,3 +20,22 @@ here are some potential ideas for modules: pinopress articles are written in an enhanced version of markdown. specification coming soon. +## variables + + **pinopress** makes extensive use of **gnu envsubst** so in each template, you can use variables that will be substituted in on build. variables can be used in both templates and blog articles. Here's a list of some that you can use: + +### global + +`$SITEURL` - the SITEURL variable you set in the pinopress config +`$USERVAR1` - global variable you can use however you want +`$USERVAR2` - a second user defined global variable + +### article only + +`$ARTICLE_TITLE` - title of article, as set in article header +`$PUBLISHED_DATE` - the date/time the article was published, as set in the article header + +## mounts + +coming soon... + diff --git a/modules/md b/modules/md index 717e395..1a4ae98 100755 --- a/modules/md +++ b/modules/md @@ -2,7 +2,8 @@ ## markdown to html module for pinopress ## based off https://github.com/stamby/md-to-html -# input $1 - filepath to html file +## usage: +## md [md_filepath] # remove document meta /^-- \w+: .+$/d diff --git a/modules/md-header b/modules/md-header new file mode 100755 index 0000000..f913fc9 --- /dev/null +++ b/modules/md-header @@ -0,0 +1,17 @@ +#!/bin/sh + +## module to read meta in header of md file +## usage: +## md-header [md_filepath] [header_name] + +[ "$#" -ne 2 ] && { echo "Incorrect usage"; exit 1; } + +propname="" +case "$2" in + name) propname=name;; + published) propname=published;; + tags) propname=tags;; + *) exit 1;; +esac + +sed -nE "s/^-- ${propname}: (.*)$/\1/p" "$1" diff --git a/modules/md-test b/modules/md-test index ec3561f..72e244f 100644 --- a/modules/md-test +++ b/modules/md-test @@ -1,3 +1,7 @@ +-- name: My First Blog Post!!! +-- published: July 11th +-- tags: random + & > < #### diff --git a/pinopress b/pinopress index c31657c..5157f99 100755 --- a/pinopress +++ b/pinopress @@ -1,6 +1,8 @@ #!/usr/bin/make -sf SITEURL := https://blog.danieliu.xyz +USERVAR1 := +USERVAR2 := .PHONY: help build clean @@ -11,30 +13,46 @@ SRC_DIR := articles MODULE_DIR := modules ARTICLE_PATH := $(BUILD_DIR)/article - ARTICLE_LIST := $(basename $(shell ls $(SRC_DIR))) +# some helper functions +define export_common + export SITEURL="$(SITEURL)" + export USERVAR1="$(USERVAR1)" + export USERVAR2="$(USERVAR2)" +endef + help: echo 'pinopress help|build|clean' build: $(BUILD_DIR)/rolling.html $(BUILD_DIR)/archive.html $(ARTICLE_PATH)/$(addsuffix .html,$(ARTICLE_LIST)) - 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 > "$@" + 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 > "$@" + envsubst < "$@" | sponge "$@" +.ONESHELL: $(ARTICLE_PATH)/%.html: articles/%.md echo 'parsing $^' + $(export_common) + export ARTICLE_TITLE="$(shell ./$(MODULE_DIR)/md-header "$<" name)" + export PUBLISHED_DATE="$(shell ./$(MODULE_DIR)/md-header "$<" published)" 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...' -- 2.20.1