flush
[mdref/mdref] / index.php
1 <?php
2
3 error_reporting(E_ALL &~ E_DEPRECATED);
4
5 function cut(array $lines, array $specs) {
6 $delim = "[[:space:]]+";
7 $bytes = [];
8 $fields= [];
9
10 foreach ($specs as $spec => $value) {
11 switch ($spec) {
12 case "d":
13 $delim = $value;
14 break;
15 case "b":
16 $bytes = $value;
17 break;
18 case "f":
19 $fields = $value;
20 break;
21 }
22 }
23
24 $result = [];
25 if ($bytes) {
26 $func = "substr";
27 } else {
28 $func = function($a, $o = 0, $l = 0) {
29 return join(" ", array_slice($a, $o, $l ? $l+1 : count($a)-$o));
30 };
31 }
32 foreach ($lines as $line) {
33 if ($bytes) {
34 $spec = $bytes;
35 } else {
36 $line = split($delim, $line);
37 $spec = $fields;
38 }
39
40 if ($spec[0] == "-") {
41 $result[] = $func($line, 0, $spec[1]);
42 } elseif ($spec[1] == "-") {
43 if (empty($spec[2])) {
44 $result[] = $func($line, $spec[0]);
45 } else {
46 $result[] = $func($line, $spec[0], $spec[2]-$spec[0]);
47 }
48 } else {
49 $result[] = $line{$spec[0]};
50 }
51 }
52 return $result;
53 }
54
55 function head($file, $lines = 1) {
56 $ld = [];
57 if (($fd = fopen($file, "r"))) {
58 while ($lines--) {
59 $ld[] = fgets($fd);
60 }
61 }
62 return $ld;
63 }
64
65 function ns($file) {
66 return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
67 }
68
69 function urlpath($dir, $file) {
70 return (strlen($dir) ? $dir . "/" : "") . basename($file, ".md");
71 }
72
73 function ls($dir) {
74 $dir = rtrim(is_dir($dir) ? $dir : dirname($dir) ."/". basename($dir, ".md"), "/");
75 printf("<ul>\n");
76 printf("<li><a href=/>Home</a></li>\n");
77 if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
78 printf("<li><a href=/%s>%s</a></li>\n",
79 urlpath($dir, ".."),
80 ns($dn));
81 }
82 if (is_dir($dir)) {
83 if ($dir !== ".") {
84 printf("<li>%s</li>\n", ns($dir));
85 }
86 foreach (scandir($dir) as $file) {
87 /* ignore dot-files */
88 if ($file{0} === ".") {
89 continue;
90 }
91
92 $path = "$dir/$file";
93
94 if (is_file($path)) {
95 $pi = pathinfo($path);
96 /* ignore files not ending in .md */
97 if (!isset($pi["extension"]) || $pi["extension"] != "md") {
98 continue;
99 }
100 if (!ctype_upper($file{0}) && !is_dir("$dir/".$pi["filename"])) {
101 continue;
102 }
103 } else {
104 /* ignore directories where an companying file exists */
105 if (is_file("$path.md")) {
106 continue;
107 }
108 }
109
110 printf("<li><a href=\"/%s\">%s</a></li>\n",
111 urlpath($dir, $file),
112 ns("$dir/".basename($file, ".md")));
113 }
114 }
115
116 printf("</ul>\n");
117 }
118
119 function ml($file) {
120 $pi = pathinfo($file);
121 if (ctype_upper($pi["filename"][0])) {
122 printf("<h2>Methods:</h2>\n");
123 $dir = $pi["dirname"] . "/" . $pi["filename"];
124 if (is_dir($dir)) {
125 printf("<ul>\n");
126 foreach (scandir($dir) as $file) {
127 if (!is_file("$dir/$file") || ctype_upper($file{0})) {
128 continue;
129 }
130 printf("<li><h3>%s</h3><p>%s</p></li>\n",
131 basename($file, ".md"),
132 join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
133 );
134 }
135 printf("</ul>\n");
136 }
137 }
138 }
139
140 function md($file) {
141 $file = rtrim($file, "/");
142 if (is_file($file) || is_file($file .= ".md")) {
143 $r = fopen($file, "r");
144 $md = MarkdownDocument::createFromStream($r);
145 $md->compile();
146 echo $md->getHtml();
147 fclose($r);
148 ml($file);
149 } else {
150 printf("<h1>Quick Markdown Doc Browser</h1>\n");
151 printf("<p>v0.1.0</p>\n");
152 printf("<p>");
153 ob_start(function($s) {
154 return nl2br(htmlspecialchars($s));
155 });
156 readfile("LICENSE");
157 ob_end_flush();
158 printf("</p>\n");
159 }
160 }
161
162
163 function index($pn) {
164 ?>
165 <!doctype html>
166 <html>
167 <head>
168 <meta charset="utf-8">
169 <title><?=ns($pn)?></title>
170 <link rel="stylesheet" href="/index.css">
171 </head>
172 <body>
173 <div class="sidebar">
174 <?php ls($pn); ?>
175 </div>
176 <?php md($pn); ?>
177 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
178 <script src="/index.js"></script>
179 </body>
180 </html>
181 <?php
182 }
183
184 chdir($_SERVER["DOCUMENT_ROOT"]);
185 $t = ["css"=>"text/css", "js"=>"application/javascript"];
186 $r = new http\Env\Request;
187 $u = new http\Url($r->getRequestUrl());
188 $s = new http\Env\Response;
189
190 switch($u->path) {
191 case "/index.js":
192 case "/index.css":
193 $s->setHeader("Content-type", $t[pathinfo($u->path, PATHINFO_EXTENSION)]);
194 $s->setBody(new http\Message\Body(fopen(basename($u->path), "r")));
195 $s->send();
196 exit;
197 default:
198 ob_start($s);
199 index(".".$u->path);
200 ob_end_flush();
201 $s->send();
202 break;
203 }
204
205 ?>