5c4b909b5057fe201b23b09978d73c0a785222dd
[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 if (($glob = glob("$dir/[_a-zA-Z]*.md"))) {
105 printf("<ul>\n");
106 foreach ($glob as $file) {
107 printf("<li>&rArr; <a href=\"%s\">%s</a></li>\n",
108 urlpath($dir, $file),
109 ns("$dir/".basename($file, ".md")));
110 }
111 printf("</ul>\n");
112 }
113 if ($dir !== ".") {
114 printf("</ul>\n");
115 }
116 }
117
118 printf("</ul>\n");
119 }
120
121 function ml($file) {
122 $pi = pathinfo($file);
123 if (!isset($pi["extension"])) {
124 return;
125 }
126 if ($pi["extension"] !== "md") {
127 return;
128 }
129 $dir = $pi["dirname"] . "/" . $pi["filename"];
130 if (($glob = glob("$dir/[_a-z]*.md"))) {
131 printf("<h2>%s:</h2>\n", !ctype_upper($pi["filename"][0]) ?
132 "Functions" : "Methods");
133 printf("<ul>\n");
134 foreach ($glob as $file) {
135 printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
136 urlpath($dir, $file),
137 basename($file, ".md"),
138 @end(head($file, 3)),
139 join(" ", cut(head($file), ["f"=>"1-"]))
140 );
141 }
142 printf("</ul>\n");
143 }
144 }
145
146 function md($file, $res) {
147 $file = rtrim($file, "/");
148 if (is_file($file) || is_file($file .= ".md")) {
149 $pi = pathinfo($file);
150
151 switch (@$pi["extension"]) {
152 case "md":
153 $r = fopen($file, "r");
154 $md = MarkdownDocument::createFromStream($r);
155 $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
156 print $md->getHtml();
157 fclose($r);
158 ml($file);
159 break;
160 case null:
161 printf("<h1>%s</h1>", basename($file));
162 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
163 break;
164 }
165 } else {
166 $res->setResponseCode(404);
167 printf("<h1>Not Found</h1>\n");
168 printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
169 }
170 }
171
172 chdir(__DIR__);
173 $t = ["css"=>"text/css", "js"=>"application/javascript"];
174 $r = new http\Env\Request;
175 $u = new http\Url($r->getRequestUrl());
176 $s = new http\Env\Response;
177 $b = dirname($_SERVER["SCRIPT_NAME"]);
178 $p = ".". substr($u->path, strlen($b));
179
180 switch($p) {
181 case "./index.php":
182 exit;
183 case "./index.js":
184 case "./index.css":
185 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
186 $s->setBody(new http\Message\Body(fopen($p, "r")));
187 $s->send();
188 exit;
189 }
190
191 ob_start($s);
192
193 ?>
194 <!doctype html>
195 <html>
196 <head>
197 <meta charset="utf-8">
198 <title><?=ns($p)?></title>
199 <base href="<?=$b?>/">
200 <link rel="stylesheet" href="index.css">
201 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
202 </head>
203 <body>
204 <div class="sidebar">
205 <?php ls($p); ?>
206 </div>
207 <?php if ($p === "./") : ?>
208 <h1>Quick Markdown Documentation Browser</h1>
209 <p>v<?php readfile("VERSION")?></p>
210 <pre><?php
211 ob_start(function($s) {
212 return htmlspecialchars($s);
213 });
214 readfile("LICENSE");
215 ob_end_flush();
216 ?></pre>
217 <?php else: ?>
218 <?php md($p, $s); ?>
219 <?php endif; ?>
220
221 <div id="disqus_thread"></div>
222
223 <footer>
224 <a href="VERSION">Version</a>
225 <a href="AUTHORS">Authors</a>
226 <a href="LICENSE">License</a>
227 <?php if ($p !== "./") : ?>
228 <a href="https://github.com/m6w6/mdref/edit/master/<?=trim($p,"/")?>.md">Edit</a>
229 <?php endif; ?>
230 </footer>
231 <script src="index.js"></script>
232 <?php if ($_SERVER["SERVER_NAME"] != "localhost") : ?>
233 <script>
234 var disqus_shortname = 'mdref';
235 var disqus_identifier = '<?=$p?>';
236 (function() {
237 var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
238 dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
239 (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
240 })();
241 </script>
242 <?php endif; ?>
243 </body>
244 </html>
245 <?php
246
247 ob_end_flush();
248 $s->send();