1
0

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
This commit is contained in:
Daniel Barlow 2025-03-05 22:38:48 +00:00
parent 2c7a16d792
commit 8c39b47cae

View File

@ -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=