shite

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

commit bca1eec1326d6745297122c41645e49040bd65e8
parent f176bb76d45c362620c524bd4fc2df274e0f90ed
Author: zakaria <e-zk@users.noreply.github.com>
Date:   Sun,  8 Aug 2021 19:52:58 +1000

Update shite, index.sh

Diffstat:
MREADME | 25+++++--------------------
Mindex.sh | 149++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mshite | 178++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
3 files changed, 257 insertions(+), 95 deletions(-)

diff --git a/README b/README @@ -1,6 +1,5 @@ shite ======= - /bin/sh site generator tools About @@ -8,33 +7,19 @@ This is the successor to the shlog[^1] project, separating each of its main functions into serveral scripts: - * common.rc - houses common script functions and variables * rss.sh - rss feed generation - * update.sh - convert new posts from markdown -> html * index.sh - generate post index (list of posts in chronological order) * shite - simple script that wraps everything into one -The main difference between these scripts and shlog is that these scripts -are easier for _me_ to maintain without worrying about Linux compat, -OS-specific edge cases, etc. +All scripts source a .env file in the current directory. It is assumed all +scripts are run at the root of the static site heirachy. -These scripts have been tested on the latest OpenBSD-current and nothing else. -At this stage, don't expect it to work anywhere else. Just know that it is -what it claims to be; shite. +These scripts have been tested on the latest OpenBSD-current and the latest +Ubuntu on WSL2. Usage ------- - -Static HTML is stored in files located under the $html_dir: - - * $html_dir/head.html - the <head> - * $html_dir/nav.html - additional site header content/navigation - * $html_dir/footer.html - additional page footer content - * $html_dir/pasts.html - added to the beginning of the post list (this is - only used on the $post_index page) - -With the exception of posts.html the content of these files are added to every -generated page in their relevant places. +Soon™ ---- diff --git a/index.sh b/index.sh @@ -1,61 +1,116 @@ #!/bin/sh +set -e -. ${XDG_CONFIG_HOME:-${HOME}/.config}/shite/common.rc +env="${1:-./.env}" +# shellcheck source=./.env.template +. "$env" +exclude_files="${exclude_files}" +meta_ext="${meta_ext:-.meta}" +site_root="${site_root}" +html_dir="${html_dir:-html}" +post_dir="${post_dir:-posts}" -# retreive list of posts in reverse chronological order -# TODO: this might out output stuff in the proper order every time -get_posts() { - for post in ${site_root}/${posts_dir}/*.html; do - if is_excluded "$(basename "$post")"; then - continue - fi - echo "$post" - done | tail -r +log() { + printf 'shite: [%s] %s\n' "$1" "$2" >&2 +} +info() { + log "$(printf '\033[32minfo\033[0m')" "$1" +} +warn() { + log "$(printf '\033[33mwarn\033[0m')" "$1" +} +error() { + log "$(printf '\033[31merror\033[0m')" "$1" +} +die() { + error "$1" + printf 'exiting...\n' >&2 + exit 1 } -gen_index() { - # START html - printf '<!DOCTYPE html>\n' - printf '<html lang="en">\n' +gen_header() { + printf '<header>\n' + printf '<nav>\n' + if [ -f "${site_root}/${html_dir}/header.html" ]; then + cat "${site_root}/${html_dir}/header.html" + else + warn "header_content file 'header.html' does not exist." + fi + printf '</nav>\n' + printf '</header>\n' +} +gen_index_head() { + cat "${html_dir}/index_head.html" +} +gen_index_tail() { + cat "${html_dir}/index_tail.html" +} - # generate the head - gen_head "$post_file" "${site_name}" +gen_index() { + find "${site_root}/${post_dir}/" -name '*.md' | while read -r post; do + post_html="${post%.*}.html" + post_meta="${post%.*}${meta_ext}" + post_url="/posts/$(basename "$post_html")" - # START body + excluded=0 + for ex in $exclude_files; do + if [ "$(basename "$post")" = "$ex" ]; then + excluded=1 + fi + done + + if [ "$excluded" -eq 1 ]; then + warn "${post} excluded" + continue + fi + + # parse metadata if .meta file exists + if [ -f "$post_meta" ]; then + # read the 'key: value' .meta file + while IFS=': ' read -r key val; do + [ "${key##\#*}" ] || continue + # export each key as a variable; '$post_<key>' + export "post_${key}=${val}" 2>/dev/null || \ + warn "'${key}' is not a valid meta tag name" + done < "$post_meta" + else + warn "no ${meta_ext} - skipping metadata parsing" + fi + + echo "${post_date}|${post_title}|${post_url}" >> index.meta + + unset post_date + unset post_title + unset post_image + unset post_description + unset post_url + done + + printf '<!DOCTYPE html>\n' + printf '<html>\n' + printf '<head>\n' + printf "<title>zakaria's web log</title>\n" + cat "${html_dir}/head.html" + printf "<meta property=\"og:title\" content=\"zakaria's web log\">\n" + printf '</head>\n' printf '<body>\n' - - # generate navigation - gen_nav - - printf '<main>\n' - - # print stuff - cat "${html_dir}/posts.html" - - # list posts - printf '<ul class="postslist">\n' - for post in $(get_posts); do - log "adding "$post" to index..." - - post_md="${post%%.*}.md" - post_url="$(basename "$post")" - date_parsed="$(parse_fname "$post_url")" - post_title="$(md_title "$post_md" | md_to_txt)" - post_date="${date_parsed%%:*}" - - printf '<li>\n' - printf '<a href="%s"><span class="right postdate">%s</span>%s</a>\n' "$post_url" "$post_date" "$post_title" - printf '</li>\n' + gen_header + gen_index_head + sort -r index.meta | while IFS='|' read -r post_date post_title post_url; do + printf '<p class="postitem"><a href="%s"><span class="postdate">%s</span>%s</a></p>\n' "$post_url" "$post_date" "$post_title" done - printf '</ul>\n' - printf '</main>\n' - - # END body + gen_index_tail printf '</body>\n' - - # END html printf '</html>\n' } -gen_index > "$posts_index" +if [ -f "./index.meta" ]; then + log 'old index.meta file found. remove?' + rm -i "./index.meta" +fi + +gen_index > "${site_root}/${post_dir}/index.html" + +# remove index tempfile +rm -i index.meta diff --git a/shite b/shite @@ -1,30 +1,151 @@ #!/bin/sh +set -e -# dir where shite is installed -SHITEPATH="${SHITEPATH:-${HOME}/bin/shite/}" - -usage() { - cat <<EOF -usage: $(basename $0) [update|index|page] [args ...] -EOF -} - -case "$1" in - "update") - shift - ${SHITEPATH}/update.sh "$@" - ;; - "index") - shift - ${SHITEPATH}/index.sh "$@" - ;; - "page") - shift - ${SHITEPATH}/page.sh "$@" - ;; - *) - echo "$(basename $0): `${1}' is a shite command, but not a shite command" - usage - exit 1 - ;; -esac +env="${1:-./.env}" +# shellcheck source=./.env.template +. "$env" + +onion="${onion}" + +exclude_files="${exclude_files}" +meta_ext="${meta_ext:-.meta}" +site_root="${site_root}" +html_dir="${html_dir:-html}" +post_dir="${post_dir:-posts}" + +log() { + printf 'shite: [%s] %s\n' "$1" "$2" >&2 +} +info() { + log "$(printf '\033[32minfo\033[0m')" "$1" +} +warn() { + log "$(printf '\033[33mwarn\033[0m')" "$1" +} +error() { + log "$(printf '\033[31merror\033[0m')" "$1" +} +die() { + error "$1" + printf 'exiting...\n' >&2 + exit 1 +} + +gen_header() { + printf '<header>\n' + printf '<nav>\n' + if [ -f "${site_root}/${html_dir}/header.html" ]; then + cat "${site_root}/${html_dir}/header.html" + else + warn "header_content file 'header.html' does not exist." + fi + printf '</nav>\n' + printf '</header>\n' +} + +gen_foot() { + post="$1" + post_html="${post%.*}.html" + post_bname="${post#*${site_root}}" + post_url="${post_html#*${site_root}}" + + printf '<footer>\n' + printf '<a href="%s">plaintext</a>&nbsp;&nbsp;' ".${post_bname#*/${post_dir}}" + [ -n "$onion" ] \ + && printf '<a href="%s%s">onion</a>\n' "${onion}" "${post_url#*${site_root}}" + cat "${site_root}/${html_dir}/footer.html" + printf '</footer>\n' +} + +gen_post() { + post="$1" + post_html="${post%.*}.html" + post_meta="${post%.*}${meta_ext}" + post_url="${post_html#*${site_root}}" + + printf '<!DOCTYPE html>\n' + printf '<html lang="en">\n' + printf '<head>\n' + + # cat static head tags + cat "${site_root}/${html_dir}/head.html" + + # if an onion was specified add the onion tag + [ -n "$onion" ] \ + && printf '<meta http-equiv="onion-location" content="%s%s">\n' "$onion" "$post_url" + + # parse metadata if .meta file exists + if [ -f "$post_meta" ]; then + + # read the 'key: value' .meta file + while IFS=': ' read -r key val; do + [ "${key##\#*}" ] || continue + # export each key as a variable; '$post_<key>' + export "post_${key}=${val}" 2>/dev/null || \ + warn "'${key}' is not a valid meta tag name" + done < "$post_meta" + + # if exist, print opengraph tags + if [ -n "$post_title" ]; then + printf '<title>%s</title>\n' "$post_title" + printf '<meta property="og:title" content="%s">\n' "$post_title" + fi + [ -n "$post_description" ] \ + && printf '<meta property="og:description" content="%s">\n' "$post_description" + if [ -n "$post_image" ]; then + printf '<meta property="og:image" content="%s">\n' "$post_image" + printf '<meta property="twitter:image:src" content="%s">\n' "$post_image" + fi + else + info "no ${meta_ext} - skipping metadata parsing" + fi + + printf '</head>\n' + printf '<body>\n' + gen_header + printf '<main>\n' + + # hero image + [ -n "$post_image" ] && printf '<img class="post-banner" src="%s"/>\n' "$post_image" + + lowdown -Thtml < "$post" + + # if a post date is given, add it + printf '<p class="info">' + [ -n "$post_title" ] && printf '%s' "$post_title" + [ -n "$post_date" ] && printf ' &bullet; %s' "$post_date" + # [ -n "$post_description" ] && printf ' &bullet; %s' "$post_description" + printf '</p>\n' + + printf '</main>\n' + + gen_foot "$post" + + printf '</body>\n' + + printf '</html>\n' + + unset post_date + unset post_title + unset post_image + unset post_description +} + +find "${site_root}" -name '*.md' | while read -r post; do + post_html="${post%.*}.html" + + excluded=0 + for ex in $exclude_files; do + if [ "$(basename "$post")" = "$ex" ]; then + excluded=1 + fi + done + + if [ "$excluded" -eq 1 ]; then + warn "${post} excluded" + continue + fi + + info "compiling ${post}..." + gen_post "$post" > "$post_html" +done +\ No newline at end of file