9bdeceaaccb438c086de2c55882072f6dbe43125
[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 (glob("$dir/[_a-z]*.md") as $file) {
158 printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
159 urlpath($dir, $file),
160 basename($file, ".md"),
161 @end(head($file, 3)),
162 join(" ", cut(head($file), ["f"=>"1-"]))
163 );
164 }
165 printf("</ul>\n");
166 }
167 } else {
168 // methods
169 $dir = $pi["dirname"] . "/" . $pi["filename"];
170 if (is_dir($dir)) {
171 printf("<h2>Methods:</h2>\n");
172 printf("<ul>\n");
173 foreach (glob("$dir/[_a-z]*.md") as $file) {
174 printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
175 urlpath($dir, $file),
176 basename($file, ".md"),
177 @end(head($file, 3)),
178 join(" ", cut(head($file), ["f"=>"1-"]))
179 );
180 }
181 printf("</ul>\n");
182 }
183 }
184 }
185
186 function md($file, $res) {
187 $file = rtrim($file, "/");
188 if (is_file($file) || is_file($file .= ".md")) {
189 $pi = pathinfo($file);
190
191 switch (@$pi["extension"]) {
192 case "md":
193 $r = fopen($file, "r");
194 $md = MarkdownDocument::createFromStream($r);
195 $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
196 print str_replace("<br/>","<br />",$md->getHtml());
197 fclose($r);
198 ml($file);
199 break;
200 case null:
201 printf("<h1>%s</h1>", basename($file));
202 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
203 break;
204 }
205 } else {
206 $res->setResponseCode(404);
207 printf("<h1>Not Found</h1>\n");
208 printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
209 }
210 }
211
212 chdir(__DIR__);
213 $t = ["css"=>"text/css", "js"=>"application/javascript"];
214 $r = new http\Env\Request;
215 $u = new http\Url($r->getRequestUrl());
216 $s = new http\Env\Response;
217 $b = dirname($_SERVER["SCRIPT_NAME"]);
218 $p = ".". substr($u->path, strlen($b));
219
220 switch($p) {
221 case "./index.php":
222 exit;
223 case "./index.js":
224 case "./index.css":
225 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
226 $s->setBody(new http\Message\Body(fopen($p, "r")));
227 $s->send();
228 exit;
229 }
230
231 ob_start($s);
232
233 ?>
234 <!doctype html>
235 <html>
236 <head>
237 <meta charset="utf-8">
238 <title><?=ns($p)?></title>
239 <base href="<?=$b?>/">
240 <link rel="stylesheet" href="index.css">
241 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
242 </head>
243 <body>
244 <div class="sidebar">
245 <?php ls($p); ?>
246 </div>
247 <?php if ($p === "./") : ?>
248 <h1>Quick Markdown Documentation Browser</h1>
249 <p>v<?php readfile("VERSION")?></p>
250 <pre><?php
251 ob_start(function($s) {
252 return htmlspecialchars($s);
253 });
254 readfile("LICENSE");
255 ob_end_flush();
256 ?></pre>
257 <?php else: ?>
258 <?php md($p, $s); ?>
259 <?php endif; ?>
260
261 <div id="disqus_thread"></div>
262
263 <footer>
264 <a href="VERSION">Version</a>
265 <a href="AUTHORS">Authors</a>
266 <a href="LICENSE">License</a>
267 <?php if ($p !== "./") : ?>
268 <a href="https://github.com/m6w6/mdref/edit/master/<?=trim($p,"/")?>.md">Edit</a>
269 <?php endif; ?>
270 </footer>
271 <script src="index.js"></script>
272 <?php if ($_SERVER["SERVER_NAME"] != "localhost") : ?>
273 <script>
274 var disqus_shortname = 'mdref';
275 var disqus_identifier = '<?=$p?>';
276 (function() {
277 var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
278 dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
279 (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
280 })();
281 </script>
282 <?php endif; ?>
283 </body>
284 </html>
285 <?php
286
287 ob_end_flush();
288 $s->send();