Add modular architecture to router
Extract configuration, helpers, content processing, and rendering logic into separate files Refactor router to use modular components
This commit is contained in:
parent
eda800d048
commit
149ba03359
5 changed files with 464 additions and 456 deletions
57
app/helpers.php
Normal file
57
app/helpers.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
function extractTitle(string $filePath, string $lang, string $defaultLang): ?string {
|
||||
$files = findAllContentFiles($filePath, $lang, $defaultLang);
|
||||
if (empty($files)) return null;
|
||||
|
||||
// Check the first content file for a title
|
||||
$file = $files[0];
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
$content = file_get_contents($file);
|
||||
|
||||
if ($ext === 'md' && preg_match('/^#\s+(.+)$/m', $content, $matches)) {
|
||||
return trim($matches[1]);
|
||||
}
|
||||
if (in_array($ext, ['html', 'php']) && preg_match('/<h1[^>]*>(.*?)<\/h1>/i', $content, $matches)) {
|
||||
return strip_tags($matches[1]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function formatNorwegianDate(string $dateString): string {
|
||||
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $dateString, $matches)) {
|
||||
$months = ['januar', 'februar', 'mars', 'april', 'mai', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'desember'];
|
||||
$day = (int)$matches[3];
|
||||
$month = $months[(int)$matches[2] - 1];
|
||||
$year = $matches[1];
|
||||
return "$day. $month $year";
|
||||
}
|
||||
return $dateString;
|
||||
}
|
||||
|
||||
function extractDateFromFolder(string $folderName): ?string {
|
||||
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})-/', $folderName, $matches)) {
|
||||
return formatNorwegianDate($matches[1] . '-' . $matches[2] . '-' . $matches[3]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findCoverImage(string $dirPath): ?string {
|
||||
$extensions = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
|
||||
foreach ($extensions as $ext) {
|
||||
if (file_exists("$dirPath/cover.$ext")) {
|
||||
return "cover.$ext";
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findPdfFile(string $dirPath): ?string {
|
||||
$files = scandir($dirPath) ?: [];
|
||||
foreach ($files as $file) {
|
||||
if (pathinfo($file, PATHINFO_EXTENSION) === 'pdf') {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue