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));
104 if (($glob = glob("$dir/[_a-zA-Z]*.md"))) {
106 foreach ($glob as $file) {
107 printf("<li>⇒ <a href=\"%s\">%s</a></li>\n",
108 urlpath($dir, $file),
109 ns("$dir/".basename($file, ".md")));
122 $pi = pathinfo($file);
123 if (!isset($pi["extension"])) {
126 if ($pi["extension"] !== "md") {
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");
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-"]))
146 function md($file, $res) {
147 $file = rtrim($file, "/");
148 if (is_file($file) ||
is_file($file .= ".md")) {
149 $pi = pathinfo($file);
151 switch (@$pi["extension"]) {
153 $r = fopen($file, "r");
154 $md = MarkdownDocument
::createFromStream($r);
155 $md->compile(MarkdownDocument
::AUTOLINK|MarkdownDocument
::TOC
);
156 print $md->getHtml();
161 printf("<h1>%s</h1>", basename($file));
162 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
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"));
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));
185 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION
)]);
186 $s->setBody(new http\Message\
Body(fopen($p, "r")));
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
>
204 <div
class="sidebar">
207 <?php
if ($p === "./") : ?
>
208 <h1
>Quick Markdown Documentation Browser
</h1
>
209 <p
>v
<?php
readfile("VERSION")?
></p
>
211 ob_start(function($s) {
212 return htmlspecialchars($s);
221 <div id
="disqus_thread"></div
>
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
>
231 <script src
="index.js"></script
>
232 <?php
if ($_SERVER["SERVER_NAME"] != "localhost") : ?
>
234 var disqus_shortname
= 'mdref';
235 var disqus_identifier
= '<?=$p?>';
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
);