fix ignore refs
[mdref/mdref] / mdref / Reference.php
1 <?php
2
3 namespace mdref;
4
5 /**
6 * The complete available reference
7 */
8 class Reference implements \IteratorAggregate {
9 /**
10 * List of mdref repositories
11 * @var array
12 */
13 private $repos = array();
14
15 /**
16 * @param array $refs list of mdref repository paths
17 */
18 public function __construct(array $refs) {
19 foreach ($refs as $path) {
20 $repo = new Repo($path);
21 $this->repos[$repo->getName()] = $repo;
22 }
23 }
24
25 /**
26 * Lookup the repo containing a ref entry
27 * @param string $entry requested reference entry, e.g. "pq/Connection/exec"
28 * @param type $canonical
29 * @return \mdref\Repo|NULL
30 */
31 public function getRepoForEntry($entry, &$canonical = null) {
32 foreach ($this->repos as $repo) {
33 /** @var $repo Repo */
34 if ($repo->hasEntry($entry, $canonical)) {
35 return $repo;
36 }
37 }
38 }
39
40 /**
41 * Implements \IteratorAggregate
42 * @return \ArrayIterator repository list
43 */
44 public function getIterator() {
45 return new \ArrayIterator($this->repos);
46 }
47
48 public function formatAnchor($anchor) {
49 if (is_numeric($anchor)) {
50 return "L$anchor";
51 }
52 return preg_replace("/[^[:alnum:]\.:_]/", ".", $anchor);
53 }
54
55 public function formatString($string) {
56 if (extension_loaded("discount")) {
57 $md = \MarkdownDocument::createFromString($string);
58 $md->compile(\MarkdownDocument::AUTOLINK);
59 return $md->getHtml();
60 }
61 if (extension_loaded("cmark")) {
62 $node = \CommonMark\Parse($string);
63 return \CommonMark\Render\HTML($node);
64 }
65 throw new \Exception("No Markdown implementation found");
66 }
67
68 public function formatFile($file) {
69 if (extension_loaded("discount")) {
70 $fd = fopen($file, "r");
71 $md = \MarkdownDocument::createFromStream($fd);
72 $md->compile(\MarkdownDocument::AUTOLINK | \MarkdownDocument::TOC);
73 $html = $md->getHtml();
74 fclose($fd);
75 return $html;
76 }
77 if (extension_loaded("cmark")) {
78 $node = \CommonMark\Parse(file_get_contents($file));
79 return \CommonMark\Render\HTML($node);
80 }
81 throw new \Exception("No Markdown implementation found");
82 }
83 }