tttml

tools for formatting a very basic markdown-like format
Log | Files | Refs | README

commit 90cc0673d34514966f93faff6c62c79babc8faf0
Author: Josuah Demangeon <mail@josuah.net>
Date:   Thu, 19 Apr 2018 02:04:43 +0200

initial import

Diffstat:
Amarkup-fmt | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amarkup-gopher | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amarkup-html | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 343 insertions(+), 0 deletions(-)

diff --git a/markup-fmt b/markup-fmt @@ -0,0 +1,136 @@ +#!/usr/bin/awk -f + +function fold(blk, first, prefix) +{ + len = length(prefix); + gsub("\t", " ", blk); + gsub(" +", " ", blk); + gsub("\\*+", "*", blk); gsub("_+", "_", blk); gsub("/+", "/", blk); + sub("^ *", "", blk); sub(" *$", "", blk); + gsub("[.!?] ", "& ", blk); + + if (match(blk, /^[0-9.]+ /)) + blk = substr(blk, RMATCH, RLENGTH - 1) substr(blk, RLENGTH + 1); + + for (p = first; (line = substr(blk, 1, 80 - len)) != ""; p = prefix) { + if (length(line) == 80 - len) + sub(" +[^ ]*$", "", line); + print(p line); + blk = substr(blk, length(line) + 1); + sub("^ *", "", blk); + } +} + +function tag(blk) +{ + match(blk, /^\*[^*]*\*:/); + print(substr(blk, 1, RLENGTH)); + blk = substr(blk, RLENGTH + 1); + fold(blk, "", ""); +} + +function link(blk) +{ + match(blk, /^\[[^]]*\]: [^ \t]*/) + print(substr(blk, 0, RLENGTH)); + fold(substr(blk, RLENGTH + 1), "", ""); +} + +function literal() +{ + print(""); + do { + print($0); + } while (getline && match($0, /^\t/)); +} + +function title(blk) +{ + fold(blk, "", ""); + half = "========================================"; + print(half half); +} + +function heading(blk) +{ + print("\n"); + fold(blk, "", ""); + half = "----------------------------------------"; + print(half half); +} + +function subheading(str) +{ + print("\n\n### " str); +} + +function printblk(blk) +{ + if (blk) print(""); + + if (type == PARAGRAPH) fold(blk, "", ""); + else if (type == QUOTE) fold(blk, "> ", "> "); + else if (type == LIST) fold(blk, "- ", " "); + else if (type == TAG) tag(blk); + else if (type == LINK) link(blk); +} + +BEGIN { + PARAGRAPH = 1; QUOTE = 2; LIST = 3; TAG = 4; LINK = 5; +} + +# print the append line set type or skip append +# last block to current print the to block +# right now block or not current block (see the end) + +/^[ \t]*$/ { + printblk(blk); blk = $0; type = PARAGRAPH; next; +} + +sub(/^[-*] /, "") { + printblk(blk); blk = $0; type = LIST; next; +} + +/^\[[^]]*\]: / { + printblk(blk); blk = $0; type = LINK; next; +} + +/^\*[^*]*\*:/ { + printblk(blk); blk = $0; type = TAG; next; +} + +sub(/^> */, "") { + type = QUOTE; +} + +/^\t/ { + printblk(blk); blk = ""; literal(); next; +} + +sub(/^# +/, "") { + printblk(blk); blk = ""; title($0); next; +} + +sub(/^## +/, "") { + printblk(blk); blk = ""; heading($0); next; +} + +sub(/^###+ */, "") { + printblk(blk); blk = ""; subheading($0); next; +} + +/^=+$/ { + title(blk); blk = ""; next; +} + +/^-+$/ { + heading(blk); blk = ""; next; +} + +{ + blk = blk " " $0; +} + +END { + printblk(blk); +} diff --git a/markup-gopher b/markup-gopher @@ -0,0 +1,62 @@ +#!/usr/bin/awk -f + +# format plain text markdown-style document into browsable gophermap + +# It recognises and convert the following patterns: +# +# [tag]: proto://host:port/path +# [tag]: proto://host/path +# [tag]: gopher://host:port/t/path +# [tag]: gopher://host/t/path +# [tag]: //host/t/path +# [tag]: /t/path + +BEGIN { + if (ARGC < 3 || 4 < ARGC) { + print("usage: markup-gph <host> <port> [<file>]"); + exit(1); + } + HOST = ARGV[1]; + PORT = ARGV[2]; + ARGV[1] = ARGV[3]; + ARGC = 2; +} + +match($0, "^\\[[^]]*\\]: ") { + host = HOST; uri = substr($0, RLENGTH + 1); + port = PORT; tag = substr($0, 2, RLENGTH - 4); + path = uri; type = "0"; + + sub("^[ \t]*", "", path); + if (match(path, "/$")) + type = "1"; + else if (match(path, "^[01789aghI]/")) { + type = substr(path, 1, 1); + sub(".", "", path); + } + + if (sub("^gopher://", "", uri)) { + host = uri; sub("/.*", "", host); + path = uri; sub(".*/", "/", path); + if (match(host, "[a-z.-]+:")) { + port = substr(host, RLENGTH + 1); + host = substr(host, RSTART, RLENGTH - 1); + } + } else if (match(uri, "^[a-z0-9-]+:")) { + type = "h"; host = ""; port = ""; + path = "URL:" uri; + } + + for (name = ""; match($0, /[^ \t]/); name = name " " $0) + if (!getline) { end = 1; break; } + if (name == "" || name == " ") name = " " uri; + + printf("%s%s:%s\t%s\t%s\t%s\n", type, tag, name, path, host, port); + + if (end) exit; +} + +{ + gsub("\t", " "); + printf("i%s\t\t\t\n", $0); +} diff --git a/markup-html b/markup-html @@ -0,0 +1,145 @@ +#!/usr/bin/awk -f + +function esc(str) +{ + gsub("&", "\\&amp;", str); + gsub("<", "\\&lt;", str); + gsub(">", "\\&gt;", str); + gsub("\"", "\\&quot;", str); + return str; +} + +function format(blk) +{ + gsub("[*_]", "", blk); + + for (out = ""; match(blk, /\[[^]]+\]/);) { + out = out substr(blk, 1, RSTART - 1); + label = substr(blk, RSTART + 1, RLENGTH - 2); + out = out sprintf("<sup>[<a href=\"#%s\">%s</a>]</sup>", label, label); + blk = substr(blk, RSTART + RLENGTH); + } + out = out blk; + return out; +} + +function paragraph(blk) +{ + if (blk) + print("<p>" format(esc(blk)) "</p>"); +} + +function title(blk) +{ + print("\n<h1>" esc(blk) "</h1>\n"); +} + +function heading(blk) +{ + print("\n<h2>" esc(blk) "</h2>\n"); +} + +function subheading(str) +{ + print("\n<h3>" esc(str) "</h3>\n"); +} + +function tag(blk) +{ + print(""); + match(blk, /^\*[^*]*\*:/); + print("<dt>" esc(substr(blk, 1, RLENGTH)) "</dt>"); + print("<dd>" esc(substr(blk, RLENGTH + 1)) "</dd>"); +} + +function link(blk) +{ + match(blk, /^\[[^]]*\]:/); + label = esc(substr(blk, RSTART + 1, RLENGTH - 3)); + blk = substr(blk, RLENGTH + 1); + match(blk, /[^ \t]+/); + printf("<p id=\"%s\">%s: <a href=\"%s\">%s</a></p>\n", + esc(label), + esc(label), + esc(substr(blk, RSTART, RLENGTH + 1)), + esc(substr(blk, RSTART + RLENGTH))); +} + +function literal() +{ + print("<pre>"); + sub(/^\t/, "", $0); + do { + print(esc($0)); + } while (getline && sub(/^\t/, "", $0)); + print("</pre>"); +} + +function printblk(blk) +{ + if (type == PARAGRAPH) paragraph(blk); + else if (type == QUOTE) quote(blk, "> ", "> "); + else if (type == LIST) list(blk, "- ", " "); + else if (type == TAG) tag(blk); + else if (type == LINK) link(blk); +} + +BEGIN { + PARAGRAPH = 1; QUOTE = 2; LIST = 3; TAG = 4; LINK = 5; +} + +# print the append line set type or skip append +# last block to current print the to block +# right now block or not current block (see the end) + +/^[ \t]*$/ { + printblk(blk); blk = $0; type = PARAGRAPH; next; +} + +sub(/^[-*] /, "") { + printblk(blk); blk = $0; type = LIST; next; +} + +/^\[[^]]*\]:/ { + printblk(blk); blk = $0; type = LINK; next; +} + +/^\*[^*]*\*:/ { + printblk(blk); blk = $0; type = TAG; next; +} + +sub(/^> */, "") { + type = QUOTE; +} + +/^\t/ { + printblk(blk); blk = ""; literal(); next; +} + +sub(/^# +/, "") { + printblk(blk); blk = ""; title($0); next; +} + +sub(/^## +/, "") { + printblk(blk); blk = ""; heading($0); next; +} + +sub(/^###+ */, "") { + printblk(blk); blk = ""; subheading($0); next; +} + +/^=+$/ { + title(blk); blk = ""; next; +} + +/^-+$/ { + heading(blk); blk = ""; next; +} + +{ + blk = blk " " $0; +} + +END { + printblk(blk); +}