fix wrapper->fmt calls
[mdref/mdref] / mdref / Reference.php
index 561bd1e2e8515f56890d7442deb2a95c23ce7c80..4197de1afa015c8aa6d14beefd81af125018ec69 100644 (file)
@@ -18,14 +18,28 @@ class Reference implements IteratorAggregate {
         */
        private $repos = array();
 
+       /**
+        * @var Formatter
+        */
+       private $fmt;
+
        /**
         * @param array $refs list of mdref repository paths
         */
-       public function __construct(array $refs) {
+       public function __construct(array $refs, Formatter $fmt = null) {
                foreach ($refs as $path) {
                        $repo = new Repo($path);
                        $this->repos[$repo->getName()] = $repo;
                }
+               $this->fmt = $fmt ?: new Formatter;
+       }
+
+       /**
+        * Get the formatter.
+        * @return Formatter
+        */
+       public function getFormatter() : Formatter {
+               return $this->fmt;
        }
 
        /**
@@ -56,7 +70,7 @@ class Reference implements IteratorAggregate {
         * @param string $anchor
         * @return string
         */
-       public function formatAnchor(string $anchor) : string {
+       public function formatAnchor(string $anchor, string $location = null) : string {
                if (is_numeric($anchor)) {
                        return "L$anchor";
                }
@@ -66,39 +80,18 @@ class Reference implements IteratorAggregate {
        /**
         * @param string $string
         * @return string
-        * @throws \Exception
+        * @throws \Exception, Exception
         */
-       public function formatString(string $string) : string {
-               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 formatString(string $string, string $location = null) : string {
+               return $this->fmt->formatString($string, $location);
        }
 
        /**
         * @param string $file
         * @return string
-        * @throws \Exception
+        * @throws \Exception, Exception
         */
-       public function formatFile(string $file) : string {
-               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");
+       public function formatFile(string $file, string $location = null) : string {
+               return $this->fmt->formatFile($file, $location);
        }
 }