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,8 +1,9 @@
|
|||
<?php
|
||||
|
||||
// Load modular components
|
||||
require_once __DIR__ . '/config.php';
|
||||
require_once __DIR__ . '/constants.php';
|
||||
require_once __DIR__ . '/helpers.php';
|
||||
require_once __DIR__ . '/config.php';
|
||||
require_once __DIR__ . '/content.php';
|
||||
require_once __DIR__ . '/rendering.php';
|
||||
|
||||
|
|
@ -16,9 +17,7 @@ if (file_exists($assetPath) && is_file($assetPath)) {
|
|||
|
||||
// Handle frontpage
|
||||
if (empty($requestPath)) {
|
||||
// Find all content files in the root content directory
|
||||
$contentFiles = findAllContentFiles($contentDir, $currentLang, $defaultLang);
|
||||
|
||||
if (!empty($contentFiles)) {
|
||||
renderMultipleFiles($contentFiles, $contentDir);
|
||||
}
|
||||
|
|
@ -56,19 +55,7 @@ switch ($parsedPath['type']) {
|
|||
$pageContent = null;
|
||||
$contentFiles = findAllContentFiles($dir, $currentLang, $defaultLang);
|
||||
if (!empty($contentFiles)) {
|
||||
ob_start();
|
||||
foreach ($contentFiles as $file) {
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
if ($ext === 'md') {
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
}
|
||||
echo (new Parsedown())->text(file_get_contents($file));
|
||||
} else {
|
||||
include $file;
|
||||
}
|
||||
}
|
||||
$pageContent = ob_get_clean();
|
||||
$pageContent = implode('', array_map('renderContentFile', $contentFiles));
|
||||
}
|
||||
|
||||
// Load metadata for this directory
|
||||
|
|
@ -79,10 +66,10 @@ switch ($parsedPath['type']) {
|
|||
$templateName = $metadata['page_template'];
|
||||
// Add .php extension if not present
|
||||
if (!str_ends_with($templateName, '.php')) {
|
||||
$templateName .= '.php';
|
||||
$templateName = str_replace('.php', '', $templateName);
|
||||
}
|
||||
$customTemplate = dirname(__DIR__) . '/custom/templates/' . $templateName;
|
||||
$defaultTemplate = __DIR__ . '/default/templates/' . $templateName;
|
||||
$customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php";
|
||||
$defaultTemplate = __DIR__ . "/default/templates/$templateName.php";
|
||||
|
||||
if (file_exists($customTemplate)) {
|
||||
$listTemplate = $customTemplate;
|
||||
|
|
@ -92,13 +79,11 @@ switch ($parsedPath['type']) {
|
|||
// If template doesn't exist, fall back to default $listTemplate
|
||||
}
|
||||
|
||||
// Default directory listing
|
||||
$subdirs = array_filter(
|
||||
scandir($dir) ?: [],
|
||||
fn($item) => !in_array($item, ['.', '..']) && is_dir("$dir/$item")
|
||||
);
|
||||
// Build list items
|
||||
$subdirs = getSubdirectories($dir);
|
||||
$langPrefix = getLangPrefix($currentLang, $defaultLang);
|
||||
|
||||
$items = array_filter(array_map(function($item) use ($dir, $requestPath, $currentLang, $defaultLang) {
|
||||
$items = array_filter(array_map(function($item) use ($dir, $requestPath, $currentLang, $defaultLang, $langPrefix) {
|
||||
$itemPath = "$dir/$item";
|
||||
|
||||
// Check if content exists for current language
|
||||
|
|
@ -119,20 +104,20 @@ switch ($parsedPath['type']) {
|
|||
$date = extractDateFromFolder($item) ?: date("F d, Y", filemtime($itemPath));
|
||||
}
|
||||
|
||||
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
|
||||
|
||||
// Use translated slug if available, otherwise use folder name
|
||||
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))
|
||||
? $metadata['slug']
|
||||
: $item;
|
||||
|
||||
$baseUrl = $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug);
|
||||
|
||||
return [
|
||||
'title' => $title,
|
||||
'date' => $date,
|
||||
'url' => $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug),
|
||||
'cover' => $coverImage ? $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug) . '/' . $coverImage : null,
|
||||
'url' => $baseUrl,
|
||||
'cover' => $coverImage ? "$baseUrl/$coverImage" : null,
|
||||
'summary' => $metadata['summary'] ?? null,
|
||||
'pdf' => $pdfFile ? $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug) . '/' . $pdfFile : null,
|
||||
'pdf' => $pdfFile ? "$baseUrl/$pdfFile" : null,
|
||||
'redirect' => $metadata['redirect'] ?? null
|
||||
];
|
||||
}, $subdirs));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue