๐Ÿ’พ Archived View for bbs.geminispace.org โ€บ s โ€บ Gemini โ€บ 19985 captured on 2024-12-17 at 15:03:50. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

A gemtext fetcher and renderer script

Not a full-fledged browser, just for occasional aggregator/gemlog checks. Uses OSC8 terminal hyperlinks to tame the links in gemtext.

#!/usr/bin/env zsh
# ยฉ Copyright 2024, sy
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. 
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

# โ€ข Should also work with bash, change the shell on the top if you like.
# โ€ข `ncat` is from nmap. Use `openssl s_client` if you like.
# โ€ข Uses `bat` pager, with a gemini color theme installed. If not installed, use `less` etc.
# โ€ข On macOS or BSD, install `gfmt` from GNU coreutils.
# โ€ข Export cert & key: `openssl pkey -in foo.key > key.pem; openssl x509 -in foo.crt > cert.pem`
#   And add `--ssl-key key.pem --ssl-cert cert.pem` below for `--ssl`
set -euo pipefail
if [[ "$#" -ne 1 || "$1" == "-h" ]]; then >&2 echo "Usage: $(basename "$0") <URI>"; exit 1; fi
if [[ $(uname) == "Linux" ]]; then fmt="fmt -s -w 75"; else fmt="gfmt -s -w 75"; fi

without_scheme=${1#gemini://} ; host_port=${without_scheme%%/*}
if [[ $host_port == *:* ]]; then host=${host_port%%:*}; port=${host_port/*:/};
else host=${host_port}; port=1965; fi
prog='\
NR==1             { if (!/^20/) print; next }           # TODO: MIME and params?
/^```/            { p=!p;if (p) {sub(/^```[[:space:]]*/,"");if (length) print "โ”Œโ”ค"$0"โ”œ"} next}
/^#/ || p         { if (p) $0="โ”‚"$0; print; next }
/^=>[^[:space:]]/ { sub(/^=>/,"=> ") }                  # Fix missing space
/^=>/             { if (NF==2) $3=$2; if ($2 ~ /^\/\//) $2="gemini:"$2; i="";
                    if ($2 ~ /^\//) $2=ROOT $2;         # absolute url (leaves . & .. intact)
                    else if ($2 !~ /^[a-z]+:/) {        # relative url (handle upto 3 consecutive .&..)
                      for (j=3;j;--j) gsub(/\/\.\//,"/", $2);          # a/./b/././c -> a/b/c
                      for (j=3;j;--j) gsub(/[^\/\.]+\/\.\.\//,"",$2);  # a/../b/c/../../d -> d
                      if ($2 ~ /^\.\//) $2=substr($2,3);               # ./a -> a
                      for (j=3;j && $2 ~ /^\.\.\//;--j) { $2=substr($2,4);sub(/[^\/]+\/?$/,"",DIR) }
                      $2=DIR $2; }
                    else if ($2 ~ /^gemini:/) i="๐Ÿ’Ž"; else ;if ($2 ~ /^https?:/) i="๐ŸŒ";
                    else if ($2 ~ /^gopher:/) i="๐Ÿ•ณ"; else if ($2 ~ /^mailto:/) i="๐Ÿ“ง";
                    $2=i OSC8 $2 ESCBEL; print $0 OSC8 ESCBEL; next }
/^\* /            { $0="x"$0; print|LFMT;close(LFMT); next }
/^>/              {           print|QFMT;close(QFMT); next }
                  {           print|PFMT;close(PFMT) }'


echo "gemini://$without_scheme" | ncat -C --ssl --no-shutdown "$host" "$port" \
  | awk -v ROOT="gemini://$host_port" -v DIR="gemini://${without_scheme%/*}/" \
    -v PFMT="$fmt" -v OSC8=


\e]8;;' -v ESCBEL=


\e\\' \
    -v LFMT="$fmt"' -p"x"|sed "s,^x[*] ,โ€ข ,;s,^x,  ,"' \
    -v QFMT="$fmt"' -p">"|sed "s,^>[^[:space:]],> ,"' \
    "$prog" | bat --plain --wrap=never -l Gemini

Posted in: s/Gemini

๐Ÿš€ sy

Sep 26 ยท 3 months ago