aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-06-02 23:35:33 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-06-02 23:35:33 +1200
commit31460b8e225813a63cc28331b794769246447397 (patch)
tree89ac5278d97f4c30daafa2194b95cb1090f3fc2b
parentAdd another mi5(1df) caveat (diff)
downloaddotfiles-31460b8e225813a63cc28331b794769246447397.tar.gz
dotfiles-31460b8e225813a63cc28331b794769246447397.zip
Allow custom delimiters for mi5(1df)
-rw-r--r--bin/mi5.awk26
-rw-r--r--man/man1/mi5.1df12
2 files changed, 24 insertions, 14 deletions
diff --git a/bin/mi5.awk b/bin/mi5.awk
index 1ddc5a2f..82c5f914 100644
--- a/bin/mi5.awk
+++ b/bin/mi5.awk
@@ -1,16 +1,22 @@
# Crude m4 preprocessor
-BEGIN { mac = 0 }
+BEGIN {
+ mac = 0
+ if (!length(open))
+ open = "<%"
+ if (!length(shut))
+ shut = "%>"
+}
# Print an m4 opener as the first byte
NR == 1 { printf "`" }
# Blocks
-NF == 1 && $1 == "<%" && !mac {
+NF == 1 && $1 == open && !mac {
mac = 1
printf "'"
next
}
-NF == 1 && $1 == "%>" && mac {
+NF == 1 && $1 == shut && mac {
mac = 0
printf "`"
next
@@ -32,10 +38,10 @@ mac && NF {
dst = ""
# As long as there's a pair of opening and closing tags
- while (src ~ /<%.*%>/) {
+ while (src ~ open ".*" shut) {
# Read up to opening tag into seg, shift from src
- ind = index(src, "<%")
+ ind = index(src, open)
seg = substr(src, 1, ind - 1)
src = substr(src, ind)
@@ -44,13 +50,13 @@ mac && NF {
dst = dst seg
# Read up to closing tag into seg, shift from src
- ind = index(src, "%>")
- seg = substr(src, 1, ind + 1)
- src = substr(src, ind + 2)
+ ind = index(src, shut)
+ seg = substr(src, 1, ind + length(shut) - 1)
+ src = substr(src, ind + length(shut))
# Translate tags to quote open and close and add to dst
- sub(/^<% */ , "'", seg)
- sub(/ *%>$/ , "`", seg)
+ sub("^" open " *", "'", seg)
+ sub(" *" shut "$", "`", seg)
dst = dst seg
}
diff --git a/man/man1/mi5.1df b/man/man1/mi5.1df
index fddcf725..8c9c1c5c 100644
--- a/man/man1/mi5.1df
+++ b/man/man1/mi5.1df
@@ -7,6 +7,9 @@
FILE > out.m4
.br
.B mi5
+-v open='{{{' -v shut='}}}' FILE > out.m4
+.br
+.B mi5
FILE1 FILE2 > out.m4
.br
prog |
@@ -23,9 +26,10 @@ covers almost every usage case for the author. It's written to work with any
POSIX awk and to generate output for any POSIX m4.
.P
mi5 inverts m4's usual approach by approaching most of the file as if it were
-part of an m4 quote, with <% and %> as the delimiters to specify markers in
-which macro expansion should occur. This is therefore a way to shoehorn m4 into
-working in a way reminiscent of templating libraries or languages like PHP.
+part of an m4 quote, with <% and %> as the (default) delimiters to specify
+markers in which macro expansion should occur. This is therefore a way to
+shoehorn m4 into working in a way reminiscent of templating libraries or
+languages like PHP.
.P
Macros can be expanded as blocks:
.P
@@ -52,7 +56,7 @@ are not inserted for inline blocks.
Ideally, you do your complex macro definition in a block at the top of your
file, and your simple macro expansion of those results in an inline.
.SH CAVEATS
-The <% delimiters %> are hardcoded for now, and there's no way to escape them.
+There's no way to escape the delimiters.
.P
Inline expansions cannot span multiple lines. Use blocks for that.
.P