Refactor template and content handling logic
Add constants for file extensions Extract helper functions for common operations Improve PDF file detection Simplify directory scanning operations Standardize template resolution Optimize content rendering pipeline
This commit is contained in:
parent
149ba03359
commit
32449d2edd
6 changed files with 98 additions and 145 deletions
|
|
@ -1,18 +1,35 @@
|
|||
<?php
|
||||
|
||||
function prepareTemplateContext(): array {
|
||||
global $contentDir, $currentLang, $defaultLang;
|
||||
|
||||
return [
|
||||
'navigation' => buildNavigation($contentDir, $currentLang, $defaultLang),
|
||||
'homeLabel' => loadMetadata($contentDir, $currentLang, $defaultLang)['slug'] ?? 'Home',
|
||||
'translations' => loadTranslations($currentLang)
|
||||
];
|
||||
}
|
||||
|
||||
function renderContentFile(string $filePath): string {
|
||||
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||
|
||||
ob_start();
|
||||
if ($ext === 'md') {
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
}
|
||||
echo '<article>' . (new Parsedown())->text(file_get_contents($filePath)) . '</article>';
|
||||
} elseif (in_array($ext, ['html', 'php'])) {
|
||||
include $filePath;
|
||||
}
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
function renderTemplate(string $content, int $statusCode = 200): void {
|
||||
global $baseTemplate, $contentDir, $currentLang, $defaultLang;
|
||||
|
||||
// Build navigation for templates
|
||||
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang);
|
||||
|
||||
// Load frontpage metadata for home button label
|
||||
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
||||
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
||||
|
||||
// Load translations
|
||||
$translations = loadTranslations($currentLang);
|
||||
|
||||
global $baseTemplate;
|
||||
|
||||
extract(prepareTemplateContext());
|
||||
|
||||
http_response_code($statusCode);
|
||||
include $baseTemplate;
|
||||
exit;
|
||||
|
|
@ -28,33 +45,16 @@ function renderFile(string $filePath): void {
|
|||
|
||||
$ext = pathinfo($realPath, PATHINFO_EXTENSION);
|
||||
|
||||
if (in_array($ext, ['php', 'html', 'md'])) {
|
||||
ob_start();
|
||||
if ($ext === 'md') {
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
}
|
||||
echo '<article>' . (new Parsedown())->text(file_get_contents($realPath)) . '</article>';
|
||||
} else {
|
||||
include $realPath;
|
||||
}
|
||||
$content = ob_get_clean();
|
||||
if (in_array($ext, CONTENT_EXTENSIONS)) {
|
||||
$content = renderContentFile($realPath);
|
||||
|
||||
// Build navigation for templates
|
||||
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang);
|
||||
|
||||
// Load metadata for current page/directory
|
||||
// Prepare template variables
|
||||
extract(prepareTemplateContext());
|
||||
|
||||
$pageDir = dirname($realPath);
|
||||
$pageMetadata = loadMetadata($pageDir, $currentLang, $defaultLang);
|
||||
$pageTitle = $pageMetadata['title'] ?? null;
|
||||
|
||||
// Load frontpage metadata for home button label
|
||||
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
||||
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
||||
|
||||
// Load translations
|
||||
$translations = loadTranslations($currentLang);
|
||||
|
||||
// Wrap content with page template
|
||||
ob_start();
|
||||
include $pageTemplate;
|
||||
|
|
@ -83,38 +83,14 @@ function renderMultipleFiles(array $filePaths, string $pageDir): void {
|
|||
}
|
||||
|
||||
// Render all content files in order
|
||||
$content = '';
|
||||
foreach ($filePaths as $filePath) {
|
||||
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||
|
||||
ob_start();
|
||||
if ($ext === 'md') {
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
}
|
||||
echo '<article>' . (new Parsedown())->text(file_get_contents($filePath)) . '</article>';
|
||||
} elseif ($ext === 'html') {
|
||||
include $filePath;
|
||||
} elseif ($ext === 'php') {
|
||||
include $filePath;
|
||||
}
|
||||
$content .= ob_get_clean();
|
||||
}
|
||||
$content = implode('', array_map('renderContentFile', $filePaths));
|
||||
|
||||
// Build navigation for templates
|
||||
$navigation = buildNavigation($contentDir, $currentLang, $defaultLang);
|
||||
|
||||
// Load metadata for current page/directory
|
||||
// Prepare template variables
|
||||
extract(prepareTemplateContext());
|
||||
|
||||
$pageMetadata = loadMetadata($pageDir, $currentLang, $defaultLang);
|
||||
$pageTitle = $pageMetadata['title'] ?? null;
|
||||
|
||||
// Load frontpage metadata for home button label
|
||||
$frontpageMetadata = loadMetadata($contentDir, $currentLang, $defaultLang);
|
||||
$homeLabel = $frontpageMetadata['slug'] ?? 'Home';
|
||||
|
||||
// Load translations
|
||||
$translations = loadTranslations($currentLang);
|
||||
|
||||
// Wrap content with page template
|
||||
ob_start();
|
||||
include $pageTemplate;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue