commit a69481e9b52fd81c8e87cd0871547dfda18c44c8
parent 4639d4cb889c00f4309d7a46bb7bf090144b72bb
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date: Sat, 19 Aug 2017 11:06:36 +0200
shell/bin/txt: notation-syntax aware formatting script
Diffstat:
2 files changed, 94 insertions(+), 1 deletion(-)
diff --git a/shell/bin/txt b/shell/bin/txt
@@ -0,0 +1,93 @@
+awk '
+
+BEGIN {
+ RS="\n\n+"
+ margin["li"] = 2
+}
+
+
+# litterals
+
+/^---\n.*\n---$/ {
+ print "\n" $0
+ next
+}
+
+/^---\n/ {
+ block = "pre"
+}
+
+/\n---$/ {
+ block = ""
+ print "\n" $0
+ next
+}
+
+block == "pre" {
+ print "\n" $0
+ next
+}
+
+
+# title
+
+NR == 1 {
+ sub("\n-+", "", $0)
+ print
+ for (i = 0; i < 80; i++)
+ printf "-"
+ print ""
+ next
+}
+
+
+# heading
+
+sub("^--- +", "", $0) {
+ d = 0
+
+ # parse
+ while (sub("[0-9#]+\\.", "", $0)) d++
+ if (sub("[0-9#]+ ", "", $0)) d++
+ for (; d < depth; depth--)
+ levels[depth] = 0
+ depth = d
+ sub("^[-. ]*", "", $0)
+ sub(" *-* *$", "", $0)
+
+ # print
+ row = "--- "
+ for (d = 1; d < depth; d++)
+ row = row levels[d] "."
+ row = row "" ++levels[d] " -- " $0 " "
+ for (i = length(row); i < 80; i++)
+ row = row "-"
+
+ print "\n" row
+ next
+}
+
+{
+ # wrap lines
+
+ sub("^[\t ]*", "", $0)
+ gsub("\n[\t ]*", " ", $0)
+ gsub(" +", " ", $0)
+ gsub("\\. ", ". ", $0)
+ gsub("\\? ", "? ", $0)
+ gsub("\\! ", "! ", $0)
+
+ width = 80 - margin[block]
+
+ print ""
+ while (length($0) > 0) {
+ row = substr($0, 1, width)
+ if (length(row) >= width)
+ sub(" [^ ]*$", "", row)
+
+ $0 = substr($0, length(row) + 1)
+ sub(" *", "")
+
+ print row
+ }
+}' "$@"
diff --git a/shell/profile b/shell/profile
@@ -18,4 +18,4 @@ export EMAIL='mail@josuah.net'
export EDITOR=vim VISUAL=$EDITOR
export PAGER=less MANPAGER=$PAGER
-export TERM=vt320
+export TERM=xterm-256color