From 33f1f863e91b65339462040341f7d5a7adeecfc4 Mon Sep 17 00:00:00 2001 From: gravel Date: Sat, 18 Nov 2023 10:14:02 +0000 Subject: [PATCH] Implement SSG for non-HTML & subfolders --- .gitignore | 1 + php/generate-html.php | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 47bff10..54dfce4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ output/servers.json # Generated HTML output/*.html +output/**/*.html # Downloaded QR codes output/qr-codes diff --git a/php/generate-html.php b/php/generate-html.php index efe7254..afcc4aa 100644 --- a/php/generate-html.php +++ b/php/generate-html.php @@ -23,9 +23,9 @@ } /** - * Generate HTML from PHP files in the templates directory. + * Generate files from PHP templates in the templates directory. */ - function generate_html() { + function generate_files() { global $LOGGING_VERBOSITY, $TEMPLATES_ROOT, $DOCUMENT_ROOT; $flags = LoggingVerbosity::getVerbosityFlags($LOGGING_VERBOSITY)[1]; @@ -35,14 +35,21 @@ if (str_contains("$phppath", "/+") || $phppath[0] == "+") continue; - + $filename = basename($phppath); $docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath); $relpath = str_replace($TEMPLATES_ROOT, "", $phppath); - $docpath = str_replace(".php", ".html", $docpath); + $dirname = dirname($relpath); + if (preg_match("/[^.]+\\.\w+\\.php$/", $filename) == 1) { + $docpath = str_replace(".php", "", $docpath); + } else { + $docpath = str_replace(".php", ".html", $docpath); + } // We do this to isolate the environment and include-once triggers, // otherwise we could include the documents in an ob_* wrapper. + mkdir("$DOCUMENT_ROOT/$dirname", recursive: true); + log_info("Generating output for $relpath."); $output = []; @@ -51,7 +58,7 @@ exec("cd '$TEMPLATES_ROOT'; php '$phppath' $flags", $output, $exit_code); if ($exit_code != 0 || empty($output)) { - log_error("HTML generation failed."); + log_error("Site generation failed."); exit(255); } @@ -60,8 +67,8 @@ file_put_contents($docpath, join("\n", $output)); } - log_info("Done generating HTML."); + log_info("Done generating site."); } - generate_html(); + generate_files(); ?>