8 use function is_numeric
;
9 use function preg_replace
;
12 * The complete available reference
14 class Reference
implements IteratorAggregate
{
16 * List of mdref repositories
19 private $repos = array();
22 * @param array $refs list of mdref repository paths
24 public function __construct(array $refs) {
25 foreach ($refs as $path) {
26 $repo = new Repo($path);
27 $this->repos
[$repo->getName()] = $repo;
32 * Lookup the repo containing a ref entry
33 * @param string $entry requested reference entry, e.g. "pq/Connection/exec"
34 * @param string $canonical
35 * @return \mdref\Repo|NULL
37 public function getRepoForEntry(string $entry, string &$canonical = null) : ?Repo
{
38 foreach ($this->repos
as $repo) {
39 /** @var $repo Repo */
40 if ($repo->hasEntry($entry, $canonical)) {
48 * Implements IteratorAggregate
49 * @return ArrayIterator repository list
51 public function getIterator() : Iterator
{
52 return new ArrayIterator($this->repos
);
56 * @param string $anchor
59 public function formatAnchor(string $anchor) : string {
60 if (is_numeric($anchor)) {
63 return preg_replace("/[^[:alnum:]\.:_]/", ".", $anchor);
67 * @param string $string
71 public function formatString(string $string) : string {
72 if (extension_loaded("discount")) {
73 $md = \MarkdownDocument
::createFromString($string);
74 $md->compile(\MarkdownDocument
::AUTOLINK
);
75 return $md->getHtml();
77 if (extension_loaded("cmark")) {
78 $node = \CommonMark\
Parse($string);
79 return \CommonMark\Render\
HTML($node);
81 throw new \
Exception("No Markdown implementation found");
89 public function formatFile(string $file) : string {
90 if (extension_loaded("discount")) {
91 $fd = fopen($file, "r");
92 $md = \MarkdownDocument
::createFromStream($fd);
93 $md->compile(\MarkdownDocument
::AUTOLINK | \MarkdownDocument
::TOC
);
94 $html = $md->getHtml();
98 if (extension_loaded("cmark")) {
99 $node = \CommonMark\
Parse(file_get_contents($file));
100 return \CommonMark\Render\
HTML($node);
102 throw new \
Exception("No Markdown implementation found");