dot

packages and services management
Log | Files | Refs | README

commit ad8d91d00a22e47ae0707d4edaedc5f835f8ab42
parent d231ef5814747eb625188db050e26652a5797ffa
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon, 27 Nov 2017 02:51:51 +0100

split()-based approach

Diffstat:
Mbin/gph-html | 79+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 37 insertions(+), 42 deletions(-)

diff --git a/bin/gph-html b/bin/gph-html @@ -9,7 +9,7 @@ HEAD='<!doctype html> awk=' -function escape(str) +function esc(str) { gsub("&", "\\&amp;", str); gsub("<", "\\&lt;", str); @@ -18,30 +18,22 @@ function escape(str) return str; } -function url(gph, tail) +function url(f) { - split(tail, fields, "|"); - path = fields[2]; - host = fields[3]; - port = fields[4]; - if (host == "server" || host == "") - host = ""; - else if (gph) - host = "http://" host; - if (port == "port" || port == "") - port = ""; + if (f[4] == "server" || f[4] == "") + f[4] = ""; + else if (f[1] == "0" || f[1] == "1") + f[4] = "http://" f[4]; + if (f[5] == "port" || f[5] == "") + f[5] = ""; else - port = ":" port; - return sprintf("%s%s%s", host, port, path); + f[5] = ":" f[5]; + return sprintf("%s%s%s", f[4], f[5], f[3]); } -function link(type, tail) +function link(type, f) { - sub("\\]$", "", tail); - addr = url(type == "file" || type == "dir/", tail); - desc = tail; - sub("\\|.*", "", desc); - return sprintf("<a href=\"%s\">[%s]</a> %s", addr, type, escape(desc)); + return sprintf("<a href=\"%s\">[%s]</a> %s", url(f), type, esc(f[2])); } FNR == 1 { @@ -51,50 +43,53 @@ FNR == 1 { printf("<h1>%s</h1>\n", FILENAME); } -sub("^t", "") { - print(" " escape($0)); +/^[^[]/ { + sub("^t", ""); + printf(" %s\n", esc($0)); next; } -sub("^\\[i\\|", "") { - sub("\\|.*", "", $0); - print(" " escape($0)); +{ + sub("^\\[", "", $0); + sub("\\]$", "", $0); + split($0, f, "|"); +} + +f[1] == "i" { + printf(" %s\n", esc(f[2])); next; } -sub("^\\[0\\|", "") { - print(link("file", $0)); +f[1] == "0" { + print(link("file", f)); next; } -sub("^\\[1\\|", "") { - print(link("dir/", $0)); +f[1] == "1" { + print(link("dir/", f)); next; } -sub("^\\[8\\|", "") { - print(link("tnet", $0)); +f[1] == "8" { + print(link("tnet", f)); next; } -sub("^\\[h\\|", "") { - print(link("link", $0)); +f[1] == "h" { + print(link("link", f)); next; } -sub("^\\[g\\|", "") || sub("^\\[I", "") { - sub("\\]$", "", $0); - sub("\\|.*", "", $0); - desc = $0; - addr = url(1, $0); - sub("\\|.*", "", desc); - printf("<img alt=\"%s\" src=\"%s\"/>", escape(desc), addr); +f[1] == "g" || f[1] == "I" { + printf("<img alt=\"%s\" src=\"%s\"/>", esc(f[2]), url(f)); next; } { - print(" " escape($0)); - next + for(i = 1; i <= 5; i++) + printf("[%s]", f[i]); + print(""); + next; } END {