5c4f5a77e4513ae8f7c8c75b1b5653e26d0f4e17
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"))) {
65 function tail($file, $lines = 1) {
69 if (is_resource($file) ||
($file = fopen($file, "r"))) {
70 fseek($file, 0, SEEK_END
);
74 while ($fp > 0 && count($ld) < $lines) {
76 fseek($file, -min($fp, $bs), SEEK_CUR
);
78 $ls = fread($file, $fs-$fp) . $ls;
79 } while ($fp > 0 && -1 === ($eol = strrpos($ls, "\n", $ls)));
81 array_unshift($ld, substr($ls, $eol));
82 $ls = substr($ls, 0, $eol-1);
90 return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
93 function urlpath($dir, $file) {
94 return (strlen($dir) ?
$dir . "/" : "") . basename($file, ".md");
98 $dir = rtrim(is_dir($dir) ?
$dir : dirname($dir) ."/". basename($dir, ".md"), "/");
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",
108 printf("<li>%s</li>\n", ns($dir));
110 foreach (scandir($dir) as $file) {
111 /* ignore dot-files */
112 if ($file{0} === ".") {
116 $path = "$dir/$file";
118 if (is_file($path)) {
119 $pi = pathinfo($path);
120 /* ignore files not ending in .md */
121 if (!isset($pi["extension"]) ||
$pi["extension"] != "md") {
124 if (!ctype_upper($file{0}) && !is_dir("$dir/".$pi["filename"])) {
128 /* ignore directories where an companying file exists */
129 if (is_file("$path.md")) {
134 printf("<li><a href=\"/%s\">%s</a></li>\n",
135 urlpath($dir, $file),
136 ns("$dir/".basename($file, ".md")));
144 $pi = pathinfo($file);
145 if (ctype_upper($pi["filename"][0])) {
146 printf("<h2>Methods:</h2>\n");
147 $dir = $pi["dirname"] . "/" . $pi["filename"];
150 foreach (scandir($dir) as $file) {
151 if (!is_file("$dir/$file") ||
ctype_upper($file{0})) {
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-"]))
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());
176 printf("<script>document.write(markdown.toHTML(decodeURIComponent(\"%s\")));</script>\n",
177 rawurlencode(file_get_contents($file)));
181 printf("<h1>Quick Markdown Doc Browser</h1>\n");
182 printf("<p>v0.1.0</p>\n");
184 ob_start(function($s) {
185 return nl2br(htmlspecialchars($s));
194 function index($pn) {
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
;
208 case "./markdown.js":
210 $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION
)]);
211 $s->setBody(new http\Message\
Body(fopen($p, "r")));
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
>
231 <div
class="sidebar">
235 <script src
="/index.js"></script
>