permalinks
[mdref/mdref] / mdref / File.php
1 <?php
2
3 namespace mdref;
4
5 /**
6 * A ref entry file
7 */
8 class File {
9 /**
10 * @var resource
11 */
12 private $fd;
13
14 /**
15 * Open the file
16 * @param string $path
17 */
18 public function __construct($path) {
19 $this->fd = fopen($path, "rb");
20 }
21
22 /**
23 * Read the title of the refentry
24 * @return string
25 */
26 public function readTitle() {
27 if (0 === fseek($this->fd, 1, SEEK_SET)) {
28 return fgets($this->fd);
29 }
30 }
31
32 /**
33 * Read the description of the refentry
34 * @return string
35 */
36 public function readDescription() {
37 if (0 === fseek($this->fd, 0, SEEK_SET)
38 && (false !== fgets($this->fd))
39 && (false !== fgets($this->fd))) {
40 return fgets($this->fd);
41 }
42 }
43
44 /**
45 * Read the first subsection of a global refentry
46 * @return string
47 */
48 public function readIntro() {
49 $intro = "";
50 if (0 === fseek($this->fd, 0, SEEK_SET)) {
51 $header = false;
52
53 while (!feof($this->fd)) {
54 if (false === ($line = fgets($this->fd))) {
55 break;
56 }
57 /* search first header and read until next header*/
58 if ("## " === substr($line, 0, 3)) {
59 if ($header) {
60 break;
61 } else {
62 $header = true;
63 continue;
64 }
65 }
66 if ($header) {
67 $intro .= $line;
68 }
69 }
70 }
71 return $intro;
72 }
73 }