Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 02:00 UTC
caddyfile: Fix formatting for backquote wrapped braces (#6903)
Ted committed
Mar 17, 2025 at 17:58 UTC
b3e692ed09f8ba15b741621c4b16d8bfee38f8a1
2 files changed
+25
-2
caddyconfig/caddyfile/formatter.go
+15
-2
@@ -61,7 +61,8 @@ func Format(input []byte) []byte {
61
heredocMarker []rune
62
heredocClosingMarker []rune
63
64
- nesting int // indentation level
64
+ nesting int // indentation level
65
+ withinBackquote bool
66
)
67
68
write := func(ch rune) {
@@ -88,6 +89,9 @@ func Format(input []byte) []byte {
89
}
90
panic(err)
91
}
92
+ if ch == '`' {
93
+ withinBackquote = !withinBackquote
94
+ }
95
96
// detect whether we have the start of a heredoc
97
if !quoted && !(heredoc != heredocClosed || heredocEscaped) &&
@@ -236,14 +240,23 @@ func Format(input []byte) []byte {
240
switch {
241
case ch == '{':
242
openBrace = true
239
- openBraceWritten = false
243
openBraceSpace = spacePrior && !beginningOfLine
244
if openBraceSpace {
245
write(' ')
246
}
247
+ openBraceWritten = false
248
+ if withinBackquote {
249
+ write('{')
250
+ openBraceWritten = true
251
+ continue
252
+ }
253
continue
254
255
case ch == '}' && (spacePrior || !openBrace):
256
+ if withinBackquote {
257
+ write('}')
258
+ continue
259
+ }
260
if last != '\n' {
261
nextLine()
262
}
caddyconfig/caddyfile/formatter_test.go
+10
@@ -434,6 +434,16 @@ block2 {
434
}
435
`,
436
},
437
+ {
438
+ description: "Preserve braces wrapped by backquotes",
439
+ input: "block {respond `All braces should remain: {{now | date \"2006\"}}`}",
440
+ expect: "block {respond `All braces should remain: {{now | date \"2006\"}}`}",
441
+ },
442
+ {
443
+ description: "Preserve braces wrapped by quotes",
444
+ input: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
445
+ expect: "block {respond \"All braces should remain: {{now | date `2006`}}\"}",
446
+ },
447
} {
448
// the formatter should output a trailing newline,
449
// even if the tests aren't written to expect that