3 error_reporting(E_ALL
&~ E_DEPRECATED
);
5 function cut(array $lines, array $specs) {
6 $delim = "[[:space:]]+";
10 foreach ($specs as $spec => $value) {
28 $func = function($a, $o = 0, $l = 0) {
29 return join(" ", array_slice($a, $o, $l ?
$l+
1 : count($a)-$o));
32 foreach ($lines as $line) {
36 $line = split($delim, $line);
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]);
46 $result[] = $func($line, $spec[0], $spec[2]-$spec[0]);
49 $result[] = $line{$spec[0]};
55 function head($file, $lines = 1) {
57 if (is_resource($file) ||
($file = fopen($file, "r"))) {
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] === ".") {
74 if ($upper && !ctype_upper($parts[$i][0])) {
81 $upper = ctype_upper($parts[$i][0]);
84 return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
87 function urlpath($dir, $file) {
88 return (strlen($dir) ?
$dir . "/" : "") . basename($file, ".md");
92 $dir = rtrim(is_dir($dir) ?
$dir : dirname($dir) ."/". basename($dir, ".md"), "/");
94 printf("<li>⇐ <a href=>Home</a></li>\n");
95 if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
96 printf("<li>⇑ <a href=%s>%s</a></li>\n",
102 printf("<ul>\n<li> %s</li>\n", ns($dir));
105 foreach (scandir($dir) as $file) {
106 /* ignore dot-files */
107 if ($file{0} === ".") {
111 $path = "$dir/$file";
113 if (is_file($path)) {
114 $pi = pathinfo($path);
115 /* ignore files not ending in .md */
116 if (!isset($pi["extension"]) ||
$pi["extension"] != "md") {
119 /* ignore files where an accompanying directory exists */
120 if (is_dir("$dir/".$pi["filename"])) {
124 /* ignore directories where no accompanying file exists */
125 if (!is_file("$path.md")) {
130 printf("<li>⇒ <a href=\"%s\">%s</a></li>\n",
131 urlpath($dir, $file),
132 ns("$dir/".basename($file, ".md")));
144 $pi = pathinfo($file);
145 if (!isset($pi["extension"])) {
148 if ($pi["extension"] !== "md") {
151 if (!ctype_upper($pi["filename"][0])) {
152 // namespaced functions
153 $dir = $pi["dirname"] . "/" . $pi["filename"];
155 printf("<h2>Functions:</h2>\n");
157 foreach (scandir($dir) as $file) {
158 if ($file{0} === "." ||
!is_file("$dir/$file") ||
ctype_upper($file{0})) {
161 printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
162 urlpath($dir, $file),
163 basename($file, ".md"),
164 @end
(head("$dir/$file", 3)),
165 join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
172 $dir = $pi["dirname"] . "/" . $pi["filename"];
174 printf("<h2>Methods:</h2>\n");
176 foreach (scandir($dir) as $file) {
177 if ($file{0} === "." ||
!is_file("$dir/$file") ||
ctype_upper($file{0})) {
180 printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
181 urlpath($dir, $file),
182 basename($file, ".md"),
183 @end
(head("$dir/$file", 3)),
184 join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
192 function md($file, $res) {
193 $file = rtrim($file, "/");
194 if (is_file($file) ||
is_file($file .= ".md")) {
195 $pi = pathinfo($file);
197 switch (@$pi["extension"]) {
199 $r = fopen($file, "r");
200 $md = MarkdownDocument
::createFromStream($r);
201 $md->compile(MarkdownDocument
::AUTOLINK|MarkdownDocument
::TOC
);
202 print str_replace("<br/>","<br />",$md->getHtml());
207 printf("<h1>%s</h1>", basename($file));
208 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
212 $res->setResponseCode(404);
213 printf("<h1>Not Found</h1>\n");
214 printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
219 $t = ["css"=>"text/css", "js"=>"application/javascript"];
220 $r = new http\Env\Request
;
221 $u = new http\
Url($r->getRequestUrl());
222 $s = new http\Env\Response
;
223 $b = dirname($_SERVER["SCRIPT_NAME"]);
224 $p = ".". substr($u->path
, strlen($b));
231 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION
)]);
232 $s->setBody(new http\Message\
Body(fopen($p, "r")));
243 <meta charset
="utf-8">
244 <title
><?
=ns($p)?
></title
>
245 <base href
="<?=$b?>/">
246 <link rel
="stylesheet" href
="index.css">
247 <script src
="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script
>
250 <div
class="sidebar">
253 <?php
if ($p === "./") : ?
>
254 <h1
>Quick Markdown Documentation Browser
</h1
>
255 <p
>v
<?php
readfile("VERSION")?
></p
>
257 ob_start(function($s) {
258 return htmlspecialchars($s);
267 <div id
="disqus_thread"></div
>
270 <a href
="VERSION">Version
</a
>
271 <a href
="AUTHORS">Authors
</a
>
272 <a href
="LICENSE">License
</a
>
274 <script src
="index.js"></script
>
275 <?php
if ($_SERVER["SERVER_NAME"] != "localhost") : ?
>
277 var disqus_shortname
= 'mdref';
278 var disqus_identifier
= '<?=$p?>';
280 var dsq
= document
.createElement('script'); dsq
.type
= 'text/javascript'; dsq
.async
= true;
281 dsq
.src
= '//' + disqus_shortname +
'.disqus.com/embed.js';
282 (document
.getElementsByTagName('head')[0] || document
.getElementsByTagName('body')[0]).appendChild(dsq
);