commit 8c39b47caefcbcb0d6890c31cdd1ce90144744ac
parent 2c7a16d7926cb854a1904464e0ece1c794d341dc
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 5 Mar 2025 22:38:48 +0000
output-template: allow splicing statements instead of expression
if the text inside the delimiters begins with ; (a semicolon) then
the rest of it is expected to be one or more Lua statements. It needs
to say `return "foo"` to interpolate anything, as there is no
implicit return of the value of the last statement
Diffstat:
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/pkgs/output-template/output-template.fnl b/pkgs/output-template/output-template.fnl
@@ -35,8 +35,11 @@
}]
(string.gsub text delim
(fn [x]
- (assert ((load (.. "return " x) x :t myenv))
- (string.format "missing value for %q" x))))))
+ (let [chunk (if (= (x:sub 1 1) ";")
+ (x:sub 2)
+ (.. "return " x))]
+ (assert ((load chunk x :t myenv))
+ (string.format "missing value for %q" x)))))))
(fn run []
(let [[opening closing] arg
@@ -49,6 +52,11 @@
(expect= (pick-values 1 (substitute "{{ json_quote(\"o'reilly\") }}" "{{" "}}"))
"\"o'reilly\"")
+ (expect= (pick-values 1 (substitute "{{; local a=9; return a }}" "{{" "}}")) "9")
+
+ ;; "globals" set in one interpolation are available in subsequent ones
+ (expect= (pick-values 1 (substitute "{{; a=42; return a }} {{ a and 999 or 0 }}" "{{" "}}")) "42 999")
+
(fn slurp [name]
(with-open [f (assert (io.open name))] (f:read "*a")))
(expect=