#!/usr/bin/env bash declare -r ARG=$1; shift source buetow.org.conf ## Test module assert::equals () { local -r result="$1"; shift local -r expected="$1"; shift if [ "$result" != "$expected" ]; then cat < "$gemfeed_dir/index.gmi.tmp" # $DOMAIN's Gemfeed ## $SUBTITLE GEMFEED ls "$gemfeed_dir" | grep '\.gmi$' | grep -v '^index.gmi$' | sort -r | while read gmi_file; do # Extract first heading as post title. local title=$(sed -n '/^# / { s/# //; p; q; }' "$gemfeed_dir/$gmi_file" | tr '"' "'") # Extract the date from the file name. local filename_date=$(basename "$gemfeed_dir/$gmi_file" | cut -d- -f1,2,3) echo "=> ./$gmi_file $filename_date $title" >> "$gemfeed_dir/index.gmi.tmp" done mv "$gemfeed_dir/index.gmi.tmp" "$gemfeed_dir/index.gmi" git add "$gemfeed_dir/index.gmi" } ## Atom module atom::meta () { local -r gmi_file_path="$1"; shift local -r meta_file=$(sed 's|gemtext|meta|; s|.gmi$|.meta|;' <<< "$gmi_file_path") local -r meta_dir=$(dirname "$meta_file") test ! -d "$meta_dir" && mkdir -p "$meta_dir" if [ ! -f "$meta_file" ]; then # Extract first heading as post title. local title=$(sed -n '/^# / { s/# //; p; q; }' "$gmi_file_path" | tr '"' "'") # Extract first paragraph from Gemtext local summary=$(sed -n '/^[A-Z]/ { p; q; }' "$gmi_file_path" | tr '"' "'") # Extract the date from the file name. local filename_date=$(basename $gmi_file_path | cut -d- -f1,2,3) local date=$(date --iso-8601=seconds --date "$filename_date $(date +%H:%M:%S)") cat < "$atom_file.tmp" $now $DOMAIN feed $SUBTITLE gemini://$DOMAIN/ ATOMHEADER while read -r gmi_file; do # Load cached meta information about the post. source <(atom::meta "$gemfeed_dir/$gmi_file") cat <> "$atom_file.tmp" $meta_title gemini://$DOMAIN/gemfeed/$gmi_file $meta_date $meta_summary $meta_author $meta_email ATOMENTRY done < <(ls "$gemfeed_dir" | sort -r | grep '.gmi$' | grep -v '^index.gmi$' | head -n $ATOM_MAX_ENTRIES) cat <> "$atom_file.tmp" ATOMFOOTER # Delete the 3rd line of the atom feeds (global feed update timestamp) if ! diff -u <(sed 3d "$atom_file") <(sed 3d "$atom_file.tmp"); then echo "Feed got something new!" mv "$atom_file.tmp" "$atom_file" git add "$atom_file" else echo "Nothing really new in the feed" rm "$atom_file.tmp" fi } ## HTML module html::paragraph () { local -r text="$1" test -n "$text" && echo "

$text

" } html::heading () { local -r text=$(sed -E 's/^#+ //' <<< "$1") local -r level="$2" echo "$text" } html::quote () { local -r quote="${1/> }" echo "
$quote
" } html::img () { local link="$1"; shift local descr="$1"; shift if [ -z "$descr" ]; then echo -n "" else echo -n "$descr:" echo -n "\"$descr\"" fi echo "
" } html::link () { local -r line="${1/=> }" local link local descr while read -r token; do if [ -z "$link" ]; then link="$token" elif [ -z "$descr" ]; then descr="$token" else descr="$descr $token" fi done < <(echo "$line" | tr ' ' '\n') if grep -E -q "$IMAGE_PATTERN" <<< "$link"; then html::img "$link" "$descr" return fi # If relative link convert .gmi to .html grep -F -q '://' <<< "$link" || link=${link/.gmi/.html} # If no description use link itself test -z "$descr" && descr="$link" echo "$descr
" } html::gemini2html () { local -r gmi_file=$1 local -i is_list=0 local -i is_plain=0 while IFS='' read -r line; do if [ $is_list -eq 1 ]; then if [[ "$line" == '* '* ]]; then echo "
  • ${line/\* /}
  • " else is_list=0 echo "" fi continue elif [ $is_plain -eq 1 ]; then if [[ "$line" == '```'* ]]; then echo "" is_plain=0 else echo "$line" | sed 's|<|\<|g; s|>|\>|g' fi continue fi case "$line" in '* '*) is_list=1 echo "