use the response
[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 (is_resource($file) || ($file = fopen($file, "r"))) {
58 while ($lines--) {
59 $ld[] = fgets($file);
60 }
61 }
62 return $ld;
63 }
64
65 function ns($path) {
66 $ns = "";
67 $parts = explode("/", $path);
68 $upper = ctype_upper($path[0]);
69 for ($i = 0; $i < count($parts); ++$i) {
70 if (!strlen($parts[$i]) || $parts[$i] === ".") {
71 continue;
72 }
73 if (strlen($ns)) {
74 if ($upper && !ctype_upper($parts[$i][0])) {
75 $ns .= "::";
76 } else {
77 $ns .= "\\";
78 }
79 }
80 $ns .= $parts[$i];
81 $upper = ctype_upper($parts[$i][0]);
82 }
83 return $ns;
84 return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
85 }
86
87 function urlpath($dir, $file) {
88 return (strlen($dir) ? $dir . "/" : "") . basename($file, ".md");
89 }
90
91 function ls($dir) {
92 $dir = rtrim(is_dir($dir) ? $dir : dirname($dir) ."/". basename($dir, ".md"), "/");
93 printf("<ul>\n");
94 printf("<li>&lArr; <a href=/>Home</a></li>\n");
95 if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
96 printf("<li>&uArr; <a href=/%s>%s</a></li>\n",
97 urlpath($dir, ".."),
98 ns($dn));
99 }
100 if (is_dir($dir)) {
101 if ($dir !== ".") {
102 printf("<ul>\n<li>&nbsp; %s</li>\n", ns($dir));
103 }
104 printf("<ul>\n");
105 foreach (scandir($dir) as $file) {
106 /* ignore dot-files */
107 if ($file{0} === ".") {
108 continue;
109 }
110
111 $path = "$dir/$file";
112
113 if (is_file($path)) {
114 $pi = pathinfo($path);
115 /* ignore files not ending in .md */
116 if (!isset($pi["extension"]) || $pi["extension"] != "md") {
117 continue;
118 }
119 /* ignore files where an accompanying directory exists */
120 if (is_dir("$dir/".$pi["filename"])) {
121 continue;
122 }
123 } else {
124 /* ignore directories where no accompanying file exists */
125 if (!is_file("$path.md")) {
126 continue;
127 }
128 }
129
130 printf("<li>&rArr; <a href=\"/%s\">%s</a></li>\n",
131 urlpath($dir, $file),
132 ns("$dir/".basename($file, ".md")));
133 }
134 printf("</ul>\n");
135 if ($dir !== ".") {
136 printf("</ul>\n");
137 }
138 }
139
140 printf("</ul>\n");
141 }
142
143 function ml($file) {
144 $pi = pathinfo($file);
145 if (!isset($pi["extension"])) {
146 return;
147 }
148 if ($pi["extension"] !== "md") {
149 return;
150 }
151 if (!ctype_upper($pi["filename"][0])) {
152 // namespaced functions
153 $dir = $pi["dirname"] . "/" . $pi["filename"];
154 if (is_dir($dir)) {
155 printf("<h2>Functions:</h2>\n");
156 printf("<ul>\n");
157 foreach (scandir($dir) as $file) {
158 if ($file{0} === "." || !is_file("$dir/$file") || ctype_upper($file{0})) {
159 continue;
160 }
161 printf("<li><h3><a href=\"/%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
162 urlpath($dir, $file),
163 basename($file, ".md"),
164 @end(head("$dir/$file", 3)),
165 join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
166 );
167 }
168 printf("</ul>\n");
169 }
170 } else {
171 // methods
172 $dir = $pi["dirname"] . "/" . $pi["filename"];
173 if (is_dir($dir)) {
174 printf("<h2>Methods:</h2>\n");
175 printf("<ul>\n");
176 foreach (scandir($dir) as $file) {
177 if ($file{0} === "." || !is_file("$dir/$file") || ctype_upper($file{0})) {
178 continue;
179 }
180 printf("<li><h3><a href=\"/%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
181 urlpath($dir, $file),
182 basename($file, ".md"),
183 @end(head("$dir/$file", 3)),
184 join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
185 );
186 }
187 printf("</ul>\n");
188 }
189 }
190 }
191
192 function md($file, $res) {
193 $file = rtrim($file, "/");
194 if (is_file($file) || is_file($file .= ".md")) {
195 $pi = pathinfo($file);
196
197 switch (@$pi["extension"]) {
198 case "md":
199 $r = fopen($file, "r");
200 $md = MarkdownDocument::createFromStream($r);
201 $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
202 print str_replace("<br/>","<br />",$md->getHtml());
203 fclose($r);
204 ml($file);
205 break;
206 case null:
207 printf("<h1>%s</h1>", basename($file));
208 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
209 break;
210 }
211 } else {
212 $res->setResponseCode(404);
213 printf("<h1>Not Found</h1>\n");
214 printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
215 }
216 }
217
218 function index($pn) {
219 ?>
220 <?php
221 }
222
223 chdir($_SERVER["DOCUMENT_ROOT"]);
224 $t = ["css"=>"text/css", "js"=>"application/javascript"];
225 $r = new http\Env\Request;
226 $u = new http\Url($r->getRequestUrl());
227 $s = new http\Env\Response;
228 $p = ".". $u->path;
229
230 switch($p) {
231 case "./index.php":
232 exit;
233 case "./index.js":
234 case "./index.css":
235 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
236 $s->setBody(new http\Message\Body(fopen($p, "r")));
237 $s->send();
238 exit;
239 }
240
241 ob_start($s);
242
243 ?>
244 <!doctype html>
245 <html>
246 <head>
247 <meta charset="utf-8">
248 <title><?=ns($p)?></title>
249 <link rel="stylesheet" href="/index.css">
250 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
251 <?php if (!extension_loaded("discount") || !getenv("DISCOUNT")) : ?>
252 <script src="/markdown.js"></script>
253 <?php endif; ?>
254 </head>
255 <body>
256 <div class="sidebar">
257 <?php ls($p); ?>
258 </div>
259 <?php if ($p === "./") : ?>
260 <h1>Quick Markdown Documentation Browser</h1>
261 <p>v<?php readfile("VERSION")?></p>
262 <pre><?php
263 ob_start(function($s) {
264 return htmlspecialchars($s);
265 });
266 readfile("LICENSE");
267 ob_end_flush();
268 ?></pre>
269 <?php else: ?>
270 <?php if (!md($p, $s)): ?>
271 <?php endif; ?>
272 <?php endif; ?>
273 <footer>
274 <a href="/VERSION">Version</a>
275 <a href="/AUTHORS">Authors</a>
276 <a href="/LICENSE">License</a>
277 </footer>
278 <script src="/index.js"></script>
279 </body>
280 </html>
281 <?php
282
283 ob_end_flush();
284 $s->send();