#!/bin/bash ## # Generate html page with blog article excerpts from ./posts.txt. Post file names should # be added to ./posts.txt in the exact order that they are supposed to appear on the blog # page. # Check if required executables can be found if ! type readlink dirname html2text mv; then echo 'One or more required executables are not present. Generation cancelled' >&2 exit 1 fi # Determine script directory (requires GNU readlink) here="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" printf 'Changing directory: ' pushd "$here" || exit $? posts_file="$here/posts.txt" if ! [[ -f "$posts_file" ]]; then printf 'Posts file "%s" not found. Generation cancelled.\n' "$posts_file" >&2 exit 1 fi escape-html() { sed 's/&/\&/g; s//\>/g; s/"/\"/g; s/'"'"'/\'/g' } html-to-text() { html2text -nobs -style compact "$@" } blog_html="$here/blog.html" { echo ' Blog Home

Blog

' while read -r post_html; do # Convert the post's html to text to make it easier to use the blog's text text="$(html-to-text "$post_html" | escape-html)" || exit $? # The title should be on the 2nd line of text, right after the link to the # homepage. This is a bit inflexible but it will do for now. title="$(tail -n +2 <<<"$text" | head -n 1 | tr -d '*')" || exit $? # Use the first 5 lines after the title as post excerpt. excerpt="$(tail -n +3 <<<"$text" | head -n 5)" || exit $? # Escape the post html file name to safely use it in the generated html. href="$(escape-html <<<"$post_html")" || exit $? printf '

%s

%s


\n' \ "$href" \ "$title" \ "$excerpt" done < "$posts_file" echo ' ' } > "$blog_html.new" mv -v "$blog_html.new" "$blog_html" || exit $? echo 'SUCCESS!'