diff --git a/mkht.php b/mkht.php index 5f2e757..6c978c2 100755 --- a/mkht.php +++ b/mkht.php @@ -104,27 +104,19 @@ foreach ($files_dates as $src_page => $last_mod) { ob_start(); eval('?>' . $content); $content = ob_get_clean(); - file_put_contents($base_filepath . '.gmi', $content); + file_put_contents($base_filepath . '.' . $path_parts['extension'], $content); // Convert Gemtext to Markdown if ($path_parts['extension'] === 'gmi') { - $gmilines = explode(LF, $content); - - foreach ($gmilines as $key => $line) { - if (str_starts_with($line, '=>')) { - preg_match('/=> +(.[^ ]+)/', $line, $lnUrl); - preg_match('/=> +.[^ ]+ +(.+)/', $line, $lnTitle); - - $urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH)); - - // .gmi > .md for local links - if (!str_contains($lnUrl[1], ':') AND $urlPathParts['extension'] === 'gmi') // If it's a local link - $lnUrl[1] = $urlPathParts['dirname'] . '/' . $urlPathParts['filename'] . '.md'; - - $gmilines[$key] = '[' . ($lnTitle[1] ?? $lnUrl[1]) . '](' . $lnUrl[1] . ')'; - } - } - $content = implode(LF, $gmilines); + $content = preg_replace_callback( + '/^=>\h*(?\H+)(:?\h+(?\H+))?$/Dm', + function ($matches) { + if (!str_contains($matches['addr'], ':') AND str_ends_with($matches['addr'], '.gmi')) + $matches['addr'] = substr($matches['addr'], 0, -3) . 'md'; + return '[' . ($matches['title'] ?? $matches['addr']) . '](' . $matches['addr'] . ')'; + }, + $content, + ); file_put_contents($base_filepath . '.md', $content); } @@ -143,11 +135,9 @@ foreach ($files_dates as $src_page => $last_mod) { exit('pandoc failed.' . PHP_EOL); // .md > .html for local links - $content = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $content); + $content = preg_replace('/ href="([^:"]+)\.md"/', ' href="$1.html"', $content); - $relativePathToRoot = ''; - for ($i = substr_count(str_replace(SITE, '', $path_parts['dirname']), '/') ; $i > 0 ; $i--) - $relativePathToRoot .= '../'; + $relative_root_path = str_repeat('../', substr_count(str_replace(SITE, '', $path_parts['dirname']), '/')); ob_start(); @@ -182,12 +172,12 @@ foreach ($files_dates as $src_page => $last_mod) { echo '<meta name="author" content="' . $config['author'] . '">'; if ($config['announce-feed']) - echo '<link rel="alternate" type="application/atom+xml" href="' . $relativePathToRoot . 'feed.atom">' . LF; + 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')) - echo '<link rel="stylesheet" media="screen" href="' . $relativePathToRoot . 'style.css">' . LF; - echo '<link rel="stylesheet" media="screen" href="' . $relativePathToRoot . 'mkht-php.css">' . LF; + 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')) @@ -200,7 +190,7 @@ foreach ($files_dates as $src_page => $last_mod) { if ($config['header']) { ?> <header> - <a href="./<?= $relativePathToRoot ?>"> + <a href="./<?= $relative_root_path ?>"> <?php if (file_exists(SITE . '/img/logo.webp')) echo '<img src="img/logo.webp" ' . getimagesize(SITE . '/img/logo.webp')[3] . ' alt="' . $config['title'] . '" />';