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"))) {
66 return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
69 function urlpath($dir, $file) {
70 return (strlen($dir) ?
$dir . "/" : "") . basename($file, ".md");
74 $dir = rtrim(is_dir($dir) ?
$dir : dirname($dir) ."/". basename($dir, ".md"), "/");
76 printf("<li>⇐ <a href=/>Home</a></li>\n");
77 if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
78 printf("<li>⇑ <a href=/%s>%s</a></li>\n",
84 printf("<ul>\n<li> %s</li>\n", ns($dir));
87 foreach (scandir($dir) as $file) {
88 /* ignore dot-files */
89 if ($file{0} === ".") {
96 $pi = pathinfo($path);
97 /* ignore files not ending in .md */
98 if (!isset($pi["extension"]) ||
$pi["extension"] != "md") {
101 if (!is_dir("$dir/".$pi["filename"])) {
105 /* ignore directories where an companying file exists */
106 if (is_file("$path.md")) {
111 printf("<li>⇒ <a href=\"/%s\">%s</a></li>\n",
112 urlpath($dir, $file),
113 ns("$dir/".basename($file, ".md")));
125 $pi = pathinfo($file);
126 if (!isset($pi["extension"])) {
129 if ($pi["extension"] !== "md") {
132 if (!ctype_upper($pi["filename"][0])) {
135 $dir = $pi["dirname"] . "/" . $pi["filename"];
137 printf("<h2>Methods:</h2>\n");
139 foreach (scandir($dir) as $file) {
140 if (!is_file("$dir/$file") ||
ctype_upper($file{0})) {
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-"]))
155 $file = rtrim($file, "/");
156 if (is_file($file) ||
is_file($file .= ".md")) {
157 $pi = pathinfo($file);
159 switch (@$pi["extension"]) {
161 $r = fopen($file, "r");
162 $md = MarkdownDocument
::createFromStream($r);
163 $md->compile(MarkdownDocument
::AUTOLINK
);
164 print str_replace("<br/>","<br />",$md->getHtml());
169 printf("<h1>%s</h1>", basename($file));
170 printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
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"));
180 function index($pn) {
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
;
197 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION
)]);
198 $s->setBody(new http\Message\
Body(fopen($p, "r")));
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
>
218 <div
class="sidebar">
221 <?php
if ($p === "./") : ?
>
222 <h1
>Quick Markdown Documentation Browser
</h1
>
223 <p
>v
<?php
readfile("VERSION")?
></p
>
225 ob_start(function($s) {
226 return htmlspecialchars($s);
232 <?php
if (!md($p)): ?
>
236 <a href
="/VERSION">Version
</a
>
237 <a href
="/AUTHORS">Authors
</a
>
238 <a href
="/LICENSE">License
</a
>
240 <script src
="/index.js"></script
>