Use pandoc instead of parsedown by default

This commit is contained in:
Miraty 2023-05-24 18:58:56 +02:00
parent 396f83fa9f
commit 6d4778ffba
2 changed files with 26 additions and 8 deletions

View file

@ -35,6 +35,7 @@ Gemini > Markdown > HTML, which means that the last of these formats you will us
* PHP
* gzip
* find
* pandoc
# Internal libraries used

View file

@ -9,6 +9,8 @@ const LF = "\n";
define('ROOT', dirname($_SERVER['SCRIPT_FILENAME']));
$use_pandoc = true;
if (isset($argv[1]))
define('SITE', $argv[1]);
else
@ -112,19 +114,34 @@ foreach ($pages as $page) {
file_put_contents($pathParts['dirname'] . '/' . $pathParts['filename'] . '.md', $code);
}
// Compile Markdown to HTML with Parsedown
// Compile Markdown to HTML
$markdown = file_get_contents($pathParts['dirname'] . '/' . $pathParts['filename'] . '.md');
if (preg_match("/# (.*)\\n/", $markdown, $matches)) // If a main heading is found
$title = $matches[1]; // Then it will be the HTML page <title>
else
$title = NULL;
require_once ROOT . '/parsedown/Parsedown.php';
require_once ROOT . '/parsedown-extra/ParsedownExtra.php';
$Parsedown = new ParsedownExtra;
$Parsedown = $Parsedown->setUrlsLinked(false);
$Parsedown = $Parsedown->setMarkupEscaped(false);
$Parsedown = $Parsedown->setBreaksEnabled(true);
$pageContent = $Parsedown->text($markdown);
if ($use_pandoc) {
$process = proc_open('pandoc --fail-if-warnings -f markdown -t html', [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
], $pipes);
if (is_resource($process) !== true)
exit('Can\'t spawn pandoc.');
fwrite($pipes[0], $markdown);
fclose($pipes[0]);
$pageContent = fread($pipes[1], 1000);
fclose($pipes[1]);
if (proc_close($process) !== 0)
exit('pandoc failed.');
} else {
require_once ROOT . '/parsedown/Parsedown.php';
require_once ROOT . '/parsedown-extra/ParsedownExtra.php';
$Parsedown = new ParsedownExtra;
$Parsedown = $Parsedown->setUrlsLinked(false);
$Parsedown = $Parsedown->setMarkupEscaped(false);
$Parsedown = $Parsedown->setBreaksEnabled(true);
$pageContent = $Parsedown->text($markdown);
}
// .md > .html for local links
$pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent);