about summary refs log tree commit diff stats
path: root/ht.awk
diff options
context:
space:
mode:
authorCase Duckworth2022-05-19 11:49:53 -0500
committerCase Duckworth2022-05-19 11:49:53 -0500
commitaf52a71c9b2d6d66ff00fb39386d8d7a5330b2b0 (patch)
treed5cfca7f6ce469b45bf4c38e4d53ae22d7bfff0a /ht.awk
downloadhat-trick-af52a71c9b2d6d66ff00fb39386d8d7a5330b2b0.tar.gz
hat-trick-af52a71c9b2d6d66ff00fb39386d8d7a5330b2b0.zip
Initial commit
I'm doing content + generation in this ... who knows if it's smart, lol.
Diffstat (limited to 'ht.awk')
-rwxr-xr-xht.awk79
1 files changed, 79 insertions, 0 deletions
diff --git a/ht.awk b/ht.awk new file mode 100755 index 0000000..5328361 --- /dev/null +++ b/ht.awk
@@ -0,0 +1,79 @@
1#!/usr/bin/awk -f
2# HAT TRICK
3# (C) 2022 C. Duckworth
4
5# ht.awk converts mostly-html (with some conveniences) to actual html
6
7function bufpush(s) {
8 BUF = BUF (BUF ? "\n" : "") s;
9}
10
11function buflush() {
12 if (BUF) print BUF;
13 BUF = "";
14 if (tag && (tag != "html")) print "</" tag ">";
15}
16
17function esc(t) {
18 gsub(/&/, "\\&amp;", t);
19 gsub(/</, "\\&lt;", t);
20 gsub(/>/, "\\&gt;", t);
21 return t;
22}
23
24/^;/ { sub(/^;/,""); print "<!--", esc($0), "-->"; next; }
25
26/^</ { # Raw HTML
27 if (! (tag == "html")) tag = "html";
28 bufpush($0);
29 next;
30}
31
32/^=>/ { # Links (Gemini-style)
33 link = "<a href=\"" esc($2) "\">" esc($3);
34 for (i=4;i<=NF;i++) link = link " " esc($i);
35 link = link "</a>";
36 bufpush(link);
37 next;
38}
39
40/^-/ { # Unordered lists
41 if (! (tag == "ul")) tag = "ul";
42 esc($0);
43 sub(/^-[ \t]*/, "<li>");
44}
45
46/^[0-9]+\./ { # Ordered lists
47 if (! (tag == "ol")) tag = "ol";
48 esc($0);
49 sub(/^[0-9]+\.[ \t]/, "<li>");
50}
51
52/^>/ { # Blockquotes
53 if (! (tag == "blockquote")) tag = "blockquote";
54 sub(/^>[ \t]*/,"");
55 esc($0);
56}
57
58/^#+/ { # Headers
59 match($0, /^#+/);
60 if (! (tag == "h" RLENGTH)) {
61 buflush();
62 tag = "h" RLENGTH;
63 }
64 sub(/^#+[ \t]*/,"");
65 esc($0);
66}
67
68/^$/ {
69 buflush();
70 tag = "";
71}
72
73/./ {
74 if (! tag) tag = "p";
75 if (! BUF) bufpush("<" tag ">");
76 bufpush($0);
77}
78
79END { buflush(); }