Fix jq-template to properly count {{ }} pairs

This fixes edge cases like:

```
{{ foo }} {{ bar }} {{
    baz
    | buzz
}}
```
This commit is contained in:
Tianon Gravi 2021-07-16 15:34:01 -07:00
parent faf7efe8f4
commit 1da7341a79
1 changed files with 13 additions and 1 deletions

View File

@ -17,6 +17,18 @@ function jq_escape(str, # parameters
return out
}
# return the number of times needle appears in haystack
function num(haystack, needle, # parameters
ret, i ) # locals
{
ret = 0
while (i = index(haystack, needle)) {
ret++
haystack = substr(haystack, i + length(needle))
}
return ret
}
BEGIN {
jq_expr_defs = ""
jq_expr = ""
@ -76,7 +88,7 @@ function append_jq(expr) {
agg_jq = agg_jq line
line = ""
if (!index(agg_jq, CLOSE)) {
if (num(agg_jq, OPEN) > num(agg_jq, CLOSE)) {
next
}