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