6 * The RefEntry class represents a reference entry, i.e. a .md file
26 * @param \mdref\Path $path
29 function __construct(Path
$path, $entry = null) {
31 $this->entry
= trim($entry ?
: $path->getPathName(), DIRECTORY_SEPARATOR
);
35 * Clean up the file handle
37 function __destruct() {
38 if (is_resource($this->file
)) {
47 function formatUrl() {
48 return htmlspecialchars($this->entry
);
51 private function joinLink(array $parts) {
53 $upper = ctype_upper($parts[0][0]);;
54 for ($i = 0; $i < count($parts); ++
$i) {
55 if (!strlen($parts[$i]) ||
$parts[$i] === ".") {
59 if ($upper && !ctype_upper($parts[$i][0])) {
66 $upper = ctype_upper($parts[$i][0]);
73 * @param bool $basename whether to use the basename only
76 function formatLink($basename = false) {
78 if (strlen($this->entry
)) {
79 $parts = explode(DIRECTORY_SEPARATOR
, $this->entry
);
80 $link = $basename ?
end($parts) : $this->joinLink($parts);
82 return htmlspecialchars($link);
86 * Create a consolidated Path of this entry
91 $file = $path($this->entry
);
95 private function openFile() {
96 if (!is_resource($this->file
)) {
97 $file = $this->getPath();
99 if (!$file->isFile()) {
100 throw new \
Exception("Not a file: '{$file}'");
102 if (!$this->file
= fopen($file->getFullPath(".md"), "r")) {
103 throw new \
Exception("Could not open {$file}");
109 * Read the title of the refentry
112 function readTitle() {
114 fseek($this->file
, 1, SEEK_SET
);
115 return htmlspecialchars(fgets($this->file
));
119 * Read the description of the refentry
122 function readDescription() {
124 fseek($this->file
, 0, SEEK_SET
);
127 return htmlspecialchars(fgets($this->file
));
131 * Format a "Edit me" URL. The project reference top directory needs a
132 * »name«.mdref file besides its »name«.md entry point with the edit URL
133 * printf template as content. The sole printf argument is the relative
137 function formatEditUrl() {
139 $base = current(explode(DIRECTORY_SEPARATOR
, $path->getPathName()));
140 $file = $path($base);
141 if ($file->isFile(".mdref")) {
142 return sprintf(file_get_contents($file->getFullPath(".mdref")),
148 * Recurse into the reference tree
149 * @param \mdref\Finder $refs
150 * @param string $pattern
151 * @param callable $cb
153 function recurse(Finder
$refs, $pattern, callable
$cb) {
154 $path = $refs->find($refs->getBaseUrl()->mod($this->entry
));
155 foreach (new RefListing($path, $refs->glob($path, $pattern)) as $entry) {
156 /* @var $entry RefEntry */
157 $cb($entry, $pattern, function($entry, $pattern) use ($refs, $cb) {
158 $entry->recurse($refs, $pattern, $cb);