#!/bin/sh blog_domain='https://lucy.moe/' output() { echo "$1" >> index.html } output_rss() { echo "$1" >> rss.xml } add_header() { output '

posts

' output_rss ' Lucy Lucys blog' output_rss " Lucys blog" } add_footer() { html_entry "https://asagi.moe" "before July 2020" "Old site" output '' output_rss '' } html_entry() { output '' path="$1" time="$2" title="$3" output "$title" output "$time" } rss_entry() { # The content is the output minus the first line # (which would otherwise be a redundant title in most rss readers) # and with escaped html. # The sed expression is stolen from https://stackoverflow.com/questions/12873682/12873723#12873723 output_rss " $1 $blog_domain$2 $(tail -n+2 "$2" | sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g') " } create_entry() { path="$9" outpath="content/$(basename "$path" .md).html" # convert new markdown posts to html pandoc "$path" -t html -f markdown -o "$outpath" # then add it to the index title="$(grep -h '^title: ' "$path" | sed 's/title: //g')" created=$(grep -h '^date: ' "$path" | sed 's/date: //g') html_entry "$outpath" "created on $created" "$title" rss_entry "$title" "$outpath" } has_updates() { git fetch &> /dev/null diff="$(git diff master origin/master)" if [ "$diff" ]; then return 0 else return 1 fi } cd /home/nginx/html/blog if has_updates; then git pull &> /dev/null rm -f index.html rm -f rss.xml add_header ls -ltu src/*.md | tail -n+1 | while read f; do create_entry $f; done add_footer # Human-readable output for the cron notification echo 'Updated blog to:' git log -1 fi