-- Leo's gemini proxy

-- Connecting to ew.srht.site:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

------------------------------------------------------------
#!/bin/bash
# 2020-11-21
# gem2html.sh file.gmi > file.html

filename="${1}"

title="${filename##*/}"

addr_pub="your-onion-site-hash-here.onion"

cat <<EOF
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>${title}</title>
  </head>
  <body>
EOF

OLDIFS=$IFS
IFS='' # preserve (leading!) white space, e.g. in <pre>...</pre>
in_list=0
in_pre=0
( cat ${filename} && echo "") |
    while read line
    do
        if [ $in_list -eq 1 ]
        then
            case "${line}" in
                (\**)
                    in_list=1
                    text=$(echo "$line" | sed -e 's/^[*][ ]*//')
                    echo "      <li>${text}</li>"
                    ;;
                (*)
                    in_list=0 # fall through into next case!
                    echo "    </ul>"
            esac
        elif [ $in_pre -eq 1 ]
        then
            case "${line}" in
                (\`\`\`*)
                    echo "    </pre>"
                    in_pre=0
                    continue
                    ;;
                (*)
                    # emacs/eww chokes on '<>' in pre section?
                    text=$(echo "$line" | sed -e 's/</\&lt;/g' -e 's/>/\&gt;/g')
                    echo "$text"
                    ;;
            esac
        fi

        if [[ $in_list -eq 0 && $in_pre -eq 0 ]]
        then
            case "${line}" in
                (\#[^#]*)
                    text=$(echo "$line" | sed -e 's/^#[ ]*//')
                    echo "    <h1>${text}</h1>"
                    ;;
                (\#\#[^#]*)
                    text=$(echo "$line" | sed -e 's/^##[ ]*//')
                    echo "    <h2>${text}</h2>"
                    ;;
                (\#\#\#[^#]*)
                    text=$(echo "$line" | sed -e 's/^###[ ]*//')
                    echo "    <h3>${text}</h3>"
                    ;;
                (\=\>*)
                    text=$(echo "$line" | sed -e 's/^=>[ ]*//')
                    link=$(echo "$text" | cut -d' ' -f1)
                    desc=$(echo "$text" | cut -d' ' -f2- | sed -e 's|^[ ]*||')
                    # rewrite site internal links
                    case "${link}" in
                        (/*\.gmi)
                            link=$( echo "${link}" | sed -e 's|\.gmi$|.html|' )
                            ;;
                        (*) :
                            ;;
                    esac
                    echo "    <p><a href=\"${link}\">${desc}</a></p>"

                    ;;
                (\>*)
                    text=$(echo "$line" | sed -e 's/^>[ ]*//')
                    echo "    <blockquote><p>${text}</p></blockquote>"
                    ;;
                (\**)
                    in_list=1
                    text=$(echo "$line" | sed -e 's/^[*][ ]*//')
                    echo "    <ul>"
                    echo "      <li>${text}</li>"
                    ;;
                (\`\`\`*)
                    in_pre=1
                    echo "    <pre>"
                    ;;
                (*) :
                    echo "    <p>${line}</p>"
                    ;;
            esac
        fi
    done

cat <<EOF
  </body>
</html>
EOF
------------------------------------------------------------

-- Response ended

-- Page fetched on Fri Mar 29 13:42:05 2024