Remove language-specific content handling
Refactor to use plugin system for language support Remove hardcoded language features from core Move language handling to plugin system Improve content file discovery Simplify context creation Add plugin system documentation Implement hook system for extensibility Add template variable hook Add context storage for plugins Improve error handling Refactor rendering logic Improve list view sorting Add support for custom list templates Improve metadata handling Add plugin system reference documentation
This commit is contained in:
parent
24ee209e17
commit
a205f2cbd7
8 changed files with 524 additions and 315 deletions
|
|
@ -14,8 +14,8 @@ function getSubdirectories(string $dir): array {
|
|||
);
|
||||
}
|
||||
|
||||
function extractTitle(string $filePath, string $lang, string $defaultLang): ?string {
|
||||
$files = findAllContentFiles($filePath, $lang, $defaultLang, []);
|
||||
function extractTitle(string $filePath): ?string {
|
||||
$files = findAllContentFiles($filePath);
|
||||
if (empty($files)) return null;
|
||||
|
||||
// Check the first content file for a title
|
||||
|
|
@ -32,17 +32,16 @@ function extractTitle(string $filePath, string $lang, string $defaultLang): ?str
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function extractDateFromFolder(string $folderName, string $lang): ?string {
|
||||
function extractDateFromFolder(string $folderName): ?string {
|
||||
if (preg_match('/^(\d{4})-(\d{2})-(\d{2})-/', $folderName, $matches)) {
|
||||
return formatDate($matches[1] . '-' . $matches[2] . '-' . $matches[3], $lang);
|
||||
$dateString = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
|
||||
// Let plugins format the date
|
||||
return Hooks::apply(Hook::PROCESS_CONTENT, $dateString, 'date_format');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findCoverImage(string $dirPath): ?string {
|
||||
// PHP 8.4: array_find() - cleaner than foreach
|
||||
$found = array_find(
|
||||
COVER_IMAGE_EXTENSIONS,
|
||||
fn($ext) => file_exists("$dirPath/cover.$ext")
|
||||
|
|
@ -51,7 +50,6 @@ function findCoverImage(string $dirPath): ?string {
|
|||
}
|
||||
|
||||
function findPdfFile(string $dirPath): ?string {
|
||||
// PHP 8.4: array_find() with glob
|
||||
$pdfs = glob("$dirPath/*.pdf") ?: [];
|
||||
return $pdfs ? basename($pdfs[0]) : null;
|
||||
}
|
||||
|
|
@ -61,54 +59,51 @@ function findPageCss(string $dirPath, string $contentDir): ?array {
|
|||
if (!file_exists($cssFile) || !is_file($cssFile)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Generate URL path relative to content directory
|
||||
|
||||
$relativePath = str_replace($contentDir, '', $dirPath);
|
||||
$relativePath = trim($relativePath, '/');
|
||||
$cssUrl = '/' . ($relativePath ? $relativePath . '/' : '') . 'styles.css';
|
||||
|
||||
|
||||
return [
|
||||
'url' => $cssUrl,
|
||||
'hash' => hash_file('md5', $cssFile)
|
||||
];
|
||||
}
|
||||
|
||||
function extractMetaDescription(string $dirPath, ?array $metadata, string $lang, string $defaultLang): ?string {
|
||||
function extractMetaDescription(string $dirPath, ?array $metadata): ?string {
|
||||
// 1. Check for search_description in metadata
|
||||
if ($metadata && isset($metadata['search_description'])) {
|
||||
return $metadata['search_description'];
|
||||
}
|
||||
|
||||
|
||||
// 2. Fall back to summary in metadata
|
||||
if ($metadata && isset($metadata['summary'])) {
|
||||
return $metadata['summary'];
|
||||
}
|
||||
|
||||
|
||||
// 3. Fall back to first paragraph in content files
|
||||
$files = findAllContentFiles($dirPath, $lang, $defaultLang, []);
|
||||
$files = findAllContentFiles($dirPath);
|
||||
if (empty($files)) return null;
|
||||
|
||||
|
||||
foreach ($files as $file) {
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
$content = file_get_contents($file);
|
||||
|
||||
|
||||
if ($ext === 'md') {
|
||||
// Skip headings and extract first paragraph
|
||||
$lines = explode("\n", $content);
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if (empty($line) || str_starts_with($line, '#')) continue;
|
||||
if (strlen($line) > 20) { // Ignore very short lines
|
||||
if (strlen($line) > 20) {
|
||||
return strip_tags($line);
|
||||
}
|
||||
}
|
||||
} elseif (in_array($ext, ['html', 'php'])) {
|
||||
// Extract first <p> tag content
|
||||
if (preg_match('/<p[^>]*>(.*?)<\/p>/is', $content, $matches)) {
|
||||
return strip_tags($matches[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue