site

My personal website.

Date: Sun, 11 Jul 2021 23:05:36 +0200

Add script to translate gemtext to html or a gopher menu

The idea is to support the web, gopher and gemini by writing in gemtext
and convertiting it to html and gopher menus.

Diffstat:

A generate                            |     142 +++++++++++++++++++++++++++++++
A index.gmi                           |      30 ++++++++++++++++++++++++++++++

2 files changed, 172 insertions(+), 0 deletions(-)

@@ -0,0 +1,142 @@
+#!/bin/sh
+
+end=""
+preformatted=""
+output="$1"
+dir="$2"
+
+[ $output = "html" ] && extension="html"
+[ $output = "gopher" ] && extension="gph"
+
+rm -rf "$dir"
+cp -rf . "$dir"
+files="$(find "$dir" -name '*.gmi')"
+
+while read -r file <&4; do
+    exec > "$(printf '%s/%s.%s' "$(dirname "$file")" "$(basename -s '.gmi' "$file")" "$extension")"
+    [ $output = "html" ] && printf '<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="utf-8" />\n<link rel="icon" href="favicon.ico" type="image/x-icon" />\n<link rel="stylesheet" href="style.css" type="text/css" />\n</head>\n<body>\n'
+
+    while read -r line <&3; do
+        if [ -n "$preformatted" ]; then
+            case "$line" in
+                '```'*)
+                    preformatted=""
+                    [ $output = "html" ] && echo "</pre></code>"
+                    ;;
+                *)
+                    if [ "$preformatted" = "first" ]; then
+                        printf "%s" "$line"
+                        preformatted="yes"
+                    else
+                        printf "\n%s" "$line"
+                    fi
+                    ;;
+            esac
+            continue
+        fi
+        case "$line" in
+            '```'*)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                preformatted="first"
+                [ $output = "html" ] && printf "<pre><code>"
+                ;;
+            "### "*)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                line="$(printf "$line" | cut -c 5- | tr -d '\n')"
+                [ $output = "html" ] && printf "<h3>%s</h3>\n" "$line"
+                [ $output = "gopher" ] && printf "i### %s\tfake\t(NULL)\t0\r\n" "$line"
+                ;;
+            "## "*)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                line="$(printf "$line" | cut -c 4- | tr -d '\n')"
+                [ $output = "html" ] && printf "<h2>%s</h2>\n" "$line"
+                [ $output = "gopher" ] && printf "i## %s\tfake\t(NULL)\t0\r\n" "$line"
+                ;;
+            "# "*)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                line="$(printf "$line" | cut -c 3- | tr -d '\n')"
+                [ $output = "html" ] && printf "<h1>%s</h1>\n" "$line"
+                [ $output = "gopher" ] && printf "i# %s\tfake\t(NULL)\t0\r\n" "$line"
+                ;;
+            "=>"*)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                line="$(printf "%s" "$line" | cut -c 3- | tr -d '\n')"
+                url="$(printf "%s" "$line" | awk '{print $1}')"
+                text="$(printf "%s" "$line" | awk '{$1="";print}' | awk '{$1=$1;print}')"
+                [ -z "$text" ] && text="$url"
+                if [ $output = "html" ]; then
+                    printf "%s\n" "<p><a href=\"$url\">$text</a></p>"
+                else
+                    host="$(printf "%s" "$url" | cut -d / -f 3)"
+                    path="$(printf "%s" "$url" | cut -d / -f 4- | xargs printf '/%s')"
+                    case "$url" in
+                        gopher://*)
+                            itemtype="1"
+                            port="70"
+                            ;;
+                        http://*)
+                            itemtype="h"
+                            port="80"
+                            ;;
+                        https://*)
+                            itemtype="h"
+                            port="443"
+                            ;;
+                        *.gmi)
+                            host="milotier.net"
+                            path="$url"
+                            itemtype="1"
+                            port="70"
+                            ;;
+                        *.jpg|*.jpeg|*.png)
+                            host="milotier.net"
+                            path="$url"
+                            itemtype="I"
+                            port="70"
+                            ;;
+                        *)
+                            host="milotier.net"
+                            path="$url"
+                            itemtype="0"
+                            port="70"
+                            ;;
+                    esac
+                    printf "%s%s\t%s\t%s\t%d\r\n" "$itemtype" "$text" "$url" "$host" "$port"
+                fi
+                ;;
+            "> "*)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                line="$(printf "$line" | cut -c 3- | tr -d '\n')"
+                [ $output = "html" ] && printf "<blockquote>%s</blockquote>\n" "$line"
+                [ $output = "gopher" ] && printf "i    %s\tfake\t(NULL)\t0\r\n" "$line"
+                ;;
+            "* "*)
+                if [ -z "$end" -a $output = "html" ]; then
+                    echo "<ul>"
+                    end="</ul>"
+                fi
+                line="$(printf "$line" | cut -c 3- | tr -d '\n')"
+                [ $output = "html" ] && printf "<li>%s</li>\n" "$line"
+                [ $output = "gopher" ] && printf "i* %s\tfake\t(NULL)\t0\r\n" "$line"
+                ;;
+            *)
+                [ -n "$end" ] && echo "$end"
+                end=""
+                [ $output = "html" ] && printf "<p>%s</p>\n" "$line"
+                [ $output = "gopher" ] && printf "i%s\tfake\t(NULL)\t0\r\n" "$line"
+                ;;
+        esac
+    done 3< "$file"
+    [ -n "$end" ] && echo "$end"
+    [ $output = "html" ] && printf '</body>\n</html>\n'
+done 4<< EOF
+$files
+EOF
+
+rm -f $files
@@ -0,0 +1,30 @@
+# Milo van der Tier's site
+
+## Introduction
+I'm interested in computers, computer science and recently Linux. On this website I post anything related to this.
+
+## Git repositories
+I host my own git server, which you can access by running
+```
+git clone https://git.milotier.net/<repository name>
+```
+My repositories:
+=> https://git.milotier.net/website website
+The full source of this website, licensed under the Creative Commons Attribution Share-Alike 4.0 license.
+=> https://git.milotier.net/dotfiles dotfiles
+The dotfiles I use in my installation of Artix Linux. This includes both configuration files and custom shell scripts.
+=> https://git.milotier.net/dwm dwm
+My custom build of dwm. All of the changes are in the branch "custom", and master is the same as the upstream.
+=> https://git.milotier.net/dmenu dmenu
+My custom build of dmenu. The branches are the same as in my other suckless builds.
+=> https://git.milotier.net/st st
+My custom build of st. The branches are the same as in my other suckless builds.
+=> https://git.milotier.net/slock slock
+My custom build of slock. The branches are the same as in my other suckless builds.
+=> https://git.milotier.net/sxiv sxiv
+My custom build of sxiv. The branches are the same as in my suckless builds.
+
+## Contact
+Email: milo at vdtier dot nl
+=> /milotier.gpg gpg key
+You can encrypt email with this gpg key.