2025-11-01 22:50:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
2025-11-01 22:54:42 +01:00
|
|
|
function resolveTemplate(string $templateName): string {
|
|
|
|
|
$customTemplate = dirname(__DIR__) . "/custom/templates/$templateName.php";
|
|
|
|
|
$defaultTemplate = __DIR__ . "/default/templates/$templateName.php";
|
|
|
|
|
return file_exists($customTemplate) ? $customTemplate : $defaultTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSubdirectories(string $dir): array {
|
|
|
|
|
if (!is_dir($dir)) return [];
|
|
|
|
|
return array_filter(
|
|
|
|
|
scandir($dir) ?: [],
|
|
|
|
|
fn($item) => !in_array($item, ['.', '..']) && is_dir("$dir/$item")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 20:19:12 +01:00
|
|
|
function extractTitle(string $filePath): ?string {
|
|
|
|
|
$files = findAllContentFiles($filePath);
|
2025-11-01 22:50:02 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 20:19:12 +01:00
|
|
|
function extractDateFromFolder(string $folderName): ?string {
|
2025-11-01 22:50:02 +01:00
|
|
|
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})-/', $folderName, $matches)) {
|
2025-11-25 20:19:12 +01:00
|
|
|
$dateString = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
|
|
|
|
|
// Let plugins format the date
|
|
|
|
|
return Hooks::apply(Hook::PROCESS_CONTENT, $dateString, 'date_format');
|
2025-11-01 22:50:02 +01:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findCoverImage(string $dirPath): ?string {
|
2025-11-01 23:33:09 +01:00
|
|
|
$found = array_find(
|
|
|
|
|
COVER_IMAGE_EXTENSIONS,
|
|
|
|
|
fn($ext) => file_exists("$dirPath/cover.$ext")
|
|
|
|
|
);
|
|
|
|
|
return $found ? "cover.$found" : null;
|
2025-11-01 22:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findPdfFile(string $dirPath): ?string {
|
2025-11-01 23:33:09 +01:00
|
|
|
$pdfs = glob("$dirPath/*.pdf") ?: [];
|
2025-11-01 22:54:42 +01:00
|
|
|
return $pdfs ? basename($pdfs[0]) : null;
|
2025-11-01 22:50:02 +01:00
|
|
|
}
|
2025-11-03 22:30:49 +01:00
|
|
|
|
2025-11-03 23:04:16 +01:00
|
|
|
function findPageCss(string $dirPath, string $contentDir): ?array {
|
|
|
|
|
$cssFile = "$dirPath/styles.css";
|
|
|
|
|
if (!file_exists($cssFile) || !is_file($cssFile)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 23:04:16 +01:00
|
|
|
$relativePath = str_replace($contentDir, '', $dirPath);
|
|
|
|
|
$relativePath = trim($relativePath, '/');
|
|
|
|
|
$cssUrl = '/' . ($relativePath ? $relativePath . '/' : '') . 'styles.css';
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 23:04:16 +01:00
|
|
|
return [
|
|
|
|
|
'url' => $cssUrl,
|
|
|
|
|
'hash' => hash_file('md5', $cssFile)
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 20:19:12 +01:00
|
|
|
function extractMetaDescription(string $dirPath, ?array $metadata): ?string {
|
2025-11-03 22:30:49 +01:00
|
|
|
// 1. Check for search_description in metadata
|
|
|
|
|
if ($metadata && isset($metadata['search_description'])) {
|
|
|
|
|
return $metadata['search_description'];
|
|
|
|
|
}
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 22:30:49 +01:00
|
|
|
// 2. Fall back to summary in metadata
|
|
|
|
|
if ($metadata && isset($metadata['summary'])) {
|
|
|
|
|
return $metadata['summary'];
|
|
|
|
|
}
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 22:30:49 +01:00
|
|
|
// 3. Fall back to first paragraph in content files
|
2025-11-25 20:19:12 +01:00
|
|
|
$files = findAllContentFiles($dirPath);
|
2025-11-03 22:30:49 +01:00
|
|
|
if (empty($files)) return null;
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 22:30:49 +01:00
|
|
|
foreach ($files as $file) {
|
|
|
|
|
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
|
|
|
|
$content = file_get_contents($file);
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 22:30:49 +01:00
|
|
|
if ($ext === 'md') {
|
|
|
|
|
$lines = explode("\n", $content);
|
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
|
$line = trim($line);
|
|
|
|
|
if (empty($line) || str_starts_with($line, '#')) continue;
|
2025-11-25 20:19:12 +01:00
|
|
|
if (strlen($line) > 20) {
|
2025-11-03 22:30:49 +01:00
|
|
|
return strip_tags($line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif (in_array($ext, ['html', 'php'])) {
|
|
|
|
|
if (preg_match('/<p[^>]*>(.*?)<\/p>/is', $content, $matches)) {
|
|
|
|
|
return strip_tags($matches[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-25 20:19:12 +01:00
|
|
|
|
2025-11-03 22:30:49 +01:00
|
|
|
return null;
|
|
|
|
|
}
|