Fix path parsing with trailing slash

This commit is contained in:
Miraty 2023-11-14 17:42:30 +01:00
parent 5c31f0798e
commit cad0ff0c13
1 changed files with 19 additions and 20 deletions

View File

@ -24,12 +24,13 @@ foreach ($argv as $arg) {
}
$opt['force'] ??= false;
define('SITE', $args[1] ?? getcwd());
$site = $args[1] ?? getcwd();
define('SITE', str_ends_with($site, '/') ? $site : $site . '/');
define('DESTINATION', $args[2] ?? 'dns');
if (file_exists(SITE . '/config.ini'))
$config = parse_ini_file(SITE . '/config.ini');
if (file_exists(SITE . 'config.ini'))
$config = parse_ini_file(SITE . 'config.ini');
$config['header'] ??= false;
$config['author'] ??= NULL;
@ -41,7 +42,7 @@ $config['announce-feed'] ??= false;
if (!isset($config['id'])) {
$config['id'] = bin2hex(random_bytes(32));
file_put_contents(SITE . '/config.ini', 'id = "' . $config['id'] . '"' . LF, FILE_APPEND);
file_put_contents(SITE . 'config.ini', 'id = "' . $config['id'] . '"' . LF, FILE_APPEND);
}
// Determine whether links need to use Onion or DNS
@ -57,9 +58,10 @@ $nodes = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SITE, Recu
foreach($nodes as $node) {
$node_info = new SplFileInfo($node->getPathName());
$src = $node_info->getPathname();
if (str_starts_with($src, SITE . '/target'))
if (str_starts_with($src, SITE . 'target/'))
continue;
$target = str_replace(SITE, SITE . '/target', $src);
$target = str_replace(SITE, SITE . 'target/', $src);
$path_parts_src = pathinfo($src);
$path_parts_target = pathinfo($target);
if (strstr($src, '/.') !== false AND !str_ends_with($src, '/.htaccess')) // Skip hidden nodes (except for .htaccess)
@ -90,7 +92,7 @@ foreach($nodes as $node) {
foreach ($pages as $node) {
$node_info = new SplFileInfo($node->getPathName());
$src = $node_info->getPathname();
$target = str_replace(SITE, SITE . '/target', $src);
$target = str_replace(SITE, SITE . 'target/', $src);
$path_parts_src = pathinfo($src);
$path_parts_target = pathinfo($target);
@ -141,7 +143,7 @@ foreach ($pages as $node) {
// .md > .html for local links
$content = preg_replace('/ href="([^:"]+)\.md"/', ' href="$1.html"', $content);
$relative_root_path = str_repeat('../', substr_count(str_replace(SITE . '/target', '', $path_parts_target['dirname']), '/'));
$relative_root_path = str_repeat('../', substr_count(str_replace(SITE . 'target', '', $path_parts_target['dirname']), '/'));
ob_start();
@ -170,13 +172,13 @@ foreach ($pages as $node) {
echo ' <link rel="alternate" type="application/atom+xml" href="' . $relative_root_path . 'feed.atom">' . LF;
if ($config['announce-css']) {
if (file_exists(SITE . '/style.css'))
if (file_exists(SITE . 'style.css'))
echo ' <link rel="stylesheet" media="screen" href="' . $relative_root_path . 'style.css">' . LF;
echo ' <link rel="stylesheet" media="screen" href="' . $relative_root_path . 'mkht-php.css">' . LF;
}
if (file_exists(SITE . '/head.inc.html'))
echo file_get_contents(SITE . '/head.inc.html');
if (file_exists(SITE . 'head.inc.html'))
echo file_get_contents(SITE . 'head.inc.html');
?>
</head>
@ -209,15 +211,15 @@ foreach ($pages as $node) {
<?php
}
if (file_exists(SITE . '/header.inc.php'))
eval('?>' . file_get_contents(SITE . '/header.inc.php'));
if (file_exists(SITE . 'header.inc.php'))
eval('?>' . file_get_contents(SITE . 'header.inc.php'));
if ($config['center-index'] AND $path_parts_target['filename'] === 'index')
echo '<div class="centered">' . $content . '</div>';
else
echo '<main>' . $content . '</main>';
if (file_exists(SITE . '/end.inc.html'))
require SITE . '/end.inc.html';
if (file_exists(SITE . 'end.inc.html'))
require SITE . 'end.inc.html';
echo '</body></html>';
$content = ob_get_clean();
@ -273,9 +275,6 @@ foreach ($pages as $node) {
$atom_entry = ob_get_clean();
foreach (explode('/', $relative_addr) as $level => $unused) {
if ($level === 0)
continue;
$dir = implode('/', array_slice(explode('/', $relative_addr), 0, $level)) . '/';
$last_update = $node_info->getMTime();
@ -306,9 +305,9 @@ foreach ($dirs_entries_per_lang as $lang => $dirs_entries) {
<name><?= $config['author'] ?? '' ?></name>
</author>
<?php
file_put_contents(SITE . '/target' . $dir . 'feed.' . $lang . '.atom', ob_get_clean() . $entries . '</feed>' . LF);
file_put_contents(SITE . 'target/' . $dir . 'feed.' . $lang . '.atom', ob_get_clean() . $entries . '</feed>' . LF);
}
}
if ($config['announce-css'])
copy(ROOT . '/style.css', SITE . '/target/mkht-php.css');
copy(ROOT . '/style.css', SITE . 'target/mkht-php.css');