From cecb9dab4feb5848eb6c6e8351cfc6a6862fb970 Mon Sep 17 00:00:00 2001 From: Connor Thomson Date: Sun, 20 Sep 2026 17:39:36 -0700 Subject: Add a preprocessor --- php/preprocess.php | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 php/preprocess.php (limited to 'php/preprocess.php') diff --git a/php/preprocess.php b/php/preprocess.php new file mode 100755 index 0000000..ae1057a --- /dev/null +++ b/php/preprocess.php @@ -0,0 +1,83 @@ +. + */ + +function strip_comments(string $html): string { + return preg_replace('//s', '', $html); +} + +function tidy(string $html): string { + $html = preg_replace('/[^\S\n]+$/m', '', $html); + $html = preg_replace('/\n{3,}/', "\n\n", $html); + + return trim($html, "\n"); +} + +function dedent(string $html): string { + $width = null; + + foreach (explode("\n", $html) as $line) { + if (trim($line) === '') { + continue; + } + + $leading = strlen($line) - strlen(ltrim($line, " \t")); + + if ($width === null || $leading < $width) { + $width = $leading; + } + } + + if (!$width) { + return $html; + } + + return preg_replace('/^[ \t]{1,' . $width . '}/m', '', $html); +} + +function indent(string $html, string $indent): string { + $html = dedent($html); + + if ($indent === '') { + return $html; + } + + $lines = explode("\n", $html); + + foreach ($lines as $number => $line) { + if ($line !== '') { + $lines[$number] = $indent . $line; + } + } + + return implode("\n", $lines); +} + +function preprocess(string $html): string { + return tidy(strip_comments($html)); +} + +function import(string $path): string { + if (!file_exists($path)) { + return ''; + } + + return preprocess(file_get_contents($path)); +} + +?> -- cgit v1.2.3