Add helper function to find page-specific CSS files Add CSS loading to base template Update rendering functions to include page CSS Update router to include page CSS for static pages
118 lines
4.1 KiB
PHP
118 lines
4.1 KiB
PHP
<?php
|
|
|
|
function renderContentFile(string $filePath): string {
|
|
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
|
ob_start();
|
|
if ($ext === 'md') {
|
|
require_once __DIR__ . '/cache.php';
|
|
$cached = getCachedMarkdown($filePath);
|
|
|
|
if ($cached !== null) {
|
|
echo $cached;
|
|
} else {
|
|
if (!class_exists('Parsedown')) {
|
|
require_once __DIR__ . '/vendor/Parsedown.php';
|
|
}
|
|
$html = '<article>' . (new Parsedown())->text(file_get_contents($filePath)) . '</article>';
|
|
setCachedMarkdown($filePath, $html);
|
|
echo $html;
|
|
}
|
|
} elseif (in_array($ext, ['html', 'php'])) {
|
|
include $filePath;
|
|
}
|
|
return ob_get_clean();
|
|
}
|
|
|
|
function renderTemplate(Context $ctx, string $content, int $statusCode = 200): void {
|
|
// Extract all necessary variables for base template
|
|
$currentLang = $ctx->currentLang;
|
|
$navigation = $ctx->navigation;
|
|
$homeLabel = $ctx->homeLabel;
|
|
$translations = $ctx->translations;
|
|
$pageTitle = null; // No specific page title for error pages
|
|
|
|
http_response_code($statusCode);
|
|
include $ctx->templates->base;
|
|
exit;
|
|
}
|
|
|
|
function renderFile(Context $ctx, string $filePath): void {
|
|
$realPath = realpath($filePath);
|
|
if (!$realPath || !str_starts_with($realPath, $ctx->contentDir) || !is_readable($realPath)) {
|
|
renderTemplate($ctx, "<article><h1>403 Forbidden</h1><p>Access denied.</p></article>", 403);
|
|
}
|
|
|
|
$ext = pathinfo($realPath, PATHINFO_EXTENSION);
|
|
|
|
if (in_array($ext, CONTENT_EXTENSIONS)) {
|
|
$content = renderContentFile($realPath);
|
|
|
|
// Prepare template variables using property hooks
|
|
$currentLang = $ctx->currentLang;
|
|
$navigation = $ctx->navigation;
|
|
$homeLabel = $ctx->homeLabel;
|
|
$translations = $ctx->translations;
|
|
|
|
$pageDir = dirname($realPath);
|
|
$pageMetadata = loadMetadata($pageDir, $ctx->currentLang, $ctx->defaultLang);
|
|
$pageTitle = $pageMetadata['title'] ?? null;
|
|
$metaDescription = extractMetaDescription($pageDir, $pageMetadata, $ctx->currentLang, $ctx->defaultLang);
|
|
|
|
// Check for page-specific CSS
|
|
$pageCss = findPageCss($pageDir, $ctx->contentDir);
|
|
$pageCssUrl = $pageCss['url'] ?? null;
|
|
$pageCssHash = $pageCss['hash'] ?? null;
|
|
|
|
// Wrap content with page template
|
|
ob_start();
|
|
include $ctx->templates->page;
|
|
$content = ob_get_clean();
|
|
|
|
// Wrap with base template
|
|
include $ctx->templates->base;
|
|
exit;
|
|
}
|
|
|
|
// Serve other file types directly
|
|
header('Content-Type: ' . (mime_content_type($realPath) ?: 'application/octet-stream'));
|
|
readfile($realPath);
|
|
exit;
|
|
}
|
|
|
|
function renderMultipleFiles(Context $ctx, array $filePaths, string $pageDir): void {
|
|
// Validate all files are safe
|
|
foreach ($filePaths as $filePath) {
|
|
$realPath = realpath($filePath);
|
|
if (!$realPath || !str_starts_with($realPath, $ctx->contentDir) || !is_readable($realPath)) {
|
|
renderTemplate($ctx, "<article><h1>403 Forbidden</h1><p>Access denied.</p></article>", 403);
|
|
}
|
|
}
|
|
|
|
// Render all content files in order
|
|
$content = implode('', array_map('renderContentFile', $filePaths));
|
|
|
|
// Prepare template variables using property hooks
|
|
$currentLang = $ctx->currentLang;
|
|
$navigation = $ctx->navigation;
|
|
$homeLabel = $ctx->homeLabel;
|
|
$translations = $ctx->translations;
|
|
|
|
$pageMetadata = loadMetadata($pageDir, $ctx->currentLang, $ctx->defaultLang);
|
|
$pageTitle = $pageMetadata['title'] ?? null;
|
|
$metaDescription = extractMetaDescription($pageDir, $pageMetadata, $ctx->currentLang, $ctx->defaultLang);
|
|
|
|
// Check for page-specific CSS
|
|
$pageCss = findPageCss($pageDir, $ctx->contentDir);
|
|
$pageCssUrl = $pageCss['url'] ?? null;
|
|
$pageCssHash = $pageCss['hash'] ?? null;
|
|
|
|
// Wrap content with page template
|
|
ob_start();
|
|
include $ctx->templates->page;
|
|
$content = ob_get_clean();
|
|
|
|
// Wrap with base template
|
|
include $ctx->templates->base;
|
|
exit;
|
|
}
|