dedent global block too, extract dedent() helper
This commit is contained in:
1 parent
79eaa7e28b
commit
db86ff63e4
1 file changed
+16
-15
+16
-15
@@ -5,6 +5,20 @@ define('CADDYFILE_STORE', getenv('CADDYFILE_STORE') ?: '/data/Caddyfile');
|
|||||||
define('CADDYFILE_IMPORT', getenv('CADDYFILE_IMPORT') ?: '');
|
define('CADDYFILE_IMPORT', getenv('CADDYFILE_IMPORT') ?: '');
|
||||||
|
|
||||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||||
|
function dedent(array $lines): array {
|
||||||
|
$nonempty = array_filter($lines, fn($l) => trim($l) !== '');
|
||||||
|
if (!$nonempty) return $lines;
|
||||||
|
preg_match('/^(\s*)/', reset($nonempty), $ind);
|
||||||
|
$prefix = $ind[1];
|
||||||
|
foreach ($nonempty as $l) {
|
||||||
|
preg_match('/^(\s*)/', $l, $m);
|
||||||
|
$len = min(strlen($prefix), strlen($m[1]));
|
||||||
|
while ($len > 0 && substr($prefix, 0, $len) !== substr($m[1], 0, $len)) $len--;
|
||||||
|
$prefix = substr($prefix, 0, $len);
|
||||||
|
}
|
||||||
|
return $prefix !== '' ? array_map(fn($l) => substr($l, strlen($prefix)), $lines) : $lines;
|
||||||
|
}
|
||||||
|
|
||||||
function parseCaddyfile(string $content): array {
|
function parseCaddyfile(string $content): array {
|
||||||
$result = ['global' => '', 'sites' => []];
|
$result = ['global' => '', 'sites' => []];
|
||||||
$lines = explode("\n", $content);
|
$lines = explode("\n", $content);
|
||||||
@@ -24,7 +38,7 @@ function parseCaddyfile(string $content): array {
|
|||||||
if ($depth > 0) $block[] = $lines[$i];
|
if ($depth > 0) $block[] = $lines[$i];
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
$result['global'] = implode("\n", $block);
|
$result['global'] = implode("\n", dedent($block));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,20 +52,7 @@ function parseCaddyfile(string $content): array {
|
|||||||
if ($depth > 0) $block[] = $lines[$i];
|
if ($depth > 0) $block[] = $lines[$i];
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
// Strip common leading indent from body lines
|
$result['sites'][] = ['address' => $address, 'body' => implode("\n", dedent($block))];
|
||||||
$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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user