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 (($fd = 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("<li>%s</li>\n", ns($dir));
86 foreach (scandir($dir) as $file) {
87 /* ignore dot-files */
88 if ($file{0} === ".") {
95 $pi = pathinfo($path);
96 /* ignore files not ending in .md */
97 if (!isset($pi["extension"]) ||
$pi["extension"] != "md") {
100 if (!ctype_upper($file{0}) && !is_dir("$dir/".$pi["filename"])) {
104 /* ignore directories where an companying file exists */
105 if (is_file("$path.md")) {
110 printf("<li><a href=\"/%s\">%s</a></li>\n",
111 urlpath($dir, $file),
112 ns("$dir/".basename($file, ".md")));
120 $pi = pathinfo($file);
121 if (ctype_upper($pi["filename"][0])) {
122 printf("<h2>Methods:</h2>\n");
123 $dir = $pi["dirname"] . "/" . $pi["filename"];
126 foreach (scandir($dir) as $file) {
127 if (!is_file("$dir/$file") ||
ctype_upper($file{0})) {
130 printf("<li><h3>%s</h3><p>%s</p></li>\n",
131 basename($file, ".md"),
132 join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
141 $file = rtrim($file, "/");
142 if (is_file($file) ||
is_file($file .= ".md")) {
143 $r = fopen($file, "r");
144 $md = MarkdownDocument
::createFromStream($r);
150 printf("<h1>Quick Markdown Doc Browser</h1>\n");
151 printf("<p>v0.1.0</p>\n");
153 ob_start(function($s) {
154 return nl2br(htmlspecialchars($s));
163 function index($pn) {
168 <meta charset
="utf-8">
169 <title
><?
=ns($pn)?
></title
>
170 <link rel
="stylesheet" href
="/index.css">
173 <div
class="sidebar">
177 <script src
="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script
>
178 <script src
="/index.js"></script
>
184 chdir($_SERVER["DOCUMENT_ROOT"]);
185 $t = ["css"=>"text/css", "js"=>"application/javascript"];
186 $r = new http\Env\Request
;
187 $u = new http\
Url($r->getRequestUrl());
188 $s = new http\Env\Response
;
193 $s->setHeader("Content-type", $t[pathinfo($u->path
, PATHINFO_EXTENSION
)]);
194 $s->setBody(new http\Message\
Body(fopen(basename($u->path
), "r")));