return $ld;
}
-function tail($file, $lines = 1) {
- $bs = 512;
- $fs = 0;
- $ld = [];
- if (is_resource($file) || ($file = fopen($file, "r"))) {
- fseek($file, 0, SEEK_END);
- $fs = ftell($file);
- $fp = $fs;
- $ls = "";
- while ($fp > 0 && count($ld) < $lines) {
- do {
- fseek($file, -min($fp, $bs), SEEK_CUR);
- $fp = ftell($file);
- $ls = fread($file, $fs-$fp) . $ls;
- } while ($fp > 0 && -1 === ($eol = strrpos($ls, "\n", $ls)));
-
- array_unshift($ld, substr($ls, $eol));
- $ls = substr($ls, 0, $eol-1);
+function ns($path) {
+ $ns = "";
+ $parts = explode("/", $path);
+ $upper = ctype_upper($path[0]);
+ for ($i = 0; $i < count($parts); ++$i) {
+ if (!strlen($parts[$i]) || $parts[$i] === ".") {
+ continue;
+ }
+ if (strlen($ns)) {
+ if ($upper && !ctype_upper($parts[$i][0])) {
+ $ns .= "::";
+ } else {
+ $ns .= "\\";
+ }
}
+ $ns .= $parts[$i];
+ $upper = ctype_upper($parts[$i][0]);
}
-
- return $ld;
-}
-
-function ns($file) {
+ return $ns;
return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
}
function ls($dir) {
$dir = rtrim(is_dir($dir) ? $dir : dirname($dir) ."/". basename($dir, ".md"), "/");
printf("<ul>\n");
- printf("<li><a href=/>Home</a></li>\n");
+ printf("<li>⇐ <a href=/>Home</a></li>\n");
if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
- printf("<li><a href=/%s>%s</a></li>\n",
+ printf("<li>⇑ <a href=/%s>%s</a></li>\n",
urlpath($dir, ".."),
ns($dn));
}
if (is_dir($dir)) {
if ($dir !== ".") {
- printf("<li>%s</li>\n", ns($dir));
+ printf("<ul>\n<li> %s</li>\n", ns($dir));
}
+ printf("<ul>\n");
foreach (scandir($dir) as $file) {
/* ignore dot-files */
if ($file{0} === ".") {
if (!isset($pi["extension"]) || $pi["extension"] != "md") {
continue;
}
- if (!ctype_upper($file{0}) && !is_dir("$dir/".$pi["filename"])) {
+ /* ignore files where an accompanying directory exists */
+ if (is_dir("$dir/".$pi["filename"])) {
continue;
}
} else {
- /* ignore directories where an companying file exists */
- if (is_file("$path.md")) {
+ /* ignore directories where no accompanying file exists */
+ if (!is_file("$path.md")) {
continue;
}
}
- printf("<li><a href=\"/%s\">%s</a></li>\n",
+ printf("<li>⇒ <a href=\"/%s\">%s</a></li>\n",
urlpath($dir, $file),
ns("$dir/".basename($file, ".md")));
}
+ printf("</ul>\n");
+ if ($dir !== ".") {
+ printf("</ul>\n");
+ }
}
printf("</ul>\n");
function ml($file) {
$pi = pathinfo($file);
- if (ctype_upper($pi["filename"][0])) {
- printf("<h2>Methods:</h2>\n");
+ if (!isset($pi["extension"])) {
+ return;
+ }
+ if ($pi["extension"] !== "md") {
+ return;
+ }
+ if (!ctype_upper($pi["filename"][0])) {
+ // namespaced functions
+ $dir = $pi["dirname"] . "/" . $pi["filename"];
+ if (is_dir($dir)) {
+ printf("<h2>Functions:</h2>\n");
+ printf("<ul>\n");
+ foreach (scandir($dir) as $file) {
+ if ($file{0} === "." || !is_file("$dir/$file") || ctype_upper($file{0})) {
+ continue;
+ }
+ printf("<li><h3><a href=\"/%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
+ urlpath($dir, $file),
+ basename($file, ".md"),
+ @end(head("$dir/$file", 3)),
+ join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
+ );
+ }
+ printf("</ul>\n");
+ }
+ } else {
+ // methods
$dir = $pi["dirname"] . "/" . $pi["filename"];
if (is_dir($dir)) {
+ printf("<h2>Methods:</h2>\n");
printf("<ul>\n");
foreach (scandir($dir) as $file) {
- if (!is_file("$dir/$file") || ctype_upper($file{0})) {
+ if ($file{0} === "." || !is_file("$dir/$file") || ctype_upper($file{0})) {
continue;
}
printf("<li><h3><a href=\"/%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
}
}
-function md($file) {
+function md($file, $res) {
$file = rtrim($file, "/");
if (is_file($file) || is_file($file .= ".md")) {
- if (extension_loaded("discount") && getenv("DISCOUNT")) {
+ $pi = pathinfo($file);
+
+ switch (@$pi["extension"]) {
+ case "md":
$r = fopen($file, "r");
$md = MarkdownDocument::createFromStream($r);
- $md->compile(MarkdownDocument::AUTOLINK);
+ $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
print str_replace("<br/>","<br />",$md->getHtml());
fclose($r);
- } else {
- printf("<script>document.write(markdown.toHTML(decodeURIComponent(\"%s\")));</script>\n",
- rawurlencode(file_get_contents($file)));
+ ml($file);
+ break;
+ case null:
+ printf("<h1>%s</h1>", basename($file));
+ printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
+ break;
}
- ml($file);
} else {
- printf("<h1>Quick Markdown Doc Browser</h1>\n");
- printf("<p>v0.1.0</p>\n");
- printf("<p>");
- ob_start(function($s) {
- return nl2br(htmlspecialchars($s));
- });
- readfile("LICENSE");
- ob_end_flush();
- printf("</p>\n");
+ $res->setResponseCode(404);
+ printf("<h1>Not Found</h1>\n");
+ printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
}
}
-
-function index($pn) {
- ?>
- <?php
-}
-
chdir($_SERVER["DOCUMENT_ROOT"]);
$t = ["css"=>"text/css", "js"=>"application/javascript"];
$r = new http\Env\Request;
$p = ".". $u->path;
switch($p) {
+case "./index.php":
+ exit;
case "./index.js":
-case "./markdown.js":
case "./index.css":
$s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
$s->setBody(new http\Message\Body(fopen($p, "r")));
<div class="sidebar">
<?php ls($p); ?>
</div>
- <?php md($p); ?>
+ <?php if ($p === "./") : ?>
+ <h1>Quick Markdown Documentation Browser</h1>
+ <p>v<?php readfile("VERSION")?></p>
+ <pre><?php
+ ob_start(function($s) {
+ return htmlspecialchars($s);
+ });
+ readfile("LICENSE");
+ ob_end_flush();
+ ?></pre>
+ <?php else: ?>
+ <?php if (!md($p, $s)): ?>
+ <?php endif; ?>
+ <?php endif; ?>
+ <footer>
+ <a href="/VERSION">Version</a>
+ <a href="/AUTHORS">Authors</a>
+ <a href="/LICENSE">License</a>
+ </footer>
<script src="/index.js"></script>
</body>
</html>