tttml-html - tttml - converters for a simpler syntax than markdown HTML git clone git://bitreich.org/tttml git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/tttml DIR Log DIR Files DIR Refs DIR Tags DIR README --- tttml-html (2784B) --- 1 #!/usr/bin/awk -f 2 3 function esc(str) 4 { 5 gsub("&", "\\&", str); 6 gsub("<", "\\<", str); 7 gsub(">", "\\>", str); 8 gsub("\"", "\\"", str); 9 gsub("'", "\\'", str); 10 return str; 11 } 12 13 function format(blk) 14 { 15 gsub("[*_/]", "", blk); 16 17 for (out = ""; match(blk, /\[[^]]+\]/);) { 18 out = out substr(blk, 1, RSTART - 1); 19 label = substr(blk, RSTART + 1, RLENGTH - 2); 20 out = out sprintf("<sup>[<a href=\"#%s\">%s</a>]</sup>", label, label); 21 blk = substr(blk, RSTART + RLENGTH); 22 } 23 out = out blk; 24 return out; 25 } 26 27 function paragraph(blk) 28 { 29 if (blk) 30 print("<p>" format(esc(blk)) "</p>"); 31 } 32 33 function blockquote(blk) 34 { 35 if (blk) 36 print("<blockquote>" format(esc(blk)) "</blockquote>"); 37 } 38 39 function list(blk) 40 { 41 if (blk) 42 print("<ul><li>" format(esc(blk)) "</li></ul>"); 43 } 44 45 function title(blk) 46 { 47 print("\n<h1>" esc(blk) "</h1>\n"); 48 } 49 50 function heading(blk) 51 { 52 print("\n<h2>" esc(blk) "</h2>\n"); 53 } 54 55 function subheading(str) 56 { 57 print("\n<h3>" esc(str) "</h3>\n"); 58 } 59 60 function tag(blk) 61 { 62 print(""); 63 match(blk, /^\*[^*]*\*:/); 64 len = RLENGTH; 65 print("<dt>" format(esc(substr(blk, 1, len - 2))) "</dt>"); 66 print("<dd>" format(esc(substr(blk, len + 2))) "</dd>"); 67 } 68 69 function link(blk) 70 { 71 match(blk, /^\[[^]]*\]:/); 72 label = esc(substr(blk, RSTART + 1, RLENGTH - 3)); 73 blk = substr(blk, RLENGTH + 1); 74 match(blk, /[^ \t]+/); 75 printf("<p id=\"%s\">%s: <a href=\"%s\">%s</a></p>\n", 76 esc(label), 77 esc(label), 78 esc(substr(blk, RSTART, RLENGTH + 1)), 79 esc(substr(blk, RSTART + RLENGTH))); 80 } 81 82 function literal() 83 { 84 print("<pre>"); 85 sub(/^\t/, "", $0); 86 do { 87 print(esc($0)); 88 } while (getline && sub(/^\t/, "", $0)); 89 print("</pre>"); 90 } 91 92 function printblk(blk) 93 { 94 if (type == PARAGRAPH) paragraph(blk); 95 else if (type == QUOTE) blockquote(blk); 96 else if (type == LIST) list(blk); 97 else if (type == TAG) tag(blk); 98 else if (type == LINK) link(blk); 99 } 100 101 BEGIN { 102 PARAGRAPH = 1; QUOTE = 2; LIST = 3; TAG = 4; LINK = 5; 103 } 104 105 # print the append line set type or skip append 106 # last block to current print the to block 107 # right now block or not current block (see the end) 108 109 /^[ \t]*$/ { 110 printblk(blk); blk = $0; type = PARAGRAPH; next; 111 } 112 113 sub(/^[-*] /, "") { 114 printblk(blk); blk = $0; type = LIST; next; 115 } 116 117 /^\[[^]]*\]:/ { 118 printblk(blk); blk = $0; type = LINK; next; 119 } 120 121 /^\*[^*]*\*:/ { 122 printblk(blk); blk = $0; type = TAG; next; 123 } 124 125 sub(/^> */, "") { 126 type = QUOTE; 127 } 128 129 /^\t/ { 130 printblk(blk); blk = ""; literal(); next; 131 } 132 133 sub(/^# +/, "") { 134 printblk(blk); blk = ""; title($0); next; 135 } 136 137 sub(/^## +/, "") { 138 printblk(blk); blk = ""; heading($0); next; 139 } 140 141 sub(/^###+ */, "") { 142 printblk(blk); blk = ""; subheading($0); next; 143 } 144 145 /^=+$/ { 146 title(blk); blk = ""; next; 147 } 148 149 /^-+$/ { 150 heading(blk); blk = ""; next; 151 } 152 153 { 154 blk = blk " " $0; 155 } 156 157 END { 158 printblk(blk); 159 }