shite

Static site generator script
git clone _git@git.zakaria.org/shite.git
Log | Files | Refs | README

shite (3622B)


      1 #!/bin/sh
      2 set -e
      3 
      4 env="${1:-./.env}"
      5 # shellcheck source=./.env.template
      6 . "$env"
      7 
      8 onion="${onion}"
      9 
     10 exclude_files="${exclude_files}"
     11 meta_ext="${meta_ext:-.meta}"
     12 site_root="${site_root}"
     13 html_dir="${html_dir:-html}"
     14 post_dir="${post_dir:-posts}"
     15 
     16 log() {
     17 	printf 'shite: [%s] %s\n' "$1" "$2" >&2
     18 }
     19 info() {
     20 	log "$(printf '\033[32minfo\033[0m')" "$1"
     21 }
     22 warn() {
     23 	log "$(printf '\033[33mwarn\033[0m')" "$1"
     24 }
     25 error() {
     26 	log "$(printf '\033[31merror\033[0m')" "$1"
     27 }
     28 die() {
     29 	error "$1"
     30 	printf 'exiting...\n' >&2
     31 	exit 1
     32 }
     33 
     34 gen_header() {
     35 	printf '<header>\n'
     36 	printf '<nav>\n'
     37 	if [ -f "${site_root}/${html_dir}/header.html" ]; then
     38 		cat "${site_root}/${html_dir}/header.html"
     39 	else
     40 		warn "header_content file 'header.html' does not exist."
     41 	fi
     42 	printf '</nav>\n'
     43 	printf '</header>\n'
     44 }
     45 
     46 gen_foot() {
     47 	post="$1"
     48 	post_title="$2"
     49 	post_date="$3"
     50 	post_html="${post%.*}.html"
     51 	post_bname="${post#*${site_root}}"
     52 	post_url="${post_html#*${site_root}}"
     53 
     54 	printf '<footer>\n<br><hr>\n'
     55 
     56 	#if [ -n "$post_title" ]; then
     57 	#	printf '%s&nbsp;&sdot;&nbsp;' "$post_title"
     58 	#fi
     59 	#
     60 	if [ -n "$post_date" ]; then
     61 		printf '<p>%s</p>\n' "$post_date"
     62 	fi
     63 
     64 	printf '<p>[&nbsp;<a href="%s">view plaintext</a>' "${post_bname#*/${site_root}}"
     65 	if [ -n "$onion" ]; then
     66 		printf '&nbsp;|&nbsp;'
     67 		printf '<a href="%s%s">view on hidden service</a>' "${onion}" "${post_url#*${site_root}}"
     68 	fi
     69 	printf '&nbsp;]</p>\n'
     70 
     71 	cat "${site_root}/${html_dir}/footer.html"
     72 	
     73 	printf '</footer>\n'
     74 }
     75 
     76 gen_post() {
     77 	post="$1"
     78 	post_html="${post%.*}.html"
     79 	post_meta="${post%.*}${meta_ext}"
     80 	post_url="${post_html#*${site_root}}"
     81 	
     82 	printf '<!DOCTYPE html>\n'
     83 	printf '<html lang="en">\n'
     84 	printf '<head>\n'
     85 
     86 	# cat static head tags
     87 	cat "${site_root}/${html_dir}/head.html"
     88 
     89 	# if an onion was specified add the onion tag
     90 	[ -n "$onion" ] \
     91 		&& printf '<meta http-equiv="onion-location" content="%s%s">\n' "$onion" "$post_url"
     92 
     93 	# parse metadata if .meta file exists
     94 	if [ -f "$post_meta" ]; then
     95 
     96 		# read the 'key: value' .meta file
     97 		while IFS=': ' read -r key val; do
     98 			[ "${key##\#*}" ] || continue
     99 			# export each key as a variable; '$post_<key>'
    100 			export "post_${key}=${val}" 2>/dev/null || \
    101 				warn "'${key}' is not a valid meta tag name"
    102 		done < "$post_meta"
    103 
    104 		# if exist, print opengraph tags
    105 		# shellcheck disable=SC2154
    106 		if [ -n "$post_title" ]; then
    107 			printf '<title>%s</title>\n' "$post_title"
    108 			printf '<meta property="og:title" content="%s">\n' "$post_title"
    109 		fi
    110 
    111 		# shellcheck disable=SC2154
    112 		[ -n "$post_description" ] \
    113 			&& printf '<meta property="og:description" content="%s">\n' "$post_description"
    114 
    115 		# shellcheck disable=SC2154
    116 		if [ -n "$post_image" ]; then
    117 			printf '<meta property="og:image" content="%s">\n' "$post_image"
    118 			printf '<meta property="twitter:image:src" content="%s">\n' "$post_image"
    119 		fi
    120 	else
    121 		info "no ${meta_ext} - skipping metadata parsing"
    122 	fi
    123 
    124 	printf '</head>\n'
    125 	printf '<body>\n'
    126 	gen_header
    127 	printf '<main>\n'
    128 
    129 	# hero image
    130 	[ -n "$post_image" ] && printf '<img class="post-banner" src="%s"/>\n' "$post_image"
    131 
    132 	# md -> html
    133 	lowdown -Thtml < "$post"
    134 
    135 	printf '</main>\n'
    136 	gen_foot "$post" "$post_title" "$post_date"
    137 	printf '</body>\n'
    138 	printf '</html>\n'
    139 
    140 	unset post_date
    141 	unset post_title
    142 	unset post_image
    143 	unset post_description
    144 }
    145 
    146 find "${site_root}" -name '*.md' | while read -r post; do
    147 	post_html="${post%.*}.html"
    148 
    149 	excluded=0
    150 	for ex in $exclude_files; do
    151 		if [ "$(basename "$post")" = "$ex" ]; then
    152 			excluded=1
    153 		fi
    154 	done
    155 
    156 	if [ "$excluded" -eq 1 ]; then
    157 		warn "${post} excluded"
    158 		continue
    159 	fi
    160 
    161 	info "compiling ${post}..."
    162 	gen_post "$post" > "$post_html"
    163 done