github edit link
[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 chdir(__DIR__);
219 $t = ["css"=>"text/css", "js"=>"application/javascript"];
220 $r = new http\Env\Request;
221 $u = new http\Url($r->getRequestUrl());
222 $s = new http\Env\Response;
223 $b = dirname($_SERVER["SCRIPT_NAME"]);
224 $p = ".". substr($u->path, strlen($b));
225
226 switch($p) {
227 case "./index.php":
228 exit;
229 case "./index.js":
230 case "./index.css":
231 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
232 $s->setBody(new http\Message\Body(fopen($p, "r")));
233 $s->send();
234 exit;
235 }
236
237 ob_start($s);
238
239 ?>
240 <!doctype html>
241 <html>
242 <head>
243 <meta charset="utf-8">
244 <title><?=ns($p)?></title>
245 <base href="<?=$b?>/">
246 <link rel="stylesheet" href="index.css">
247 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
248 </head>
249 <body>
250 <div class="sidebar">
251 <?php ls($p); ?>
252 </div>
253 <?php if ($p === "./") : ?>
254 <h1>Quick Markdown Documentation Browser</h1>
255 <p>v<?php readfile("VERSION")?></p>
256 <pre><?php
257 ob_start(function($s) {
258 return htmlspecialchars($s);
259 });
260 readfile("LICENSE");
261 ob_end_flush();
262 ?></pre>
263 <?php else: ?>
264 <?php md($p, $s); ?>
265 <?php endif; ?>
266
267 <div id="disqus_thread"></div>
268
269 <footer>
270 <a href="VERSION">Version</a>
271 <a href="AUTHORS">Authors</a>
272 <a href="LICENSE">License</a>
273 <?php if ($p !== "./") : ?>
274 <a href="https://github.com/m6w6/mdref/edit/master/<?=trim($p,"/")?>.md">Edit</a>
275 <?php endif; ?>
276 </footer>
277 <script src="index.js"></script>
278 <?php if ($_SERVER["SERVER_NAME"] != "localhost") : ?>
279 <script>
280 var disqus_shortname = 'mdref';
281 var disqus_identifier = '<?=$p?>';
282 (function() {
283 var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
284 dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
285 (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
286 })();
287 </script>
288 <?php endif; ?>
289 </body>
290 </html>
291 <?php
292
293 ob_end_flush();
294 $s->send();