#!/usr/bin/make -f
# yes, there are bash-isms
SHELL = /bin/bash
export SHELL
BLOG := $(MAKE) -f $(lastword $(MAKEFILE_LIST)) --no-print-directory
ifneq ($(filter-out help,$(MAKECMDGOALS)),)
include config
endif
# The following can be configured in config
BLOG_DATE_FORMAT_INDEX ?= %x
BLOG_DATE_FORMAT ?= %x %X
BLOG_TITLE ?= trevcan's blog
BLOG_DESCRIPTION ?= the blog by trevcan, a.k.a. Hector Canizales
BLOG_URL_ROOT ?= blog
BLOG_FEED_MAX ?= 1000
BLOG_HTTPS_LINK := https://blog.thetrevor.tech
BLOG_FEEDS ?= rss atom
#BLOG_FEEDS ?= rss
# rss stuff.
BLOG_FEED_ROOT := https://blog.thetrevor.tech
BLOG_SRC ?= articles
BLOG_WEBSITE_SHELL_USER ?= trevcan
BLOG_WEBSITE_SHELL_HOST ?= internets
BLOG_WEBSITE_SHELL_PATH ?= /dev/human
BLOG_WEBSITE_SHELL_PROMPT ?= $(BLOG_WEBSITE_SHELL_USER)@$(BLOG_WEBSITE_SHELL_HOST) $(BLOG_WEBSITE_SHELL_PATH)
BLOG_WEBSITE_NAME ?= $(BLOG_WEBSITE_SHELL_PROMPT) $
BLOG_AUTHOR_ALIAS ?= trevcan
BLOG_AUTHOR ?= Hector Canizales
BLOG_REDIRECT_TARGET ?=
BLOG_REDIRECT_TIME ?= 3
.PHONY: help init build deploy clean redirect redirects
ARTICLES = $(shell git ls-tree HEAD --name-only -- $(BLOG_SRC)/ 2>/dev/null)
TAGFILES = $(patsubst $(BLOG_SRC)/%.md,tags/%,$(ARTICLES))
help:
$(info blogit init|build|deploy|clean)
init:
mkdir -p $(BLOG_SRC) data templates
# uncomment these lines to get a more 'vanilla' experience of blogit
#
# printf '
$$TITLE' > templates/header.html
# printf '' > templates/footer.html
# printf '' > templates/index_header.html
# printf 'Tags:' > templates/tag_list_header.html
# printf '$$NAME' > templates/tag_entry.html
# printf ', ' > templates/tag_separator.html
# printf '
' > templates/tag_list_footer.html
# printf 'Articles
' > templates/article_list_header.html
# printf '- $$DATE $$TITLE
' > templates/article_entry.html
# printf '' > templates/article_separator.html
# printf '
' > templates/article_list_footer.html
# printf '' > templates/index_footer.html
# printf '' > templates/tag_index_header.html
# printf '' > templates/tag_index_footer.html
# printf '$$TITLE
' > templates/article_header.html
# printf '' > templates/article_footer.html
printf 'blog\n' > .git/info/exclude
build: blog/index.html tagpages $(patsubst $(BLOG_SRC)/%.md,blog/%.html,$(ARTICLES)) $(patsubst %,blog/%.xml,$(BLOG_FEEDS)) redirects
deploy: build
rsync -rLtvz $(BLOG_RSYNC_OPTS) blog/ data/ $(BLOG_REMOTE)
clean:
rm -rf blog tags
config:
printf 'BLOG_REMOTE:=%s\n' \
'$(shell printf "Blog remote (eg: host:/var/www/html): ">/dev/tty; head -n1)' \
> $@
tags/%: $(BLOG_SRC)/%.md
mkdir -p tags
grep -i '^; *tags:' "$<" | cut -d: -f2- | sed 's/ */\n/g' | sed '/^$$/d' | sort -u > $@
blog/index.html: $(ARTICLES) $(TAGFILES) $(addprefix templates/,$(addsuffix .html,header index_header tag_list_header tag_entry tag_separator tag_list_footer article_list_header article_entry article_separator article_list_footer index_footer footer))
mkdir -p blog
TITLE="$(BLOG_TITLE)"; \
export TITLE; \
export BLOG_WEBSITE_NAME="$(BLOG_WEBSITE_NAME)"; \
export BLOG_AUTHOR_ALIAS="$(BLOG_AUTHOR_ALIAS)"; \
export BLOG_AUTHOR="$(BLOG_AUTHOR)"; \
export BLOG_URL_ROOT="$(BLOG_URL_ROOT)" ; \
export BLOG_HTTPS_LINK="$(BLOG_HTTPS_LINK)" ; \
envsubst < templates/header.html > $@; \
envsubst < templates/index_header.html >> $@; \
envsubst < templates/tag_list_header.html >> $@; \
first=true; \
for t in $(shell cat $(TAGFILES) | sort -u); do \
"$$first" || envsubst < templates/tag_separator.html; \
NAME="$$t" \
URL="@$$t.html" \
envsubst < templates/tag_entry.html; \
first=false; \
done >> $@; \
envsubst < templates/tag_list_footer.html >> $@; \
envsubst < templates/article_list_header.html >> $@; \
first=true; \
export LC_ALL=C ; \
for f in $(ARTICLES); do \
printf '%s ' "$$f"; \
git log --diff-filter=A --date="format:%s $(BLOG_DATE_FORMAT_INDEX)" --pretty=format:'%ad%n' -- "$$f"; \
done | sort -k2nr | cut -d" " -f1,3- | while IFS=" " read -r FILE DATE; do \
"$$first" || envsubst < templates/article_separator.html; \
URL="`printf '%s' "\$$FILE" | sed 's,^$(BLOG_SRC)/\(.*\).md,\1,'`.html" \
DATE="$$DATE" \
TITLE="`head -n1 "\$$FILE" | sed -e 's/^# //g'`" \
envsubst < templates/article_entry.html; \
first=false; \
done >> $@; \
envsubst < templates/article_list_footer.html >> $@; \
envsubst < templates/index_footer.html >> $@; \
envsubst < templates/footer.html >> $@; \
blog/tag/%.html: $(ARTICLES) $(addprefix templates/,$(addsuffix .html,header tag_header index_entry tag_footer footer))
.PHONY: tagpages
tagpages: $(TAGFILES)
+$(BLOG) $(patsubst %,blog/@%.html,$(shell cat $(TAGFILES) | sort -u))
blog/@%.html: $(TAGFILES) $(addprefix templates/,$(addsuffix .html,header tag_index_header tag_list_header tag_entry tag_separator tag_list_footer article_list_header article_entry article_separator article_list_footer tag_index_footer footer))
mkdir -p blog
TITLE="Articles tagged $*"; \
TAGS="$*"; \
export TITLE; \
export BLOG_WEBSITE_NAME="$(BLOG_WEBSITE_NAME)"; \
export BLOG_AUTHOR_ALIAS="$(BLOG_AUTHOR_ALIAS)"; \
export BLOG_AUTHOR="$(BLOG_AUTHOR)"; \
export TAGS; \
envsubst < templates/header.html > $@; \
envsubst < templates/tag_index_header.html >> $@; \
envsubst < templates/article_list_header.html >> $@; \
first=true; \
export LC_ALL=C ; \
for f in $(shell grep -FH '$*' $(TAGFILES) | sed 's,^tags/\([^:]*\):.*,$(BLOG_SRC)/\1.md,'); do \
printf '%s ' "$$f"; \
git log --diff-filter=A --date="format:%s $(BLOG_DATE_FORMAT_INDEX)" --pretty=format:'%ad%n' -- "$$f"; \
done | sort -k2nr | cut -d" " -f1,3- | while IFS=" " read -r FILE DATE; do \
"$$first" || envsubst < templates/article_separator.html; \
URL="`printf '%s' "\$$FILE" | sed 's,^$(BLOG_SRC)/\(.*\).md,\1,'`.html" \
DATE="$$DATE" \
TITLE="`head -n1 "\$$FILE" | sed -e 's/^# //g'`" \
envsubst < templates/article_entry.html; \
first=false; \
done >> $@; \
envsubst < templates/article_list_footer.html >> $@; \
envsubst < templates/tag_index_footer.html >> $@; \
envsubst < templates/footer.html >> $@; \
blog/%.html: $(BLOG_SRC)/%.md $(addprefix templates/,$(addsuffix .html,header article_header article_footer footer))
mkdir -p blog
TITLE="$(shell head -n1 $<)"; \
export TITLE; \
export BLOG_WEBSITE_NAME="$(BLOG_WEBSITE_NAME)"; \
export BLOG_AUTHOR_ALIAS="$(BLOG_AUTHOR_ALIAS)"; \
export BLOG_AUTHOR="$(BLOG_AUTHOR)"; \
export BLOG_URL_ROOT="$(BLOG_URL_ROOT)"; \
AUTHOR="$(shell git log -n 1 --reverse --format="%cn" -- "$<")"; \
export AUTHOR; \
DATE_POSTED="$(shell git log --diff-filter=A --date="format:$(BLOG_DATE_FORMAT)" --pretty=format:'%ad' -- "$<")"; \
export DATE_POSTED; \
DATE_EDITED="$(shell git log -n 1 --date="format:$(BLOG_DATE_FORMAT)" --pretty=format:'%ad' -- "$<")"; \
export DATE_EDITED; \
echo "--------------------"; \
echo " LASTTT edit date is... $$DATE_EDITED " ; \
TAGS="$(shell grep -i '^; *tags:' "$<" | cut -d: -f2- | paste -sd ',')"; \
export TAGS; \
envsubst < templates/header.html > $@; \
envsubst < templates/article_header.html >> $@; \
sed -e '/^;/d' < $< | tail -n +2 | markdown -f fencedcode,toc >> $@; \
envsubst < templates/article_footer.html >> $@; \
envsubst < templates/footer.html >> $@; \
# grep '^;history: yes' < $<; \
# if [ $$? -eq 0 ]; \
# ARTICLE_FILE_NAME="$${$${$$@/blog\/}%%.html}.md" \
# URL_BASE="https://git.thetrevor.tech/blog.git/log/articles/" \
# HISTORY_ARTICLE_URL=$$URL_BASE$$ARTICLE_FILE_NAME \
# export HISTORY_ARTICLE_URL; \
# envsubst < templates/article_history.html > $@; \
# echo is 0; \
# fi \
#
# #
blog/rss.xml: $(ARTICLES)
printf '\n\n' > $@
printf '\t\n\t%s\n\t$(BLOG_FEED_ROOT)/index.html\n\t%s\n' \
"$(BLOG_TITLE)" "$(BLOG_DESCRIPTION)" >> $@ ;
for f in $(ARTICLES); do \
printf '%s ' "$$f"; \
git log --diff-filter=A --date="format:%s %a, %d %b %Y %H:%M:%S %z" --pretty=format:'%ad%n' -- "$$f"; \
done | sort -k2nr | head -n $(BLOG_FEED_MAX) | cut -d" " -f1,3- | while IFS=" " read -r FILE DATE; do \
printf '\t\t- \n\t\t%s\n\t\t%s\n\t\t%s\n\t\t%s\n' \
"`head -n 1 $$FILE`" \
"$(BLOG_FEED_ROOT)/`basename $$FILE .md`.html" \
"$(BLOG_FEED_ROOT)/`basename $$FILE .md`.html" \
"$$DATE" ; \
printf '\t\t\n\t\t\n\t\t
\n'; \
done >> $@
printf '\n\n' >> $@
blog/atom.xml: $(ARTICLES)
printf '\n\n' > $@
printf '\n\n' >> $@
#printf '\t\n\t%s\n\t$(BLOG_FEED_ROOT)/index.html\n\t%s\n' \
# "$(BLOG_TITLE)" "$(BLOG_DESCRIPTION)" >> $@ ;
printf '\t%s\n\t%s\n\t\n\t\n\ttrevcan\n\t%s\n\t%s\n\n' \
"$(BLOG_TITLE)" \
"$(BLOG_DESCRIPTION)" \
"$(BLOG_FEED_ROOT)" \
"$(BLOG_FEED_ROOT)/atom.xml" \
"https://blog.thetrevor.tech/atom.xml" \
"$(shell date +%Y-%m-%dT%H:%M:%SZ)" >> $@ # last updated on...
for f in $(ARTICLES); do \
printf '%s ' "$$f";\
git log --diff-filter=A --date="format:%s %a, %d %b %Y %H:%M:%S %z" --pretty=format:'%ad%n' -- "$$f"; \
done | sort -k2nr | head -n $(BLOG_FEED_MAX) | cut -d" " -f1,3- | while IFS=" " read -r FILE DATE; do \
printf '\t\t\n\t\t\n\t\t\n%s\n\t\t%s\n' \
"`head -n 1 $$FILE`" \
"$(BLOG_FEED_ROOT)/`basename $$FILE .md`.html" \
"$(BLOG_FEED_ROOT)/`basename $$FILE .md`.html" \
"`date -d \"$$DATE\" +%Y-%m-%dT%H:%m:%S%:z`"; \
printf '\t\t\n\t\t\n'; \
done >> $@
printf '\n' >> $@
# tail -n +49 blog/`basename $$FILE .md`.html | head -n 20 ; \
# "$(BLOG_TITLE)" "$(BLOG_DESCRIPTION)" "$(shell date +%Y-%m-%dT%H:%M:%SZ)" "hhref" "$(BLOG_URL_ROOT)/atom.xml" "$(BLOG_URL_ROOT)/atom.xml" > $@
#
# rss xml:
# for f in $(ARTICLES); do \
# printf '%s ' "$$f"; \
# git log --diff-filter=A --date="format:%s %a, %d %b %Y %H:%M:%S %z" --pretty=format:'%ad%n' -- "$$f"; \
# done | sort -k2nr | head -n $(BLOG_FEED_MAX) | cut -d" " -f1,3- | while IFS=" " read -r FILE DATE; do \
# printf '\t\t- \n\t\t%s\n\t\t%s\n\t\t%s\n\t\t%s\n' \
# "`head -n 1 $$FILE`" \
# "$(BLOG_FEED_ROOT)/`basename $$FILE .md`.html" \
# "$(BLOG_FEED_ROOT)/`basename $$FILE .md`.html" \
# "$$DATE" ; \
# printf '\t\t\n\t\t\n\t\t
\n'; \
# done >> $@
# printf '\n' >> $@
#****************
# printf '\n\n%s\n%s\n%s\n\n%s\n\n' \
# "$(BLOG_TITLE)" "$(BLOG_DESCRIPTION)" "$(shell date +%Y-%m-%dT%H:%M:%SZ)" "hhref" "$(BLOG_URL_ROOT)/atom.xml" "$(BLOG_URL_ROOT)/atom.xml" > $@
# for f in $(ARTICLES); do \
# printf '%s ' "$$f"; \
# git log --diff-filter=A --date="format:%s %Y-%m-%dT%H:%M:%SZ" --pretty=format:'%ad %aN%n' -- "$$f"; \
# done | sort -k2nr | head -n $(BLOG_FEED_MAX) | cut -d" " -f1,3- | while IFS=" " read -r FILE DATE AUTHOR; do \
# printf '\n%s\n\n%s\n%s\n%s\n%s\n' \
# "`head -n 1 $$FILE`" \
# "`basename $$FILE .md`.html" \
# "`basename $$FILE .md`.html" \
# "$$DATE" \
# "`git log -n 1 --date="format:%Y-%m-%dT%H:%M:%SZ" --pretty=format:'%ad' -- "$$FILE"`" \
# "$$AUTHOR" \
# printf '\t\t \n\t\t\n\t\t\n'; \
# done >> $@
# printf '\n' >> $@
redirect: redirects
redirects:
for target in $$(find data/ -name '*.redirect'); do \
printf "$$target \n\t " ; \
printf "Found\n\t$$target\n\t\t" ; \
cat $$target; \
target_html=$${$\target%.redirect}.html ; \
if [[ ! -a $$target_html ]] ; then \
echo "$${new}" ; \
cat $$target ; \
BLOG_REDIRECT_TARGET="$$(cat $$target | tr -d '\n')" ; \
export BLOG_REDIRECT_TITLE="$$(./get_title.sh $$BLOG_REDIRECT_TARGET)" ; \
export BLOG_REDIRECT_TARGET="$$(cat $$target)" ; \
export BLOG_REDIRECT_TIME="$(BLOG_REDIRECT_TIME)" ; \
export BLOG_AUTHOR="$(BLOG_AUTHOR)"; \
export BLOG_AUTHOR_ALIAS="$(BLOG_AUTHOR_ALIAS)"; \
export TITLE="$(BLOG_TITLE)"; \
export BLOG_URL_ROOT="$(BLOG_URL_ROOT)" ; \
export BLOG_WEBSITE_NAME="$(BLOG_WEBSITE_NAME)"; \
envsubst < templates/redirect.html > $$target_html ; \
fi ; \
done; \
# let "half=`wc -l`/2 "
# let "LINES_TARGET=$(wc -l | cut --delimiter= --fields=1 ) / 2"; \
# echo $LINES_TARGET