X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FReference.php;h=3e40cfff7705d52c016018fedca332e47185562f;hb=30ba41f956001432d6f07a23794d8e34248b3f7c;hp=7c22dba37fee16e9b9b4cfd7b90746b892ced4fb;hpb=c5efaeee1075614b4f8cc1ce373df1adcc4ee9fb;p=mdref%2Fmdref diff --git a/mdref/Reference.php b/mdref/Reference.php index 7c22dba..3e40cff 100644 --- a/mdref/Reference.php +++ b/mdref/Reference.php @@ -11,7 +11,7 @@ class Reference implements \IteratorAggregate { * @var array */ private $repos = array(); - + /** * @param array $refs list of mdref repository paths */ @@ -21,7 +21,7 @@ class Reference implements \IteratorAggregate { $this->repos[$repo->getName()] = $repo; } } - + /** * Lookup the repo containing a ref entry * @param string $entry requested reference entry, e.g. "pq/Connection/exec" @@ -30,12 +30,13 @@ 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; } } } - + /** * Implements \IteratorAggregate * @return \ArrayIterator repository list @@ -44,18 +45,39 @@ class Reference implements \IteratorAggregate { return new \ArrayIterator($this->repos); } + public function formatAnchor($anchor) { + if (is_numeric($anchor)) { + return "L$anchor"; + } + return preg_replace("/[^[:alnum:]\.:_]/", ".", $anchor); + } + 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"); } }