add JPG preview generation for pages and update routing logic
This commit is contained in:
@@ -1,25 +1,87 @@
|
||||
<?php
|
||||
$url = array_filter(explode('/',ltrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),'/')));
|
||||
|
||||
if (count($url) === 1 && str_starts_with($url[0], 'seed-')) {
|
||||
|
||||
$file = __DIR__ . '/../pages/' . $url[0] . '.html';
|
||||
if (file_exists($file)) {
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
echo file_get_contents($file);
|
||||
exit;
|
||||
}
|
||||
else {
|
||||
http_response_code(404);
|
||||
echo 'Page not found';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
else if($url[0] === 'generate') {
|
||||
include_once __DIR__ . '/generator.php';
|
||||
declare(strict_types=1);
|
||||
|
||||
$path = (string) parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
||||
$segments = array_values(array_filter(explode('/', ltrim($path, '/'))));
|
||||
$firstSegment = $segments[0] ?? '';
|
||||
|
||||
if (count($segments) === 1 && str_starts_with($firstSegment, 'seed-')) {
|
||||
$file = __DIR__ . '/../pages/' . $firstSegment . '.html';
|
||||
if (file_exists($file)) {
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
echo file_get_contents($file);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(404);
|
||||
echo 'Page not found';
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($firstSegment === 'generate') {
|
||||
include_once __DIR__ . '/generator.php';
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($firstSegment === 'list') {
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
|
||||
$pageFiles = glob(__DIR__ . '/../pages/seed-*.html') ?: [];
|
||||
usort($pageFiles, static fn (string $a, string $b): int => filemtime($b) <=> filemtime($a));
|
||||
|
||||
echo '<!doctype html><html lang="en"><head><meta charset="UTF-8">';
|
||||
echo '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
|
||||
echo '<title>Generated Seed Pages</title>';
|
||||
echo '<script src="https://cdn.tailwindcss.com"></script></head>';
|
||||
echo '<body class="min-h-screen bg-zinc-950 text-zinc-100 p-6 md:p-10">';
|
||||
echo '<div class="max-w-7xl mx-auto">';
|
||||
echo '<div class="flex flex-wrap items-center justify-between gap-4 mb-8">';
|
||||
echo '<h1 class="text-3xl md:text-4xl font-bold">Generated Seeds</h1>';
|
||||
echo '<a href="/" class="px-4 py-2 rounded-md bg-emerald-600 hover:bg-emerald-500 transition text-white font-medium">Generate New</a>';
|
||||
echo '</div>';
|
||||
|
||||
if ($pageFiles === []) {
|
||||
echo '<p class="text-zinc-300">No generated seed pages found yet.</p>';
|
||||
} else {
|
||||
echo '<div class="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 gap-6">';
|
||||
|
||||
foreach ($pageFiles as $pageFile) {
|
||||
$seed = basename($pageFile, '.html');
|
||||
$previewWebPath = '/previews/' . $seed . '.jpg';
|
||||
$previewFsPath = __DIR__ . '/previews/' . $seed . '.jpg';
|
||||
$hasPreview = file_exists($previewFsPath);
|
||||
|
||||
$title = $seed;
|
||||
$rawHtml = file_get_contents($pageFile);
|
||||
if ($rawHtml !== false && preg_match('/<title>(.*?)<\/title>/is', $rawHtml, $matches) === 1) {
|
||||
$title = trim(strip_tags($matches[1]));
|
||||
}
|
||||
|
||||
$updated = date('Y-m-d H:i:s', filemtime($pageFile));
|
||||
|
||||
echo '<a href="/' . htmlspecialchars($seed, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '" class="block rounded-xl overflow-hidden border border-zinc-800 bg-zinc-900/70 hover:border-emerald-500 transition">';
|
||||
echo '<div class="aspect-[16/10] bg-zinc-800">';
|
||||
if ($hasPreview) {
|
||||
echo '<img loading="lazy" src="' . htmlspecialchars($previewWebPath, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '" alt="Preview for ' . htmlspecialchars($seed, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '" class="w-full h-full object-cover">';
|
||||
} else {
|
||||
echo '<div class="w-full h-full grid place-items-center text-zinc-400 text-sm">Preview not available yet</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '<div class="p-4">';
|
||||
echo '<h2 class="text-lg font-semibold mb-1 line-clamp-2">' . htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</h2>';
|
||||
echo '<p class="text-xs text-zinc-400 mb-1">' . htmlspecialchars($seed, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</p>';
|
||||
echo '<p class="text-xs text-zinc-500">Updated: ' . htmlspecialchars($updated, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</p>';
|
||||
echo '</div></a>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
echo '</div></body></html>';
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user