#!/usr/bin/awk -f # -*- indent-tabs-mode: t; -*- # HAT TRICK # (C) 2022 C. Duckworth ### Commentary: ### Code: BEGIN { split("html,gemini,gopher", HT_FORMATS_AVAILABLE, ",") process_arguments() normalize_ht_formats() if (! HT_TAGCHARS[1]) { split("b:**,i://,code:``", HT_TAGCHARS, ",") } # Output buffer. The output will be chunked into blocks. BUFFER = "" # Default block type DEFAULT_BLOCK = "p" # The current block type. We start with a standard paragraph. BLOCK = DEFAULT_BLOCK } ### RAW TEXT /^>>>/ { BLOCK = "raw" if ($1) { split($1, raw_type, ",") } } /^<</ { # Block quote } /^-/ { # Unordered list } /^[0-9]\./ { # Ordered list } /^---$/ { # Section break } /^$/ { # End a block buflush() BLOCK = DEFAULT_BLOCK } ### LINES /^=>/ { # Link } /^") } print } function bufpush(str, sep) { # Push STR onto the buffer after SEP (defaults to space). sep = sep ? sep : " " BUFFER = BUFFER (BUFFER ? sep : "") str } function ht_print(str) { if (HT_FORMATS_COUNT == 1) { print str } else { split(str, arr, "\n") for (format in HT_FORMATS) { line = 1 while (arr[line]) { printf "%s\t%s\n", format, arr[line++] } } } } function html_escape(str) { # Escape HTML entities and beginning-line spaces. gsub(/&/, "\\&", t) gsub(//, "\\>", t) sub(/^ /, "\\ ", t) return t } function normalize_ht_formats() { for (format in HT_FORMATS_AVAILABLE) { normat[format] = 0 } if (! HT_FORMATS[1]) { for (i in HT_FORMATS_AVAILABLE) { HT_FORMATS[i] = HT_FORMATS_AVAILABLE[i] } } for (format in HT_FORMATS) { if (format == "all") { for (i in HT_FORMATS_AVAILABLE) { HT_FORMATS[i] = HT_FORMATS_AVAILABLE[i] } return } else if (format ~ /^-/) { delete normat[substr(format, 2)] } else { normat[format] = 1 } } for (format in normat) { if (normat[format]) { HT_FORMATS[format] = format } } for (format in HT_FORMATS) { HT_FORMATS_COUNT++ } } function process_arguments() { a = 1 HT_FORMATS[1] = 0 HT_TAGCHARS[1] = 0 while (ARGV[a]) { if (a == "-c" || a ~ /^--chars=/) { # HTML tag <-> markup character correspondance if (a == "-c") { a++ } else if (a ~ /^--chars=/) { sub(/^[^=]*=/, "", a) # HT_TAGCHARS is an array } split(a, HT_TAGCHARS, ",") } else if (a == "-f" || a ~ /^--format=/) { # Output format if (a == "-f") { a++ } else if (a ~ /^--format=/) { sub(/^[^=]*=/, "", a) # HT_FORMATS is an array } split(a, HT_FORMATS, ",") } a++ } }