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 * PHP
* gzip * gzip
* find * find
* pandoc
# Internal libraries used # Internal libraries used

View file

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