From: Michael Wallner Date: Wed, 6 Mar 2019 15:12:50 +0000 (+0100) Subject: support ext-discount and ext-cmark X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=d942f60dc24887e48991d599281834a752dd4c0a;p=mdref%2Fmdref support ext-discount and ext-cmark unfortunately, cmark lacks a lot of features, though --- diff --git a/mdref/Reference.php b/mdref/Reference.php index 892be7e..3e40cff 100644 --- a/mdref/Reference.php +++ b/mdref/Reference.php @@ -30,6 +30,7 @@ class Reference implements \IteratorAggregate { */ public function getRepoForEntry($entry, &$canonical = null) { foreach ($this->repos as $repo) { + /** @var $repo Repo */ if ($repo->hasEntry($entry, $canonical)) { return $repo; } @@ -52,17 +53,31 @@ class Reference implements \IteratorAggregate { } public function formatString($string) { - $md = \MarkdownDocument::createFromString($string); - $md->compile(\MarkdownDocument::AUTOLINK); - return $md->getHtml(); + if (extension_loaded("discount")) { + $md = \MarkdownDocument::createFromString($string); + $md->compile(\MarkdownDocument::AUTOLINK); + return $md->getHtml(); + } + if (extension_loaded("cmark")) { + $node = \CommonMark\Parse($string); + return \CommonMark\Render\HTML($node); + } + throw new \Exception("No Markdown implementation found"); } public function formatFile($file) { - $fd = fopen($file, "r"); - $md = \MarkdownDocument::createFromStream($fd); - $md->compile(\MarkdownDocument::AUTOLINK | \MarkdownDocument::TOC); - $html = $md->getHtml(); - fclose($fd); - return $html; + if (extension_loaded("discount")) { + $fd = fopen($file, "r"); + $md = \MarkdownDocument::createFromStream($fd); + $md->compile(\MarkdownDocument::AUTOLINK | \MarkdownDocument::TOC); + $html = $md->getHtml(); + fclose($fd); + return $html; + } + if (extension_loaded("cmark")) { + $node = \CommonMark\Parse(file_get_contents($file)); + return \CommonMark\Render\HTML($node); + } + throw new \Exception("No Markdown implementation found"); } }