From a7a348461b24b8ffe969a48cd804ab5b8a3d7469 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Tue, 30 Jan 2024 23:42:09 -0600 Subject: Begin subtext.sh --- subtext.awk | 95 ++++++++++++++++++++++++++++++++++++++++--------------------- subtext.sh | 57 +++++++++++++++++++++++++++++++++++++ test.st | 4 +-- 3 files changed, 121 insertions(+), 35 deletions(-) create mode 100755 subtext.sh diff --git a/subtext.awk b/subtext.awk index c3e135b..ee04238 100644 --- a/subtext.awk +++ b/subtext.awk @@ -2,17 +2,13 @@ # (C) C Duckworth BEGIN { + true = 1; false = 0 ## Tuneables - postproc = get_value(postproc, "ST_POSTPROC", "shexpand") - bodyfunc = get_value(bodyfunc, "ST_BODYFUNC", "sub_text") - sopath = get_value(sopath, "ST_SOPATH", ".:"ENVIRON["HOME"]"/.subtext") - split(sopath, asopath, ":") - # postproc = postproc ? postproc : "shexpand" - # bodyfunc = bodyfunc ? bodyfunc : "sub_text" + runbody = bool(runbody ? runbody : true) + postproc = postproc ? postproc : "shexpand" + bodyfunc = bodyfunc ? bodyfunc : "body" + sopath = get_value(sopath, ".:"ENVIRON["HOME"]"/.subtext") ## Globals - # Booleans - true = 1 - false = 0 ## Wrap the text in a function pretext = "### begin text\n"bodyfunc"(){\n" posttext = "}\n### end text" @@ -47,7 +43,7 @@ BEGIN { ### End a block end[endn] && $0 == end[endn] { - par = par "\n" end[endn--] "\n)" + pushpar(end[endn--] "\n)", true) printpar() subdocp = false next @@ -64,27 +60,31 @@ end[endn] && $0 == end[endn] { ### Special commands ## These call subtext-internal functions -/^\.so/ { # Insert $2 verbatim (if in sopath), else error +/^#so/ { # Insert $2 verbatim (if in sopath), else error + pushpar($0) source($2) next } -### Escape sequences - -/^\.\.\./ { # Delimit document (end is optional) +/^###$/ { # Delimit document printpar() docp = !docp - if (docp) print pretext "unpreface ':'<<'_'|eval \"$ST_POSTPROC\"" - else end_text() + if (docp) { + print pretext "unpreface ':'<<'_'|eval \"$ST_POSTPROC\"" + } else { + end_text() + } next } +### Escape sequences + /^\.\./ && docp { # Begin a heredoc # ..[] [] [< [] ## wraps the line in $( ... ), basically (also quotes) specialp = 2 - gsub(/"/,"\\\\&") + gsub(/"/, "\\\\&", $0) ln = "$$(" substr($1, 2) - for (f=2;f<=NF;f++) ln = ln " \"" $f "\"" + for (f=2; f<=NF; f++) { + ln = ln " \"" $f "\"" + } ln = ln ")" $0 = ln } @@ -106,53 +108,80 @@ end[endn] && $0 == end[endn] { ### Book-keeping /^$/ { - if (!par) next + if (!par) + next printpar() } -{ par = par (par?"\n":"") $0 } -{ if (--specialp < 0) specialp = 0 } +{ + pushpar($0) + if (--specialp < 0) + specialp = 0 +} END { - if (dead) exit dead - if (par) printpar() + if (dead) + exit dead + if (par) + printpar() while (endn > 0) print "\n" end[endn--] "\n)" - if (docp) end_text() + if (docp) + end_text() + if (runbody) + print bodyfunc } function end_text() { print "_\n" posttext } +function pushpar(text, force_newline) { + par = par ((par || force_newline) ? "\n" : "") text +} + function printpar() { specialp = specialp || (match(par, /^[ ]*" par "

" + if (!subdocp && !specialp) + par = "

" par "

" shx = shellfix shxwrap print par | shx close(shx) - } else print par + } else + print par par = "" } function get_value(var, env_var, default) { - if (var) return var - else if (ENVIRON[env_var]) return ENVIRON[env_var] - else return default + if (var) + return var + else + return default +} + +function bool(var, default) { + if (var=="false" || var=="no" || var=="0") + return false + else if (var=="true" || var=="yes" || var=="1") + return true + else + return var || default } function source(name) { found = false sp = "" + split(sopath, asopath, ":") for (dir in asopath) { fn = asopath[dir] "/" name sp = asopath[dir] "\n\t" sp while ((getline ln < fn) > 0) { found = true - par = par (par?"\n":"") ln + pushpar(ln) } - if (found) break + if (found) + break } if (!found) die("Couldn't source " name "; looked in:\n\t" sp, 9) diff --git a/subtext.sh b/subtext.sh new file mode 100755 index 0000000..d845955 --- /dev/null +++ b/subtext.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +stawk() { + awk -f subtext.awk "$@" # SUBTEXT AWK SCRIPT HERE +} + +usage() { + cat>&2 <&2 'Unknown option -%s\n' "$OPTARG"; + usage 1 ;; + (*) usage 1 ;; + esac + done +} + +main() { + configure "$@" + shift $((OPTIND-1)) + stawk -vsopath="$ST_SOPATH" "$@" | + if "$ST_PIPE_SH" + then sh + else cat + fi +} + +die() { + ec="$1"; shift + printf >&2 '!! %s\n' "$*" + exit "$ec" +} + +main "$@" diff --git a/test.st b/test.st index b11bd87..18319fe 100644 --- a/test.st +++ b/test.st @@ -1,7 +1,7 @@ -.so etlib.sh +#so etlib.sh alias verse=cat title="test file" -... +### here's a test file .a href="https://example.com" a link or something!! -- cgit 1.4.1-21-gabe81