}
# display the page
-display() { # display MIMETYPE < DOCUMENT
- mimetype="$1"
- case "$mimetype" in
+display() { # display META < DOCUMENT
+ case "$1" in
+ *\;*)
+ mime="$(cut -d\; -f1 <<<"$1" | trim)"
+ charset="$(cut -d\; -f2 <<<"$1" | trim)"
+ ;;
+ *) mime="$(trim <<<"$1")" ;;
+ esac
+ [ -z "$mime" ] && mime="text/gemini"
+ if [ -z "$charset" ]; then
+ charset="utf-8"
+ else
+ charset="${charset#*=}"
+ fi
+
+ log 5 "mime=$mime; charset=$charset"
+
+ case "$mime" in
+ text/gemini)
+ lfn | typeset_gemini | less -R
+ ;;
text/*)
- # normalize line endings to "\n"
- # gawk 'BEGIN{RS=""}{gsub(/\r\n?/,"\n");print}'
- cat
+ lfn
# TODO: use less with linking and stuff
# less -R -p'^=>' +g
# lesskey:
# also look into the prompt, the filename, and input preprocessor
# ($LESSOPEN, $LESSCLOSE)
;;
- *)
- download "$URL"
- ;;
+ *) download "$URL" ;;
esac
}
+# normalize line endings to \n (LF)
+lfn() {
+ gawk 'BEGIN{RS="\n\n"}{gsub(/\r\n?/,"\n");print;print ""}'
+}
+
+# typeset text
+typeset_gemini() { # typeset_gemini < INPUT
+ gawk '
+ BEGIN { pre = 0 }
+ /^###/ { sub(/^#+[[:space:]]*/, "");
+ printf " \033[3m%s\033[0m\n", $0
+ next }
+ /^##/ { sub(/^#+[[:space:]]*/, "");
+ printf " \033[1m%s\033[0m\n", $0
+ next }
+ /^#/ { sub(/^#+[[:space:]]*/, "");
+ printf " \033[1;4m%s\033[0m\n", $0
+ next }
+ /^=>/ {
+ sub(/=>[[:space:]]*/, "")
+ url = $1; desc = ""
+ for (w=2;w<=NF;w++)
+ desc = desc (desc?" ":"") $w
+ printf " \033[1m[%s]\033[0m \033[4m%s\033[0m \033[36m%s\033[0m\n",
+ (++ln), desc, "(" url ")"
+ next }
+ # /^\*/ { sub(/\*[[:space:]]*/, ""); }
+ /```/ { pre = !pre; next }
+ { sub(/^/, " "); print }
+ '
+}
+
download() { # download URL < FILE
tn="$(mktemp)"
dd status=progress >"$tn"
}
bollux_cleanup() {
- echo
+ exit $?
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then