#!/usr/bin/awk -f
# HAT TRICK
# (C) 2022 C. Duckworth
# ht.awk converts mostly-html (with some conveniences) to actual html
/^;/ {
sub(/^;/, "", $0)
print ""
next
}
/^ { # Raw HTML
if (! (tag == "html")) {
tag = "html"
}
bufpush($0)
next
}
/^```$/ { # Raw block
if (! (tag == "raw")) {
tag = "raw"
getline
bufpush("
" $0)
} else {
bufpush("
")
buflush()
tag = ""
}
next
}
/^=>/ { # Links (Gemini-style)
if (tag == "raw") {
next
}
link = "" $3
for (i = 4; i <= NF; i++) {
link = link " " $i
}
link = link ""
bufpush(link)
next
}
/^-/ { # Unordered lists
if (tag == "raw") {
next
}
if (! (tag == "ul")) {
tag = "ul"
}
sub(/^-[ \t]*/, "", $0)
}
/^[0-9]+\./ { # Ordered lists
if (tag == "raw") {
next
}
if (! (tag == "ol")) {
tag = "ol"
}
sub(/^[0-9]+\.[ \t]/, "", $0)
}
/^>/ { # Blockquotes
if (tag == "raw") {
next
}
if (! (tag == "blockquote")) {
tag = "blockquote"
}
sub(/^>[ \t]*/, "", $0)
}
/^#+/ { # Headers
if (tag == "raw") {
next
}
match($0, /^#+/)
if (! (tag == "h" RLENGTH)) {
buflush()
tag = "h" RLENGTH
}
sub(/^#+[ \t]*/, "", $0)
}
/^$/ {
if (tag == "raw") {
next
}
buflush()
tag = ""
}
/./ {
if (! tag) {
tag = "p"
}
if (! BUF) {
bufpush("<" tag ">")
}
if (tag == "raw") {
$0 = esc($0)
}
bufpush($0)
}
END {
buflush()
}
function buflush()
{
if (BUF) {
print BUF
}
BUF = ""
if (tag && (tag != "html") && (tag != "raw")) {
print "" tag ">"
}
}
function bufpush(s)
{
BUF = BUF (BUF ? "\n" : "") s
}
function esc(t)
{
# This is of much more limited utility than I initially realized.
gsub(/&/, "\\&", t)
gsub(/, "\\<", t)
gsub(/>/, "\\>", t)
sub(/^ /, "\\ ", t)
return t
}