support ext-discount and ext-cmark
authorMichael Wallner <mike@php.net>
Wed, 6 Mar 2019 15:12:50 +0000 (16:12 +0100)
committerMichael Wallner <mike@php.net>
Wed, 6 Mar 2019 15:15:33 +0000 (16:15 +0100)
unfortunately, cmark lacks a lot of features, though

mdref/Reference.php

index 892be7e096ab8da0f66e140bbd39dc4f2a53d8e4..3e40cfff7705d52c016018fedca332e47185562f 100644 (file)
@@ -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");
        }
 }