From c818f2b61c75195cf7adb542dd443e37feea7b59 Mon Sep 17 00:00:00 2001 From: Kenny Ballou Date: Fri, 17 Aug 2018 22:30:27 -0600 Subject: Convert to Org-mode and Pandoc Create some fairly straight-forward conversion scripts to generate a static site from Emacs Org-mode files using Pandoc to generate HTML files. --- .gitignore | 3 ++ Makefile | 61 ++++++++++++++++++++++++++++------- scripts/generate_index_html.sh | 13 ++++++++ scripts/generate_post_html.sh | 22 +++++++++++++ scripts/generate_post_preview.sh | 10 ++++++ scripts/generate_post_summary_html.sh | 24 ++++++++++++++ scripts/generate_post_summary_xml.sh | 21 ++++++++++++ scripts/generate_rss.sh | 19 +++++++++++ scripts/org-get-slug.sh | 8 +++++ scripts/org-metadata.sh | 20 ++++++++++++ scripts/site-templates.sh | 6 ++++ templates/html_footer.html | 14 ++++++++ templates/html_header.html | 23 +++++++++++++ templates/html_sub_header.html | 42 ++++++++++++++++++++++++ 14 files changed, 275 insertions(+), 11 deletions(-) create mode 100755 scripts/generate_index_html.sh create mode 100755 scripts/generate_post_html.sh create mode 100755 scripts/generate_post_preview.sh create mode 100755 scripts/generate_post_summary_html.sh create mode 100755 scripts/generate_post_summary_xml.sh create mode 100755 scripts/generate_rss.sh create mode 100755 scripts/org-get-slug.sh create mode 100755 scripts/org-metadata.sh create mode 100755 scripts/site-templates.sh create mode 100644 templates/html_footer.html create mode 100644 templates/html_header.html create mode 100644 templates/html_sub_header.html diff --git a/.gitignore b/.gitignore index 567609b..59ad424 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ build/ +posts/*.preview.org +posts/*.html +posts/*.xml diff --git a/Makefile b/Makefile index 35b25ea..abf2739 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,54 @@ -OUTPUT=`pwd`/build +PROJ_ROOT:=$(shell git rev-parse --show-toplevel) +SCRIPTS_DIR:=$(PROJ_ROOT)/scripts +STATIC_DIR:=static +IMAGES_DIR:=$(STATIC_DIR)/media +VIDEOS_DIR:=$(STATIC_DIR)/media/videos +BUILD_DIR:=build +blog_dir = $(shell $(SCRIPTS_DIR)/org-get-slug.sh $(1)) +POSTS_ORG_INPUT:=$(wildcard posts/*.org) +POSTS_ORG_SUM_XML_OUTPUT:=$(patsubst posts/%.org, posts/%.sum.xml, $(POSTS_ORG_INPUT)) +POSTS_ORG_SUM_OUTPUT:=$(patsubst posts/%.org, posts/%.sum.html, $(POSTS_ORG_INPUT)) +POSTS_ORG_HTML_OUTPUT:=$(foreach post,$(POSTS_ORG_INPUT),$(BUILD_DIR)$(call blog_dir,$(post))/index.html) +STATIC_FILES:=$(shell find $(STATIC_DIR) -type f) +STATIC_FILES_OUT:=$(patsubst $(STATIC_DIR)/%,$(BUILD_DIR)/%,$(STATIC_FILES)) -all: build +.PHONY: all +all: $(BUILD_DIR)/index.html \ + $(BUILD_DIR)/index.xml \ + $(POSTS_ORG_HTML_OUTPUT) \ + $(STATIC_FILES_OUT) -.PHONY: build -build: - @hugo -d ${OUTPUT} +posts/%.preview.org: posts/%.org + $(SCRIPTS_DIR)/generate_post_preview.sh $< > $@ -.PHONY: deploy -deploy: build - @rsync -avz ${OUTPUT}/ kennyballou.com:/srv/www/blog/. +posts/%.sum.html: posts/%.org posts/%.preview.org + $(SCRIPTS_DIR)/generate_post_summary_html.sh $^ > $@ -.PHONY: serve -serve: - -hugo -d ${OUTPUT} serve +posts/%.sum.xml: posts/%.org posts/%.preview.org + $(SCRIPTS_DIR)/generate_post_summary_xml.sh $^ > $@ + +$(BUILD_DIR): + mkdir -p $@ + +$(BUILD_DIR)/index.html: $(POSTS_ORG_SUM_OUTPUT) | $(BUILD_DIR) + $(SCRIPTS_DIR)/generate_index_html.sh $^ > $@ + +$(BUILD_DIR)/index.xml: $(POSTS_ORG_SUM_XML_OUTPUT) | $(BUILD_DIR) + $(SCRIPTS_DIR)/generate_rss.sh $^ > $@ + +define BLOG_BUILD_DEF +$(BUILD_DIR)$(call blog_dir,$T): + mkdir -p $$@ +$(BUILD_DIR)$(call blog_dir,$T)/index.html: $T | $(BUILD_DIR)$(call blog_dir,$T) + $(SCRIPTS_DIR)/generate_post_html.sh $$< > $$@ +endef + +$(foreach T,$(POSTS_ORG_INPUT),$(eval $(BLOG_BUILD_DEF))) + +$(BUILD_DIR)/%: $(STATIC_DIR)/% | $(BUILD_DIR) + mkdir -p $(dir $@) + cp $< $@ + +.PHONY: clean +clean: + -rm -r $(BUILD_DIR) diff --git a/scripts/generate_index_html.sh b/scripts/generate_index_html.sh new file mode 100755 index 0000000..0a2196a --- /dev/null +++ b/scripts/generate_index_html.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# Generate index.html page + +INPUT_FILES=${@} +PROJ_ROOT=$(git rev-parse --show-toplevel) +source ${PROJ_ROOT}/scripts/site-templates.sh + +cat "${HTML_HEADER_FILE}" +echo "" +cat "${HTML_SUB_HEADER_FILE}" +cat ${INPUT_FILES} | sort -r -n -k1 -k2 -k3 | awk -F' ' '{print $4}' +echo "" +cat "${HTML_FOOTER_FILE}" diff --git a/scripts/generate_post_html.sh b/scripts/generate_post_html.sh new file mode 100755 index 0000000..42608dc --- /dev/null +++ b/scripts/generate_post_html.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Generate HTML for blog post + +ORGIN=${1} +PROJ_ROOT=$(git rev-parse --show-toplevel) +source ${PROJ_ROOT}/scripts/site-templates.sh +source ${PROJ_ROOT}/scripts/org-metadata.sh +DISPLAY_DATE=$(date -d ${DATE} +'%a %b %d, %Y') +SORT_DATE=$(date -d ${DATE} +'%Y %m %d ') + +cat ${HTML_HEADER_FILE} +cat ${HTML_SUB_HEADER_FILE} +echo -n "

${TITLE}

" +echo -n "
" +echo -n '' +echo -n "

${DISPLAY_DATE}

" +pandoc --from org \ + --to html \ + ${ORGIN} +cat ${HTML_FOOTER_FILE} diff --git a/scripts/generate_post_preview.sh b/scripts/generate_post_preview.sh new file mode 100755 index 0000000..2590787 --- /dev/null +++ b/scripts/generate_post_preview.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Generate HTML post summary tags + +ORGIN=${1} +PROJ_ROOT=$(git rev-parse --show-toplevel) + +source ${PROJ_ROOT}/scripts/org-metadata.sh + +echo "${LINKS}" +echo "${PREVIEW}" diff --git a/scripts/generate_post_summary_html.sh b/scripts/generate_post_summary_html.sh new file mode 100755 index 0000000..b184246 --- /dev/null +++ b/scripts/generate_post_summary_html.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Generate HTML post summary tags + +ORGIN=${1} +GENERATED_PREVIEW_FILE=${2} +PROJ_ROOT=$(git rev-parse --show-toplevel) + +source ${PROJ_ROOT}/scripts/org-metadata.sh +DISPLAY_DATE=$(date -d ${DATE} +'%a %b %d, %Y') +SORT_DATE=$(date -d ${DATE} +'%Y %m %d ') +PREVIEW_CONTENT=$(cat ${GENERATED_PREVIEW_FILE} | pandoc -f org -t html) + +echo -n "${SORT_DATE}" +echo -n '
' +echo -n "

${TITLE}

" +echo -n "
${DISPLAY_DATE}
" +echo -n "
$(echo ${PREVIEW_CONTENT})
" +echo -n '' +echo -n '" +echo "" diff --git a/scripts/generate_post_summary_xml.sh b/scripts/generate_post_summary_xml.sh new file mode 100755 index 0000000..b409209 --- /dev/null +++ b/scripts/generate_post_summary_xml.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Generate HTML post summary tags + +ORGIN=${1} +GENERATED_PREVIEW_FILE=${2} +PROJ_ROOT=$(git rev-parse --show-toplevel) + +source ${PROJ_ROOT}/scripts/org-metadata.sh +DISPLAY_DATE=$(date -d ${DATE} +'%a %b %d, %Y') +SORT_DATE=$(date -d ${DATE} +'%Y %m %d ') +PREVIEW_CONTENT=$(cat ${GENERATED_PREVIEW_FILE} | pandoc -f org -t html) + +echo -n "${SORT_DATE}" +echo -n "" +echo -n "${TITLE}" +echo -n "https://kennyballou.com${SLUG}" +echo -n "${SLUG}" +echo -n "${DISPLAY_DATE}" +echo -n "${DESCRIPTION}" +echo -n "" +echo "" diff --git a/scripts/generate_rss.sh b/scripts/generate_rss.sh new file mode 100755 index 0000000..17b0395 --- /dev/null +++ b/scripts/generate_rss.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Generate index.html page + +INPUT_FILES=${@} +PROJ_ROOT=$(git rev-parse --show-toplevel) +source ${PROJ_ROOT}/scripts/site-templates.sh + +echo "" +echo "" +echo "" +echo "~kballou/blog" +echo "https://kennyballou.com" +echo "en-us" +echo "Kenny Ballou" +echo "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License" +echo "$(date --utc --rfc-3339='date')" +cat ${INPUT_FILES} | sort -r -n -k1 -k2 -k3 | awk -F' ' '{print $4}' +echo "" +echo "" diff --git a/scripts/org-get-slug.sh b/scripts/org-get-slug.sh new file mode 100755 index 0000000..07d77cd --- /dev/null +++ b/scripts/org-get-slug.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Generate blog directory structure + +ORGIN=${1} +PROJ_ROOT=$(git rev-parse --show-toplevel) +source ${PROJ_ROOT}/scripts/org-metadata.sh + +echo -n ${SLUG} diff --git a/scripts/org-metadata.sh b/scripts/org-metadata.sh new file mode 100755 index 0000000..ff527ee --- /dev/null +++ b/scripts/org-metadata.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# Extract ORG metadata + +ORGIN=${1} +LC_TIME="C" + +STUB=$(awk -F': ' '/^#\+SLUG:/ { printf "%s", $2}' ${ORGIN}) +DATE=$(awk -F': ' '/^#\+DATE:/ { printf "%s", $2}' ${ORGIN}) +YEAR=$(echo ${DATE} | awk -F'-' '{ print $1 }') +MONTH=$(echo ${DATE} | awk -F'-' '{ print $2 }') +SLUG="/blog/${YEAR}/${MONTH}/${STUB}" +TITLE=$(awk -F': ' '/^#\+TITLE:/ { printf "%s", $2}' ${ORGIN}) +PREVIEW=$(sed -n \ + '/^#+BEGIN_PREVIEW/,/^#+END_PREVIEW/p' \ + ${ORGIN} \ + | head -n-1 \ + | tail -n+2) +DESCRIPTION=$(awk -F': ' '/^#\+DESCRIPTION:/ { printf "%s", $2}' ${ORGIN}) +TAGS=$(awk -F': ' '/^#\+TAGS:/ { $1 = ""; printf "%s\n", $0}' ${ORGIN}) +LINKS=$(sed -n -e '/^#+LINK:/p' ${ORGIN}) diff --git a/scripts/site-templates.sh b/scripts/site-templates.sh new file mode 100755 index 0000000..6ec4b86 --- /dev/null +++ b/scripts/site-templates.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +#template variables + +HTML_HEADER_FILE=${HTML_HEADER_FILE:-templates/html_header.html} +HTML_SUB_HEADER_FILE=${HTML_SUB_HEADER_FILE:-templates/html_sub_header.html} +HTML_FOOTER_FILE=${HTML_FOOTER_FILE:-templates/html_footer.html} diff --git a/templates/html_footer.html b/templates/html_footer.html new file mode 100644 index 0000000..9a99467 --- /dev/null +++ b/templates/html_footer.html @@ -0,0 +1,14 @@ + + + diff --git a/templates/html_header.html b/templates/html_header.html new file mode 100644 index 0000000..9357d2d --- /dev/null +++ b/templates/html_header.html @@ -0,0 +1,23 @@ + + + + + + + + + + ~kb/blog + + + + + + + + + + diff --git a/templates/html_sub_header.html b/templates/html_sub_header.html new file mode 100644 index 0000000..a1f3e03 --- /dev/null +++ b/templates/html_sub_header.html @@ -0,0 +1,42 @@ + -- cgit v1.2.1