From d17a818581cf73ec2801bfd8d80a2e6e95c3b412 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 25 Jan 2024 23:04:02 -0600 Subject: Add templating capability --- et | 32 ++++++++++++++++++++++---------- template.htm | 3 +++ test.et | 1 + tet | 9 +++++++++ 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 template.htm create mode 100755 tet diff --git a/et b/et index 144b10a..19a8833 100755 --- a/et +++ b/et @@ -3,24 +3,31 @@ # usage: et | sh BEGIN { - dend = ";doc_end;" + # Tuneables + postproc = postproc ? postproc : "shexpand" + bodyfunc = bodyfunc ? bodyfunc : "body" + # Globals + wrpfx = ";:"; shxpfx = "!shx!"; def_docend = "---end---" + pretext = bodyfunc"(){\n"; posttext = "}\n"bodyfunc # Ask sed to do these b/c awk has no capture groups ;_; shellfix = "sed -E" \ " -e 's/`/\\\\`/g'" \ " -e 's/(^|[^\\$])\\$([^\\$]|$)/\\1\\\\$\\2/g'" \ " -e 's/(^|[^\\$])\\$(\\$+)([^\\$]|$)/\\1\\2\\3/g'" - shxwrap = " -e 's/^/:/'" + shxwrap = " -e 's/^/"wrpfx"/'" htmlfix = "sed -E" \ " -e 's#([^\\\\]|^)&#\\1\\&#g'" \ " -e 's#([^\\\\]|^)<#\\1\\<#g'" \ " -e 's#([^\\\\]|^)>#\\1\\>#g'" \ " -e 's#\\\\([&<>])#\\1#g'" true = 1 ; false = 0 - print "#!/bin/sh\n########## generated with excitable text #########" - print "shexpand()(eval \"$(:;(echo 'cat<<.';cat|sed 's/^/,/';echo .)"\ - "|unwrap ,)\")" + print "### excitable text ###" + print "wrap()(sed \"s/^/$1/\");unwrap()(sed \"s/^$1//\")" + print "shexpand()(eval"\ + " \"$(:;(echo 'cat<<.';sed 's/^/"shxpfx"/';echo .)"\ + "|unwrap "shxpfx")\")" print "shellfix()(" shellfix ")\nhtmlfix()(" htmlfix ")" - print "wrap()(sed \"s/^/$1/\")\nunwrap()(sed \"s/^$1//\")" + print "postproc=" postproc } end[endn] && $0 == end[endn] { @@ -39,9 +46,10 @@ end[endn] && $0 == end[endn] { /^\.\.\./ { printpar() docp = !docp - docend = $2 ? $2 : dend - if (docp) print "unwrap :<<'" docend "'|shexpand" - else print docend "\n" + docend = $2 ? $2 : def_docend + if (docp) print pretext "unwrap '" wrpfx "'" \ + "<<'" docend "'|$postproc" + else end_text() next } @@ -71,7 +79,11 @@ end[endn] && $0 == end[endn] { END { if (par) printpar() - if (docp) print docend + if (docp) end_text() +} + +function end_text() { + print docend "\n" posttext } function printpar() { diff --git a/template.htm b/template.htm new file mode 100644 index 0000000..13d48d5 --- /dev/null +++ b/template.htm @@ -0,0 +1,3 @@ + +${title} +$(body) diff --git a/test.et b/test.et index 927a4af..60c59bc 100644 --- a/test.et +++ b/test.et @@ -1,5 +1,6 @@ . ./etlib.sh alias verse=cat +title="test file" ... here's a test file .a href="https://example.com" a link or something!! diff --git a/tet b/tet new file mode 100755 index 0000000..c97d677 --- /dev/null +++ b/tet @@ -0,0 +1,9 @@ +#!/bin/sh + +__template__="$1"; shift +et="$(which et||echo ./et)" + +"$et" "$@" | sed '$d' +echo 'shexpand<<%' +cat "$__template__" +echo % -- cgit 1.4.1-21-gabe81