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