shite

git clone _git@git.zakaria.org/shite.git
Log | Files | Refs | README

commit 8e1338b29216b0e6446d5d93b4447f1d53f9dfa3
Author: zakaria <58356365+e-zk@users.noreply.github.com>
Date:   Thu, 25 Feb 2021 16:57:50 +1000

add initial files

Diffstat:
Acommon.rc | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Arss.sh | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aupdate.sh | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 278 insertions(+), 0 deletions(-)

diff --git a/common.rc b/common.rc @@ -0,0 +1,75 @@ +#!/bin/sh + +# +# common variables +# + +# site metadata +fqdn=zakaria.org +base_url="https://${fqdn}" +onion_url="http://g5hwwozzm3co43bu6np2noyhsju7zuok3cqawlbeo4entvfoads5trqd.onion/" +site_name="zakaria's web log" + +# paths +posts_dir="${HOME}/usr/src/www/blog/posts" +posts_index=${posts_dir}/index.html + +# options for lowdown +lowdown_opts='--html-no-escapehtml --html-no-skiphtml' + +# feed location +rss_feed=rss.xml + + +# +# common functions +# + +# log to stderr +log() { + printf '%s: %s\n' "$(basename $0)" "$1" >&2 +} + +# ded af +die() { + log "$1" + exit 1 +} + +# is $1 in the list of excluded files? +is_excluded() { + case "$1" in + index.*) return 0 ;; + *) return 1 ;; + esac + return 1 +} + +# run lowdown on some input +# $1 - markdown input +run_lowdown() { + echo "$@" | lowdown -Thtml ${lowdown_opts} +} + +# separate a filename into parsable chunks (date:title) +# $1 - filename (basename) +parse_fname() { + echo "$1" | \ + sed 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\)-\(.*\)\..*/\1:\2/g' +} + +# get title from markdown file +# $1 - markdown file (path) +md_title() { + head -n 1 "$1" | sed 's/#\ \(.*\)/\1/g' +} + +# convert markdown to plaintext: +# remove code, links, use image alt-text +# input is stdin +md_to_txt() { + sed -E \ + -e 's/`//g' \ + -e 's/!\[(.+)\]\(.+\)/\[img "\1"\]/g' \ + -e 's/\[(.+)\]\(.+\)/\1/g' +} +\ No newline at end of file diff --git a/rss.sh b/rss.sh @@ -0,0 +1,81 @@ +#!/bin/sh +# rss feed generator + + +. ${XDG_CONFIG_HOME:-${HOME}/.config}/shite/common.rc + +exclude_fnames="${exclude_fnames:-index.html index.md}" +rss_baseurl="${base_url}/" +rss_title="${site_name}" +rss_link="${base_url}/posts/" + +# convert input date to RFC2822 +# time and tz are set to 00:00:00 +0000 +# $1 - input date +to_rfc2822() { + date="$1" + format="%F" + rfc2822="+%a, %d %b %Y 00:00:00 +0000" + + # if on Linux, use GNU date syntax + if [ "$(uname)" = "Linux" ]; then + date -d "$date" "$rfc2822" + else + date -j -f "$format" "$date" "$rfc2822" + fi +} + +rss_preamble() { + printf '<rss version="2.0" xml:base="%s">\n' "$rss_baseurl" + printf '<channel>\n' + printf '<title>%s</title>\n' "$rss_title" + printf '<description/>\n' # TODO add description? + printf '<link>%s</link>\n' "$rss_link" +} + +rss_postamble() { + printf '</channel>\n' + printf '</rss>\n' +} + +# remove markdown from title +remove_md() { + sed 's/`//g' +} + +reverse_posts() { + break=0 + posts='' + for file in ${posts_dir}/*.html; do + base="$(basename "$file")" + for ex in ${exclude_fnames}; do + [ "$base" = "$ex" ] && break=1 + done + [ "$break" -eq 1 ] && continue + posts="${posts}\\n${file}" + done + echo "$posts" | tail -r +} + +rss_preamble +for file in $(reverse_posts); do + post_bname="$(basename "$file")" + md="${file%%.*}.md" + + parsed="$(parse_fname "$post_bname")" + + post_date="$(to_rfc2822 "${parsed%%:*}")" + post_title="$(md_title "$md" | remove_md)" + + log "adding \"${post_title}\" @ ${post_date}..." + + printf '<item>\n' + printf '<link>%s/posts/%s</link>\n' "$base_url" "$post_bname" + printf '<title>%s</title>\n' "$post_title" + printf '<pubDate>%s</pubDate>\n' "$post_date" + printf '<description>\n' + lowdown ${lowdown_opts} "$md" + printf '</description>\n' + printf '</item>\n' +done +rss_postamble diff --git a/update.sh b/update.sh @@ -0,0 +1,121 @@ +#!/bin/sh + +. ${XDG_CONFIG_HOME:-${HOME}/.config}/shite/common.rc + + +remove_title() { + awk '{ + if (!(FNR==1) && !($0 ~ /^#.*\n/)) + print $0 + }' +} + +gen_desc() { + post_file="$1" + head -n 3 "$post_file" | sed -e '/#.*/d' -e '/^$/d' | md_to_txt | tr -d '\n' | sed -e 's/\ *$//' + printf ' [...]' +} + +gen_head() { + page_file="$1" + page_title="$2" + + post_bname="$(basename "$page_file")" + + page_url="${base_url}/posts/${post_bname%%.*}.html" + page_desc="$(gen_desc "$page_file")" + + printf '<head>\n' + printf '<meta http-equiv="content-type" content="text/html; charset=utf-8">\n' + printf '<meta name="viewport" content="width=device-width, initial-scale=1">\n' + printf '<meta name="theme-color" content="#101010">\n' + printf '<meta property="og:title" content="%s">\n' "${page_title:-${site_name}}" + printf '<meta property="og:description" content="%s">\n' "${page_desc}" + printf '<meta property="og:url" content="%s">\n' "$page_url" + printf '<meta property="og:type" content="article">\n' + printf '<link rel="icon" href="/favicon.svg">\n' + printf '<link rel="alternate icon" href="/favicon.ico">\n' + printf '<link rel="stylesheet" href="/style.css">\n' + + if [ -n "$page_title" ]; then + printf '<title>%s - %s</title>\n' "$page_title" "$site_name" + else + printf '<title>%s</title>\n' "$site_name" + fi + + printf '</head>\n' +} + +gen_nav() { + printf '<header>\n' + printf '<nav>\n' + printf '<a href="%s">%s</a>\n' "/" "$fqdn" + printf '<span class="right">\n' + printf '<a href="/posts/">blog</a>&nbsp;&nbsp;\n' + printf '<a href="/lists/">lists</a>&nbsp;&nbsp;\n' + printf '<a href="/about.html">about</a>\n' + printf '</span>' + printf '</nav>\n' + printf '</header>\n' +} + +gen_footer() { + post_path="$1" + + post_pt="$(basename "${post_path%%.*}.md")" + + printf '<footer>\n' + printf '<a href="%s/%s">plaintext</a>&nbsp;&nbsp;<a href="%s">onion</a>\n' "$base_url" "$post_pt" "$onion_url" + printf '<span class="right">(c) zakaria <a href="https://creativecommons.org/licenses/by-sa/4.0/">cc by-sa</a></span>\n' + printf '</footer>\n' +} + +gen_info() { + post_created="$1" + post_modified="$2" + + printf '<p class="info">\n' + printf 'created: %s<br>\n' "$post_created" + printf 'modified: %s<br>\n' "$post_modified" + printf '</p>\n' +} + +gen_post() { + post_file="$1" + + post_title_md="$(head -n 1 < "$post_file")" + post_title_html="$(run_lowdown "$post_title_md")" + post_content="$(run_lowdown "$(cat "$post_file" | remove_title)")" + + post_date_parsed="$(parse_fname "${post_file##*/}")" + + printf '<!DOCTYPE html>\n' + printf '<html lang="en">\n' + gen_head "$post_file" "$(echo "$post_title_md" | sed -E -e 's/^#\ (.*)/\1/g')" + printf '<body>\n' + gen_nav + printf '<main>\n' + printf '%s\n' "$post_title_html" + printf '%s\n\n' "$(gen_info "${post_date_parsed%%:*}" "$(date '+%F')")" + printf '%s\n' "$post_content" + gen_footer + printf '</main>\n' + printf '</body>\n' + printf '</html>\n' +} + +for post in ${posts_dir}/*.md; do + post_html="${post%%.*}.html" + + # if the html for this .md exists; do not include in list + if [ -f "$post_html" ]; then + continue + fi + + if is_excluded "$(basename "$post")"; then + continue + fi + + log "adding "$post"..." + gen_post "$post" > "$post_html" +done