From 79eaa7e28b7e50a4ec37c4f252e7a1005d0ca1a2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 6 Apr 2026 23:15:33 +0200 Subject: [PATCH] dedent site body lines when parsing Caddyfile --- app/index.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/index.php b/app/index.php index a2c2278..a396e01 100644 --- a/app/index.php +++ b/app/index.php @@ -38,6 +38,19 @@ function parseCaddyfile(string $content): array { if ($depth > 0) $block[] = $lines[$i]; $i++; } + // Strip common leading indent from body lines + $nonempty = array_filter($block, fn($l) => trim($l) !== ''); + if ($nonempty) { + preg_match('/^(\s*)/', reset($nonempty), $ind); + $prefix = $ind[1]; + foreach ($nonempty as $l) { + preg_match('/^(\s*)/', $l, $m2); + $len = min(strlen($prefix), strlen($m2[1])); + while ($len > 0 && substr($prefix, 0, $len) !== substr($m2[1], 0, $len)) $len--; + $prefix = substr($prefix, 0, $len); + } + if ($prefix !== '') $block = array_map(fn($l) => substr($l, strlen($prefix)), $block); + } $result['sites'][] = ['address' => $address, 'body' => implode("\n", $block)]; continue; }