!/usr/bin/awk -f

gmi2html2 - convert a Gemini file to an HTML body

SPDX-FileCopyrightText: 2023 Daniel Kalak

SPDX-License-Identifier: GPL-3.0-or-later

{ prev = this; }

{ if (prev == "pre-on" || prev == "pre") this = "pre"; }

   { if (this != "pre") this = "text"   ; }

/^```/ { if (this != "pre") this = "pre-on" ;

     else               this = "pre-off"; }

/^#/ { if (this != "pre") this = "h1" ; }

/^##/ { if (this != "pre") this = "h2" ; }

/^###/ { if (this != "pre") this = "h3" ; }

/^* / { if (this != "pre") this = "list" ; }

/^>/ { if (this != "pre") this = "quote" ; }

function closeln() {

if (prev == "text")    printf("</pre>");

if (prev == "pre-off") printf("</pre>");

if (prev == "list")    printf("</ul>");

if (prev == "quote")   printf("</pre></blockquote>");

}

prev != this { closeln(); }

prev { printf("\n"); }

END { closeln(); printf("\n"); }

prev != this {

if (this == "text")   printf("<pre class=text>");

if (this == "pre-on") printf("<pre class=code>");

if (this == "list")   printf("<ul>");

if (this == "quote")  printf("<blockquote><pre class=text>");

}

function makesafe(s) {

gsub(/&/, "\\&amp;",  s);

gsub(/</, "\\&lt;",   s);

gsub(/>/, "\\&gt;",   s);

gsub(/"/, "\\&quot;", s);

gsub(/'/, "\\&apos;", s);

return s;

}

function skipws() {

return makesafe(substr($0, index($0, $1)));

}

this == "text" && /^=>/ {

sub(/^=>/, "");

url = makesafe($1);

printf("<a href=\"%s\">", url);

$0 = substr($0, index($0, $1) + length($1));

printf("%s</a>", ($NF) ? skipws() : url);

next;

}

this == "text" { printf("%s", makesafe($0)); }

this == "pre" { printf("%s", makesafe($0)); }

this == "h1" { sub(/^#/, "") ; printf("%s", skipws()); }

this == "h2" { sub(/^##/, "") ; printf("%s", skipws()); }

this == "h3" { sub(/^###/, ""); printf("%s", skipws()); }

this == "list" { sub(/^* /, ""); printf("%s", skipws()); }

this == "quote" { sub(/^>/, "") ; printf("%s", skipws()); }

Proxy Information
Original URL
gemini://dkalak.de/software/gmi2html2/gmi2html2.awk
Status Code
Success (20)
Meta
text/plain
Capsule Response Time
141.425045 milliseconds
Gemini-to-HTML Time
0.900983 milliseconds

This content has been proxied by September (ba2dc).