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 ($parts[$i][0] === ":") {
61 } elseif ($upper && !ctype_upper($parts[$i][0])) {
67 $link .= trim($parts[$i], ": ");
68 $upper = ctype_upper($parts[$i][0]);
75 * @param bool $basename whether to use the basename only
78 function formatLink($basename = false) {
80 if (strlen($this->entry
)) {
81 $parts = explode(DIRECTORY_SEPARATOR
, $this->entry
);
82 $link = $basename ?
end($parts) : $this->joinLink($parts);
84 return htmlspecialchars($link);
88 * Create a consolidated Path of this entry
93 $file = $path($this->entry
);
97 private function openFile() {
98 if (!is_resource($this->file
)) {
99 $file = $this->getPath();
101 if (!$file->isFile()) {
102 throw new \
Exception("Not a file: '{$file}'");
104 if (!$this->file
= fopen($file->getFullPath(".md"), "r")) {
105 throw new \
Exception("Could not open {$file}");
111 * Read the title of the refentry
114 function readTitle() {
116 fseek($this->file
, 1, SEEK_SET
);
117 return htmlspecialchars(fgets($this->file
));
121 * Read the description of the refentry
124 function readDescription() {
126 fseek($this->file
, 0, SEEK_SET
);
129 return htmlspecialchars(fgets($this->file
));
133 * Format a "Edit me" URL. The project reference top directory needs a
134 * »name«.mdref file besides its »name«.md entry point with the edit URL
135 * printf template as content. The sole printf argument is the relative
139 function formatEditUrl() {
141 $base = current(explode(DIRECTORY_SEPARATOR
, $path->getPathName()));
142 $file = $path($base);
143 if ($file->isFile(".mdref")) {
144 return sprintf(file_get_contents($file->getFullPath(".mdref")),
150 * Recurse into the reference tree
151 * @param \mdref\Finder $refs
152 * @param string $pattern
153 * @param callable $cb
155 function recurse(Finder
$refs, $pattern, callable
$cb) {
156 $path = $refs->find($refs->getBaseUrl()->mod($this->entry
));
157 foreach (new RefListing($path, $refs->glob($path, $pattern)) as $entry) {
158 /* @var $entry RefEntry */
159 $cb($entry, $pattern, function($entry, $pattern) use ($refs, $cb) {
160 $entry->recurse($refs, $pattern, $cb);