X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FReference.php;h=561bd1e2e8515f56890d7442deb2a95c23ce7c80;hb=ef5031e18d8497180ad6fc1bc5bc4ea59c64c9d5;hp=153004fdafc83dd75ea4e072fcad0c1afcc298a8;hpb=6478b415c59070f70ed860f3a592377eef9783b1;p=mdref%2Fmdref diff --git a/mdref/Reference.php b/mdref/Reference.php index 153004f..561bd1e 100644 --- a/mdref/Reference.php +++ b/mdref/Reference.php @@ -2,16 +2,22 @@ namespace mdref; +use ArrayIterator; +use Iterator; +use IteratorAggregate; +use function is_numeric; +use function preg_replace; + /** * The complete available reference */ -class Reference implements \IteratorAggregate { +class Reference implements IteratorAggregate { /** * List of mdref repositories - * @var array + * @var Repo[] */ private $repos = array(); - + /** * @param array $refs list of mdref repository paths */ @@ -21,27 +27,78 @@ 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" - * @param type $canonical + * @param string $canonical * @return \mdref\Repo|NULL */ - public function getRepoForEntry($entry, &$canonical = null) { + public function getRepoForEntry(string $entry, string &$canonical = null) : ?Repo { foreach ($this->repos as $repo) { + /** @var $repo Repo */ if ($repo->hasEntry($entry, $canonical)) { return $repo; } } + return null; } - + /** - * Implements \IteratorAggregate - * @return \ArrayIterator repository list + * Implements IteratorAggregate + * @return ArrayIterator repository list */ - public function getIterator() { - return new \ArrayIterator($this->repos); + public function getIterator() : Iterator { + return new ArrayIterator($this->repos); + } + + /** + * @param string $anchor + * @return string + */ + public function formatAnchor(string $anchor) : string { + if (is_numeric($anchor)) { + return "L$anchor"; + } + return preg_replace("/[^[:alnum:]\.:_]/", ".", $anchor); + } + + /** + * @param string $string + * @return string + * @throws \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"); + } + + /** + * @param string $file + * @return string + * @throws \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"); } - }