Files
eos-modern/build/import_old_articles.php
T
2026-04-28 02:11:53 +02:00

132 lines
5.1 KiB
PHP

<?php
// build/import_old_articles.php
// Tento skript převede staré .php články na .md pro nový web
$oldDir = '/srv/http/EOS_PHP/'; // Cesta ke starému projektu
$newContentDir = '/srv/http/eos-modern/content/'; // Cesta k novému obsahu
// Ujisti se, že složka existuje
if (!is_dir($newContentDir)) {
mkdir($newContentDir, 0755, true);
}
// Seznam souborů, které chceme importovat
$files = glob($oldDir . '*.php');
// Filtr: Ignorovat systémové soubory
$ignoreFiles = [
'index.php', 'search.php', 'header.php', 'footer.php', 'base.php',
'config.php', 'functions.php', 'returnbutton.php', 'head.php',
'wiki.php', 'rozcestnik.php', 'o-nas.php', 'odkazy.php',
'z-internetu.php', 'novinky.php', 'clanky.php', 'style.css',
'script.js', 'wiki.css', 'LICENSE.md', 'README.md',
'import_old_articles.php' // Samotný tento skript
];
$importedCount = 0;
foreach ($files as $file) {
$basename = basename($file);
// Ignorovat soubory v seznamu
if (in_array($basename, $ignoreFiles)) {
continue;
}
// Získat slug (název bez .php)
$slug = pathinfo($basename, PATHINFO_FILENAME);
// Číst obsah
$content = file_get_contents($file);
// Extrahovat obsah z <main> sekce
if (preg_match('/<main[^>]*>(.*?)<\/main>/s', $content, $matches)) {
$mainContent = $matches[1];
// 1. Odstranit PHP tagy
$mainContent = preg_replace('/<\?php.*?\?>/s', '', $mainContent);
// 2. Odstranit obrázky a nahradit textem
$mainContent = preg_replace('/<img[^>]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/i', '[Obrázek: $2]', $mainContent);
// 3. Odstranit zbytečné divy a sekce (zachovat text)
$mainContent = preg_replace('/<div[^>]*>/', "\n", $mainContent);
$mainContent = preg_replace('/<\/div>/', "\n", $mainContent);
$mainContent = preg_replace('/<section[^>]*>/', "\n", $mainContent);
$mainContent = preg_replace('/<\/section>/', "\n", $mainContent);
// 4. Odstranit <br>
$mainContent = str_replace('<br>', "\n", $mainContent);
$mainContent = str_replace('<br/>', "\n", $mainContent);
// 5. Odstranit <hr>
$mainContent = preg_replace('/<hr[^>]*>/i', "---\n", $mainContent);
// 6. Odstranit <strong> a <em>
$mainContent = preg_replace('/<strong>(.*?)<\/strong>/i', '**$1**', $mainContent);
$mainContent = preg_replace('/<em>(.*?)<\/em>/i', '*$1*', $mainContent);
// 7. Odstranit <a> a převést na Markdown
$mainContent = preg_replace('/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/i', '[$2]($1)', $mainContent);
// 8. Odstranit <pre><code> a převést na ```bash
$mainContent = preg_replace('/<pre[^>]*><code[^>]*>(.*?)<\/code><\/pre>/s', "\n```bash\n$1\n```\n", $mainContent);
$mainContent = preg_replace('/<code>(.*?)<\/code>/i', '`$1`', $mainContent);
// 9. Odstranit <h1>...<h6> a převést na # (OPRAVENO)
$mainContent = preg_replace_callback('/<h([1-6])[^>]*>(.*?)<\/h\1>/i', function($matches) {
$level = intval($matches[1]); // Převod na číslo
$text = $matches[2];
return "\n" . str_repeat('#', $level) . ' ' . $text . "\n";
}, $mainContent);
// 10. Odstranit <p>
$mainContent = preg_replace('/<p[^>]*>(.*?)<\/p>/s', "\n$1\n", $mainContent);
// 11. Odstranit <ul>, <ol>, <li>
$mainContent = preg_replace('/<ul[^>]*>/i', "\n", $mainContent);
$mainContent = preg_replace('/<\/ul>/i', "\n", $mainContent);
$mainContent = preg_replace('/<ol[^>]*>/i', "\n", $mainContent);
$mainContent = preg_replace('/<\/ol>/i', "\n", $mainContent);
$mainContent = preg_replace('/<li[^>]*>(.*?)<\/li>/i', "- $1\n", $mainContent);
// 12. Odstranit všechny zbývajících HTML tagy
$mainContent = strip_tags($mainContent);
// 13. Čištění mezer
$mainContent = preg_replace('/\n{3,}/', "\n\n", $mainContent);
$mainContent = trim($mainContent);
// Získání titulku
$title = $slug;
if (preg_match('/<h1[^>]*>(.*?)<\/h1>/i', $content, $titleMatches)) {
$title = strip_tags($titleMatches[1]);
}
// Datum
$date = date('Y-m-d');
if (preg_match('/(\d{1,2}\.\d{1,2}\.\d{4})/', $content, $dateMatches)) {
$parts = explode('.', $dateMatches[1]);
if (count($parts) == 3) {
$date = "$parts[2]-$parts[1]-{$parts[0]}";
}
}
// Vytvořit Markdown obsah
$markdownContent = "# $title\n\n";
$markdownContent .= "> Datum: $date\n\n";
$markdownContent .= $mainContent;
// Uložit
$newFile = $newContentDir . $slug . '.md';
file_put_contents($newFile, $markdownContent);
echo "Importováno: $slug -> $newFile\n";
$importedCount++;
} else {
echo "Nenašel se <main> v: $basename\n";
}
}
echo "\nCelkem importováno článků: $importedCount\n";
echo "Nyní spusť: php build/generate_index.php\n";