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:
Ruben 2025-11-01 22:54:42 +01:00
parent 149ba03359
commit 32449d2edd
6 changed files with 98 additions and 145 deletions

View file

@ -2,20 +2,18 @@
// Find all content files in a directory (supporting language variants)
function findAllContentFiles(string $dir, string $lang, string $defaultLang): array {
global $availableLangs;
if (!is_dir($dir)) return [];
$files = scandir($dir) ?: [];
$contentFiles = [];
$extensions = ['md', 'html', 'php'];
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
// Exclude system files from content
if ($file === 'index.php') continue;
if ($file === '.' || $file === '..' || $file === 'index.php') continue;
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (!in_array($ext, $extensions)) continue;
if (!in_array($ext, CONTENT_EXTENSIONS)) continue;
$filePath = "$dir/$file";
if (!is_file($filePath)) continue;
@ -27,27 +25,26 @@ function findAllContentFiles(string $dir, string $lang, string $defaultLang): ar
if (count($parts) >= 3) {
// Pattern: name.lang.ext
$fileLang = $parts[count($parts) - 2];
if (in_array($fileLang, ['no', 'en'])) {
if (in_array($fileLang, $availableLangs)) {
// Only include if it matches current language
if ($fileLang === $lang) {
$contentFiles[] = [
'path' => $filePath,
'name' => $file,
'sort_key' => $parts[0] // Use base name for sorting
'sort_key' => $parts[0]
];
}
continue;
}
}
// Default files (no language suffix) - include if current lang is default
// or if no language-specific version exists
// Default files (no language suffix) - include if no language-specific version exists
$baseName = $parts[0];
$hasLangVersion = false;
if ($lang !== $defaultLang) {
// Check if language-specific version exists
foreach ($extensions as $checkExt) {
foreach (CONTENT_EXTENSIONS as $checkExt) {
if (file_exists("$dir/$baseName.$lang.$checkExt")) {
$hasLangVersion = true;
break;
@ -86,10 +83,7 @@ function resolveTranslatedPath(string $requestPath, string $contentDir, string $
// Check all subdirectories for slug matches
$found = false;
if (is_dir($currentPath)) {
$subdirs = array_filter(
scandir($currentPath) ?: [],
fn($item) => !in_array($item, ['.', '..']) && is_dir("$currentPath/$item")
);
$subdirs = getSubdirectories($currentPath);
foreach ($subdirs as $dir) {
$metadata = loadMetadata("$currentPath/$dir", $lang, $defaultLang);
@ -123,10 +117,7 @@ function parseRequestPath(string $requestPath, string $contentDir, bool $hasTrai
if (is_dir($contentPath)) {
// Check if directory has subdirectories
$hasSubdirs = !empty(array_filter(
scandir($contentPath) ?: [],
fn($item) => !in_array($item, ['.', '..']) && is_dir("$contentPath/$item")
));
$hasSubdirs = !empty(getSubdirectories($contentPath));
// If directory has subdirectories, it's an article-type folder (list view)
if ($hasSubdirs) {
@ -176,12 +167,7 @@ function loadTranslations(string $lang): array {
function buildNavigation(string $contentDir, string $currentLang, string $defaultLang): array {
$navItems = [];
// Scan top-level directories in content
$items = array_filter(
scandir($contentDir) ?: [],
fn($item) => !in_array($item, ['.', '..']) && is_dir("$contentDir/$item")
);
$items = getSubdirectories($contentDir);
foreach ($items as $item) {
$itemPath = "$contentDir/$item";
@ -204,7 +190,7 @@ function buildNavigation(string $contentDir, string $currentLang, string $defaul
// Extract title and build URL
$title = $metadata['title'] ?? extractTitle($itemPath, $currentLang, $defaultLang) ?? ucfirst($item);
$langPrefix = $currentLang !== $defaultLang ? "/$currentLang" : '';
$langPrefix = getLangPrefix($currentLang, $defaultLang);
// Use translated slug if available
$urlSlug = ($currentLang !== $defaultLang && $metadata && isset($metadata['slug']))