From 6d4778ffba88250de6ff22c93b5a97dde59c2316 Mon Sep 17 00:00:00 2001 From: Miraty Date: Wed, 24 May 2023 18:58:56 +0200 Subject: [PATCH] Use pandoc instead of parsedown by default --- README.md | 1 + mkht.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ad153d3..62b4f04 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/mkht.php b/mkht.php index 744fb8c..acc7552 100755 --- a/mkht.php +++ b/mkht.php @@ -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 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);