1 2ad4a6a2 2022-01-23 op #!/usr/bin/awk -f
3 2ad4a6a2 2022-01-23 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
5 2ad4a6a2 2022-01-23 op # Permission to use, copy, modify, and distribute this software for any
6 2ad4a6a2 2022-01-23 op # purpose with or without fee is hereby granted, provided that the above
7 2ad4a6a2 2022-01-23 op # copyright notice and this permission notice appear in all copies.
9 2ad4a6a2 2022-01-23 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2ad4a6a2 2022-01-23 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2ad4a6a2 2022-01-23 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2ad4a6a2 2022-01-23 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2ad4a6a2 2022-01-23 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2ad4a6a2 2022-01-23 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2ad4a6a2 2022-01-23 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 2ad4a6a2 2022-01-23 op !in_pre && /^###/ { output("<h3>", substr($0, 4), "</h3>"); next }
23 2ad4a6a2 2022-01-23 op !in_pre && /^##/ { output("<h2>", substr($0, 3), "</h2>"); next }
24 2ad4a6a2 2022-01-23 op !in_pre && /^#/ { output("<h1>", substr($0, 2), "</h1>"); next }
25 2ad4a6a2 2022-01-23 op !in_pre && /^>/ { output("<blockquote>", substr($0, 2), "</blockquote>"); next }
26 2ad4a6a2 2022-01-23 op !in_pre && /^\* / { output("<li>", substr($0, 2), "</li>"); next }
28 2ad4a6a2 2022-01-23 op !in_pre && /^=>/ {
29 2ad4a6a2 2022-01-23 op $0 = substr($0, 3);
32 2ad4a6a2 2022-01-23 op output_link(link, $0);
36 2ad4a6a2 2022-01-23 op !in_pre && /^```/ {
38 2ad4a6a2 2022-01-23 op if (in_list) {
40 2ad4a6a2 2022-01-23 op print("</ul>");
42 2ad4a6a2 2022-01-23 op print "<pre>";
46 2ad4a6a2 2022-01-23 op in_pre && /^```/ { in_pre = 0; print "</pre>"; next }
47 2ad4a6a2 2022-01-23 op !in_pre { output("<p>", $0, "</p>"); next }
48 2ad4a6a2 2022-01-23 op in_pre { print san($0); next }
54 2ad4a6a2 2022-01-23 op print "</pre>"
57 2ad4a6a2 2022-01-23 op function trim(s) {
58 2ad4a6a2 2022-01-23 op sub("^[ \t]*", "", s);
62 2ad4a6a2 2022-01-23 op function san(s) {
63 2ad4a6a2 2022-01-23 op gsub("&", "\\&", s)
64 2ad4a6a2 2022-01-23 op gsub("<", "\\<", s)
65 2ad4a6a2 2022-01-23 op gsub(">", "\\>", s)
69 2ad4a6a2 2022-01-23 op function output(ot, content, et) {
70 2ad4a6a2 2022-01-23 op content = trim(content);
72 2ad4a6a2 2022-01-23 op if (!in_list && ot == "<li>") {
77 2ad4a6a2 2022-01-23 op if (in_list && ot != "<li>") {
79 2ad4a6a2 2022-01-23 op print "</ul>";
82 2ad4a6a2 2022-01-23 op if (ot == "<p>" && content == "")
85 2ad4a6a2 2022-01-23 op printf("%s%s%s\n", ot, san(content), et);
88 2ad4a6a2 2022-01-23 op function output_link(link, content) {
89 2ad4a6a2 2022-01-23 op if (in_list) {
91 2ad4a6a2 2022-01-23 op print "</ul>";
94 2ad4a6a2 2022-01-23 op if (content == "")
95 2ad4a6a2 2022-01-23 op content = link;
97 2ad4a6a2 2022-01-23 op printf("<p><a href=\"%s\">%s</a></p>\n", link, trim(san(content)));