Simplify Gemtext>Markdown conversion

This commit is contained in:
Miraty 2023-05-31 23:12:02 +02:00
parent 350b74c24e
commit 1cb79416c2

View file

@ -104,27 +104,19 @@ foreach ($files_dates as $src_page => $last_mod) {
ob_start(); ob_start();
eval('?>' . $content); eval('?>' . $content);
$content = ob_get_clean(); $content = ob_get_clean();
file_put_contents($base_filepath . '.gmi', $content); file_put_contents($base_filepath . '.' . $path_parts['extension'], $content);
// Convert Gemtext to Markdown // Convert Gemtext to Markdown
if ($path_parts['extension'] === 'gmi') { if ($path_parts['extension'] === 'gmi') {
$gmilines = explode(LF, $content); $content = preg_replace_callback(
'/^=>\h*(?<addr>\H+)(:?\h+(?<title>\H+))?$/Dm',
foreach ($gmilines as $key => $line) { function ($matches) {
if (str_starts_with($line, '=>')) { if (!str_contains($matches['addr'], ':') AND str_ends_with($matches['addr'], '.gmi'))
preg_match('/=> +(.[^ ]+)/', $line, $lnUrl); $matches['addr'] = substr($matches['addr'], 0, -3) . 'md';
preg_match('/=> +.[^ ]+ +(.+)/', $line, $lnTitle); return '[' . ($matches['title'] ?? $matches['addr']) . '](' . $matches['addr'] . ')';
},
$urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH)); $content,
);
// .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);
file_put_contents($base_filepath . '.md', $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); exit('pandoc failed.' . PHP_EOL);
// .md > .html for local links // .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 = ''; $relative_root_path = str_repeat('../', substr_count(str_replace(SITE, '', $path_parts['dirname']), '/'));
for ($i = substr_count(str_replace(SITE, '', $path_parts['dirname']), '/') ; $i > 0 ; $i--)
$relativePathToRoot .= '../';
ob_start(); ob_start();
@ -182,12 +172,12 @@ foreach ($files_dates as $src_page => $last_mod) {
echo '<meta name="author" content="' . $config['author'] . '">'; echo '<meta name="author" content="' . $config['author'] . '">';
if ($config['announce-feed']) 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 ($config['announce-css']) {
if (file_exists(SITE . '/style.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="' . $relative_root_path . '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 . 'mkht-php.css">' . LF;
} }
if (file_exists(SITE . '/head.inc.html')) if (file_exists(SITE . '/head.inc.html'))
@ -200,7 +190,7 @@ foreach ($files_dates as $src_page => $last_mod) {
if ($config['header']) { if ($config['header']) {
?> ?>
<header> <header>
<a href="./<?= $relativePathToRoot ?>"> <a href="./<?= $relative_root_path ?>">
<?php <?php
if (file_exists(SITE . '/img/logo.webp')) if (file_exists(SITE . '/img/logo.webp'))
echo '<img src="img/logo.webp" ' . getimagesize(SITE . '/img/logo.webp')[3] . ' alt="' . $config['title'] . '" />'; echo '<img src="img/logo.webp" ' . getimagesize(SITE . '/img/logo.webp')[3] . ' alt="' . $config['title'] . '" />';