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");
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));
98 var_dump(sscanf($email, "%s <%s>"));
102 $dir = rtrim(is_dir($dir) ?
$dir : dirname($dir) ."/". basename($dir, ".md"), "/");
104 printf("<li>⇐ <a href=>Home</a></li>\n");
105 if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
106 printf("<li>⇑ <a href=%s>%s</a></li>\n",
112 printf("<ul>\n<li> %s</li>\n", ns($dir));
114 if (($glob = glob("$dir/[_a-zA-Z]*.md"))) {
116 foreach ($glob as $file) {
117 printf("<li>⇒ <a href=\"%s\">%s</a></li>\n",
118 urlpath($dir, $file),
119 ns("$dir/".basename($file, ".md")));
132 $pi = pathinfo($file);
133 if (!isset($pi["extension"])) {
136 if ($pi["extension"] !== "md") {
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");
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-"]))
156 function md($file, $res) {
157 $file = rtrim($file, "/");
158 if (is_file($file) ||
is_file($file .= ".md")) {
159 $pi = pathinfo($file);
161 switch (@$pi["extension"]) {
163 $r = fopen($file, "r");
164 $md = MarkdownDocument
::createFromStream($r);
165 $md->compile(MarkdownDocument
::AUTOLINK|MarkdownDocument
::TOC
);
166 print $md->getHtml();
171 printf("<h1>%s</h1>", basename($file));
172 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
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"));
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));
195 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION
)]);
196 $s->setBody(new http\Message\
Body(fopen($p, "r")));
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
>
214 <div
class="sidebar">
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
>
221 ob_start(function($s) {
222 return htmlspecialchars($s);
231 <div id
="disqus_thread"></div
>
235 <li
><a href
="https://github.com/m6w6/mdref">mdref
-v
<?php
readfile("VERSION")?
></a
></li
>
236 <li
><a href
="LICENSE">©
; <?
= implode("-", array_unique([2013,idate("Y")]))?
></a
></li
>
239 <script src
="index.js"></script
>
240 <?php
if ($_SERVER["SERVER_NAME"] != "localhost") : ?
>
242 var disqus_shortname
= 'mdref';
243 var disqus_identifier
= '<?=$p?>';
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
);