From 7b853058f9d412ca896cb1fc94aa80978f02c5fd Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 21 Jan 2024 00:06:21 -0600 Subject: Initial commit --- et | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ etlib.sh | 34 +++++++++++++++++++++++++++ test.et | 31 ++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100755 et create mode 100644 etlib.sh create mode 100644 test.et diff --git a/et b/et new file mode 100755 index 0000000..06cbf4c --- /dev/null +++ b/et @@ -0,0 +1,82 @@ +#!/bin/sh +{ excitable="text" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- +# usage: et | sh + +BEGIN { + xend = xend ? xend : "...xend..." + dend = dend ? dend : "...dend..." + # We shell out to sed for this because awk doesn't do capture groups ;-; + shellfix = "sed -E -e 's/`/\\\\`/g' " \ + "-e 's/(^|[^\\$])\\$([^\\$]|$)/\\1\\\\$\\2/g' " \ + "-e 's/(^|[^\\$])\\$(\\$+)([^\\$]|$)/\\1\\2\\3/g'" + true = 1 ; false = 0 + print "shellexpand()(eval \"$(echo \"cat<<'"xend"'\";cat;echo "xend")\")" + print "shellfix()(" shellfix ")" +} + +end[endn] && $0 == end[endn] { + par = par "\n" end[endn--] "\n)" + printpar() + subdocp = false + next +} + +/\\$/ { + getline nl + sub(/\\$/,"") + $0 = $0 " " nl +} + +/^\.\.\./ { + printpar() + docp = !docp + docend = $2 ? $2 : dend + if (docp) + print "shellexpand<<" docend + else + print docend "\n" + next +} + +/^\.\./ && docp { + subdocp = true + end[++endn] = (match($0,"<<") ? substr($0,RSTART+RLENGTH) : "..") + command = substr($0, 3, RSTART ? RSTART - 2 : length) + $0 = "$$(" (command ? command : "cat") "<<" end[endn] +} + +/^\./ && docp { + specialp = 2 + gsub(/"/,"\\\\&") + ln = "$$(" substr($1, 2) + for (f=2;f<=NF;f++) + ln = ln " \"" $f "\"" + ln = ln ")" + $0 = ln +} + +/^$/ { + if (!par) next + printpar() +} + +{ par = par (par?"\n":"") $0 } +{ if (--specialp < 0) specialp = 0 } + +END { + if (par) printpar() + if (docp) print docend +} + +function printpar() { + specialp = specialp || (match(par, /^[ ]*" par "

" + print par | shellfix + print "" + close(shellfix) + } else + print par + par = "" +} diff --git a/etlib.sh b/etlib.sh new file mode 100644 index 0000000..971b879 --- /dev/null +++ b/etlib.sh @@ -0,0 +1,34 @@ +# etlib + +echo()(printf '%s\n' "$*") + +attr(){ # attr [NAME|VALUE] ATTR_STRING + case "$1" in + (name) echo "${2%%=*}" ;; + (value) echo "${2#*=}" ;; + esac +} + +html_el(){ # el TAG [ATTRS...] [TEXT...] [< INPUT] + tag="$1"; attrs= ; text= + shift + for arg + do + case "$arg" in + (*=*) attrs="$attrs ${arg%%=*}=\"${arg#*=}\""; shift ;; + (*) break ;; + esac + done + + printf '<%s%s>%s' "$tag" "$attrs" + test -n "$1" && printf '%s' "$*" + if read -r fl + then echo "$fl"; cat + else echo + fi |sed '$s,$,,' +} + +alias p='html_el p' +alias h1='html_el h1' +alias blockquote='html_el blockquote' +alias bq=blockquote diff --git a/test.et b/test.et new file mode 100644 index 0000000..d62a243 --- /dev/null +++ b/test.et @@ -0,0 +1,31 @@ +. ./etlib.sh +alias verse=cat +... +here's a test file + +.h1 it has "headers" or whatever + +..blockquote title=foo +it has paragraphs of quotes +.. + +$$(echo it has shell $scripty-stuff) + +.. +escaped par! +omg wow +.. + +..p +another one :O + +holy crap +this is great +.. + +i should have more paragraphs + +they are wonderful +i love them + +very much -- cgit 1.4.1-21-gabe81