Fix regex for Gemini>Markdown link conversion

This commit is contained in:
Miraty 2023-06-01 18:13:20 +02:00
parent 1cb79416c2
commit d6f8e7b84e

View file

@ -82,18 +82,16 @@ foreach ($config['base-url'] as $url)
$feed = ob_get_clean();
foreach ($files_dates as $src_page => $last_mod) {
$dest_page = str_replace('/src/', '/', $src_page);
$content = file_get_contents($src_page);
preg_match('/^# ?(?<title>.*)$/Dm', $content, $matches);
$title = $matches['title'] ?? NULL;
$path_parts = pathinfo($dest_page);
$path_parts = pathinfo(str_replace('/src/', '/', $src_page));
$base_filepath = $path_parts['dirname'] . '/' . $path_parts['filename'];
if (!file_exists($dest_page) OR (filemtime($src_page) > filemtime($dest_page)) OR $opt['force']) {
if (!file_exists($base_filepath . '.html') OR (filemtime($src_page) > filemtime($base_filepath . '.html')) OR $opt['force']) {
echo 'Compiling ' . $src_page . ' ' . date("Y-m-d H:i:s", $last_mod) . LF;
// Create parent directory if needed
@ -109,7 +107,7 @@ foreach ($files_dates as $src_page => $last_mod) {
// Convert Gemtext to Markdown
if ($path_parts['extension'] === 'gmi') {
$content = preg_replace_callback(
'/^=>\h*(?<addr>\H+)(:?\h+(?<title>\H+))?$/Dm',
'/^=>\h*(?<addr>\S+)(:?\h+(?<title>\V+))?$/m',
function ($matches) {
if (!str_contains($matches['addr'], ':') AND str_ends_with($matches['addr'], '.gmi'))
$matches['addr'] = substr($matches['addr'], 0, -3) . 'md';
@ -121,7 +119,7 @@ foreach ($files_dates as $src_page => $last_mod) {
}
// Compile Markdown to HTML
$process = proc_open('pandoc --fail-if-warnings -f markdown_phpextra-citations-native_divs-native_spans+abbreviations+hard_line_breaks+lists_without_preceding_blankline -t html --wrap none', [
$process = proc_open('pandoc --fail-if-warnings -f markdown_phpextra-citations-native_divs-native_spans+abbreviations+hard_line_breaks+lists_without_preceding_blankline -t html --wrap none', [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
], $pipes);