site

My personal website.

Date: Tue, 13 Jul 2021 11:12:51 +0200

Convert generate into a mostly awk script

Diffstat:

M generate                            |     310 +++++++++++++++++--------------

1 file changed, 169 insertions(+), 141 deletions(-)

@@ -8,6 +8,174 @@ dir="$2"
 [ $output = "html" ] && extension="html"
 [ $output = "gopher" ] && extension="gph"
 
+program="$(printf "%s" '
+BEGIN {
+    html = --HTML--;
+    if (html)
+        print "<!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>";
+}
+END {
+    if (html)
+        print "</body>\n</html>";
+}
+function wrap(text,   q, y, z) {
+    while (text) {
+        q = match(text, / |$/);
+        y += q
+        if (y > 80) {
+            if (html)
+                z = z "\n";
+            else
+                z = z "\nt";
+            y = q - 1;
+        } else if (z) {
+            z = z FS;
+        }
+        z = z substr(text, 1, q - 1);
+        text = substr(text, q + 1);
+    }
+    return z;
+}
+/^=>/ && !preformatted {
+    $0 = substr($0, 3, length($0));
+    if (html && list) {
+        print "</ul>";
+        list = 0;
+    }
+    url = $1;
+    $1 = "";
+    sub(/^ /, "", $0);
+    if (html) {
+        print "<p><a href=\"" url "\">" $0 "</a></p>";
+    } else {
+        split(url, segments, "/");
+        host = segments[3];
+        if (match(url, /^gopher:\/\//)) {
+            itemtype = "1";
+            path = $0;
+            port = "70";
+        } else if (match(url, /^http:\/\//)) {
+            itemtype = "h";
+            path = "URL:" url;
+            port = "80";
+        } else if (match(url, /^https:\/\//)) {
+            itemtype = "h";
+            path = "URL:" url;
+            port = "443";
+        } else if (match(url, /^gemini:\/\//)) {
+            itemtype = "h";
+            path = "URL:" url;
+            port = "1965";
+        } else if (match(url, /\.gmi$/)) {
+            host = "milotier.net";
+            itemtype = "1";
+            path = url;
+            port = "70";
+        } else if (match(url, /(\.jpg|\.jpeg|\.png)$/)) {
+            host = "milotier.net";
+            itemtype = "I";
+            path = url;
+            port = "70";
+        } else {
+            host = "milotier.net";
+            itemtype = "0";
+            path = url;
+            port = "70";
+        }
+        print "[" itemtype "|" path "|" host "|" port "]";
+    }
+    next;
+}
+html {
+    gsub(/&/, "\\&amp;");
+    gsub(/</, "\\&lt;");
+    gsub(/>/, "\\&gt;");
+}
+/^```/ {
+    preformatted = !preformatted;
+    next;
+}
+preformatted {
+    if (html)
+        print "<pre><code>" $0 "</pre></code>";
+    else
+        print "t  " $0;
+    next;
+}
+/^# / {
+    if (html && list) {
+        print "</ul>";
+        list = 0;
+    }
+    $1 = "";
+    if (html)
+        print "<h1>" $0 "</h1>";
+    else
+        print "t# " $0;
+    next;
+}
+/^## / {
+    if (html && list) {
+        print "</ul>";
+        list = 0;
+    }
+    $1 = "";
+    if (html)
+        print "<h2>" $0 "</h2>";
+    else
+        print "t## " $0;
+    next;
+}
+/^### / {
+    if (html && list) {
+        print "</ul>";
+        list = 0;
+    }
+    $1 = "";
+    if (html)
+        print "<h3>" $0 "</h3>";
+    else
+        print "t### " $0;
+    next;
+}
+/^> / {
+    if (html && list) {
+        print "</ul>";
+        list = 0;
+    }
+    $1 = "";
+    if (html)
+        print "<blockquote>" $0 "</blockquote>";
+    else
+        print "t    " $0;
+    next;
+}
+/^* / {
+    if (html) {
+        list = 1;
+        print "<ul>";
+    }
+    $1 = "";
+    print;
+    next;
+}
+{
+    $0 = wrap($0);
+    if (html && list) {
+        print "</ul>";
+        list = 0;
+    }
+    if (html) {
+        if ($0 == "")
+            print "<br />";
+        else
+            print "<p>" $0 "</p>";
+    } else {
+        print "t" $0;
+    }
+}
+' | sed "s/--HTML--/$([ $output = "html" ] && printf 1 || printf 0)/")"
+
 escape() {
     [ $output = "html" ] && printf "%s" "$1" | sed 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'
     [ $output = "gopher" ] && printf "%s" "$1"
@@ -18,147 +186,7 @@ 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" ] && printf "</pre></code>\n"
-                    [ $output = "gopher" ] && printf "\n"
-                    ;;
-                *)
-                    line="$(escape "$line")"
-                    if [ "$preformatted" = "first" ]; then
-                        [ $output = "html" ] && printf "%s" "$line"
-                        [ $output = "gopher" ] && printf "t%s" "$line"
-                        preformatted="yes"
-                    else
-                        [ $output = "html" ] && printf "\n%s" "$line"
-                        [ $output = "gopher" ] && printf "\nt%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="$(escape "$line" | cut -c 5- | tr -d '\n')"
-                [ $output = "html" ] && printf "<h3>%s</h3>\n" "$line"
-                [ $output = "gopher" ] && printf "t### %s\n" "$line"
-                ;;
-            "## "*)
-                [ -n "$end" ] && echo "$end"
-                end=""
-                line="$(escape "$line" | cut -c 4- | tr -d '\n')"
-                [ $output = "html" ] && printf "<h2>%s</h2>\n" "$line"
-                [ $output = "gopher" ] && printf "t## %s\n" "$line"
-                ;;
-            "# "*)
-                [ -n "$end" ] && echo "$end"
-                end=""
-                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
-                [ $output = "html" ] && printf "<h1>%s</h1>\n" "$line"
-                [ $output = "gopher" ] && printf "t# %s\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="$(escape "$line" | awk '{$1="";print}' | awk '{$1=$1;print}')"
-                line="$(escape "$line")"
-                [ -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"
-                            path="$(printf "URL:%s" "$url")"
-                            port="80"
-                            ;;
-                        https://*)
-                            itemtype="h"
-                            path="$(printf "URL:%s" "$url")"
-                            port="443"
-                            ;;
-                        gemini://*)
-                            itemtype="h"
-                            path="$(printf "URL:%s" "$url")"
-                            port="1965"
-                            ;;
-                        *.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
-                    text="$(printf "%s" "$text" | sed 's/|/\\|/g;s/\[/\\\[/g;s/\]/\\\]/g')"
-                    printf "[%s|%s|%s|%s|%d]\n" "$itemtype" "$text" "$path" "$host" "$port"
-                fi
-                ;;
-            "> "*)
-                [ -n "$end" ] && echo "$end"
-                end=""
-                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
-                [ $output = "html" ] && printf "<blockquote>%s</blockquote>\n" "$line"
-                [ $output = "gopher" ] && printf "%s\n" "$(printf "%s" "$line" | fold -sw 80 | sed 's/^/t    ')"
-                ;;
-            "* "*)
-                if [ -z "$end" -a $output = "html" ]; then
-                    echo "<ul>"
-                    end="</ul>\n"
-                else
-                    end="\n"
-                fi
-                line="$(escape "$line" | cut -c 3- | tr -d '\n')"
-                [ $output = "html" ] && printf "<li>%s</li>\n" "$line"
-                [ $output = "gopher" ] && printf "t* %s\n" "$line"
-                ;;
-            *)
-                [ -n "$end" ] && printf "$end"
-                end=""
-                line="$(escape "$line")"
-                if [ $output = "html" ]; then
-                    [ -n "$line" ] && printf "<p>%s</p>\n" "$line"
-                    [ -z "$line" ] && printf "<br />\n" "$line"
-                fi
-                [ $output = "gopher" ] && printf "%s\n" "$(printf "%s" "$line" | fold -sw 80 | sed 's/^/t/')"
-                ;;
-        esac
-    done 3< "$file"
-    [ -n "$end" ] && echo "$end"
-    [ $output = "html" ] && printf '</body>\n</html>\n'
+    awk "$program" "$file" > "$(printf '%s/%s.%s' "$(dirname "$file")" "$(basename -s '.gmi' "$file")" "$extension")"
 done 4<< EOF
 $files
 EOF