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