summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xphp/header.php2
-rwxr-xr-xphp/preprocess.php83
-rwxr-xr-xphp/render.php6
-rwxr-xr-xphp/replace.php14
4 files changed, 99 insertions, 6 deletions
diff --git a/php/header.php b/php/header.php
index 37d0612..3f7279b 100755
--- a/php/header.php
+++ b/php/header.php
@@ -20,7 +20,7 @@
require_once ROOT . '/php/replace.php';
$targets = [
- '{{navbar}}' => file_get_contents(ROOT . '/html/navbar.html')
+ '{{navbar}}' => import(ROOT . '/html/navbar.html')
];
$html = replace(ROOT . '/html/header.html', $targets);
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 @@
+<?php
+/*
+ * GNUfault.org - GNUfault's website
+ * Copyright (C) 2026 Connor Thomson
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+function strip_comments(string $html): string {
+ return preg_replace('/<!--(?!\[if).*?-->/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));
+}
+
+?>
diff --git a/php/render.php b/php/render.php
index 8e63247..902d81b 100755
--- a/php/render.php
+++ b/php/render.php
@@ -24,16 +24,16 @@ function render_content(string $page) {
$device = 'desktop';
$targets = [
- '{{stylesheets}}' => file_get_contents(ROOT . '/html/stylesheets.html'),
+ '{{stylesheets}}' => import(ROOT . '/html/stylesheets.html'),
'{{header}}' => execute(ROOT . '/php/header.php'),
- '{{content}}' => file_get_contents($page),
+ '{{content}}' => import($page),
'{{footer}}' => execute(ROOT . '/php/footer.php'),
'{{device}}' => $device
];
$main_html = replace(ROOT . '/html/main.html', $targets);
- echo $main_html;
+ echo $main_html . "\n";
}
?>
diff --git a/php/replace.php b/php/replace.php
index 9cdc5bf..dcc5fb6 100755
--- a/php/replace.php
+++ b/php/replace.php
@@ -17,14 +17,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+require_once ROOT . '/php/preprocess.php';
+
function replace(string $templatePath, array $replacements = []): string {
- $html = file_get_contents($templatePath);
+ $html = import($templatePath);
foreach ($replacements as $placeholder => $replacementValue) {
+ $replacementValue = tidy((string)$replacementValue);
+
+ $pattern = '/^([ \t]*)' . preg_quote($placeholder, '/') . '/m';
+
+ $html = preg_replace_callback($pattern, function (array $match) use ($replacementValue) {
+ return indent($replacementValue, $match[1]);
+ }, $html);
+
$html = str_replace($placeholder, $replacementValue, $html);
}
- return $html;
+ return tidy($html);
}
?>