X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FReference.php;h=6e3b27e29d53de3a31f3941c92db007beb5f3711;hb=5049ac5407240b679b54f26dcdcb6dca4e478217;hp=7c22dba37fee16e9b9b4cfd7b90746b892ced4fb;hpb=c5efaeee1075614b4f8cc1ce373df1adcc4ee9fb;p=mdref%2Fmdref diff --git a/mdref/Reference.php b/mdref/Reference.php index 7c22dba..6e3b27e 100644 --- a/mdref/Reference.php +++ b/mdref/Reference.php @@ -2,60 +2,88 @@ 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(); - + + /** + * @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 ?: Formatter::factory(); } - + /** * 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); } - public function formatString($string) { - $md = \MarkdownDocument::createFromString($string); - $md->compile(\MarkdownDocument::AUTOLINK); - return $md->getHtml(); + /** + * @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, Exception + */ + public function formatString(string $string) : string { + return $this->fmt->formatString($string); } - - 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; + + /** + * @param string $file + * @return string + * @throws \Exception, Exception + */ + public function formatFile(string $file) : string { + return $this->fmt->formatFile($file); } }