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