index.sh (2615B)
1 #!/bin/sh 2 set -e 3 4 env="${1:-./.env}" 5 # shellcheck source=./.env.template 6 . "$env" 7 8 exclude_files="${exclude_files}" 9 meta_ext="${meta_ext:-.meta}" 10 site_root="${site_root}" 11 html_dir="${html_dir:-html}" 12 post_dir="${post_dir:-posts}" 13 14 log() { 15 printf 'shite: [%s] %s\n' "$1" "$2" >&2 16 } 17 info() { 18 log "$(printf '\033[32minfo\033[0m')" "$1" 19 } 20 warn() { 21 log "$(printf '\033[33mwarn\033[0m')" "$1" 22 } 23 error() { 24 log "$(printf '\033[31merror\033[0m')" "$1" 25 } 26 die() { 27 error "$1" 28 printf 'exiting...\n' >&2 29 exit 1 30 } 31 32 gen_header() { 33 printf '<header>\n' 34 printf '<nav>\n' 35 if [ -f "${site_root}/${html_dir}/header.html" ]; then 36 cat "${site_root}/${html_dir}/header.html" 37 else 38 warn "header_content file 'header.html' does not exist." 39 fi 40 printf '</nav>\n' 41 printf '</header>\n' 42 } 43 gen_index_head() { 44 cat "${html_dir}/index_head.html" 45 } 46 gen_index_tail() { 47 cat "${html_dir}/index_tail.html" 48 } 49 50 gen_index() { 51 find "${site_root}/${post_dir}/" -name '*.md' | while read -r post; do 52 post_html="${post%.*}.html" 53 post_meta="${post%.*}${meta_ext}" 54 post_url="/posts/$(basename "$post_html")" 55 56 excluded=0 57 for ex in $exclude_files; do 58 if [ "$(basename "$post")" = "$ex" ]; then 59 excluded=1 60 fi 61 done 62 63 if [ "$excluded" -eq 1 ]; then 64 warn "${post} excluded" 65 continue 66 fi 67 68 # parse metadata if .meta file exists 69 if [ -f "$post_meta" ]; then 70 # read the 'key: value' .meta file 71 while IFS=': ' read -r key val; do 72 [ "${key##\#*}" ] || continue 73 # export each key as a variable; '$post_<key>' 74 export "post_${key}=${val}" 2>/dev/null || \ 75 warn "'${key}' is not a valid meta tag name" 76 done < "$post_meta" 77 else 78 warn "no ${meta_ext} - skipping metadata parsing" 79 fi 80 81 echo "${post_date}|${post_title}|${post_url}" >> index.meta 82 83 unset post_date 84 unset post_title 85 unset post_image 86 unset post_description 87 unset post_url 88 done 89 90 printf '<!DOCTYPE html>\n' 91 printf '<html>\n' 92 printf '<head>\n' 93 printf "<title>zakaria's web log</title>\n" 94 cat "${html_dir}/head.html" 95 printf "<meta property=\"og:title\" content=\"zakaria's web log\">\n" 96 printf '</head>\n' 97 printf '<body>\n' 98 gen_header 99 gen_index_head 100 sort -r index.meta | while IFS='|' read -r post_date post_title post_url; do 101 printf '<p class="postitem"><a href="%s"><span class="postdate">%s</span>%s</a></p>\n' "$post_url" "$post_date" "$post_title" 102 done 103 gen_index_tail 104 printf '</body>\n' 105 printf '</html>\n' 106 } 107 108 if [ -f "./index.meta" ]; then 109 log 'old index.meta file found. remove?' 110 rm -i "./index.meta" 111 fi 112 113 gen_index > "${site_root}/${post_dir}/index.html" 114 115 # remove index tempfile 116 rm -i index.meta