X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=index.php;h=5c4b909b5057fe201b23b09978d73c0a785222dd;hb=4199b3fd65d963057c82c1541df6e2f786e6bd9d;hp=160fed53b3418e5a0a30fb4f80e4e594c5fbf86d;hpb=277cfe1f0e09b05e6615ce87603672e0f305520a;p=mdref%2Fmdref diff --git a/index.php b/index.php index 160fed5..5c4b909 100644 --- a/index.php +++ b/index.php @@ -2,8 +2,6 @@ error_reporting(E_ALL &~ E_DEPRECATED); -define("OUTPUT", fopen("php://memory", "w+")); - function cut(array $lines, array $specs) { $delim = "[[:space:]]+"; $bytes = []; @@ -56,111 +54,195 @@ function cut(array $lines, array $specs) { function head($file, $lines = 1) { $ld = []; - if (($fd = fopen($file, "r"))) { + if (is_resource($file) || ($file = fopen($file, "r"))) { while ($lines--) { - $ld[] = fgets($fd); + $ld[] = fgets($file); } } return $ld; } -function ns($file) { +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 $ns; return str_replace("/", "\\", str_replace("//", "/", trim($file, "/."))); } function urlpath($dir, $file) { - return (strlen($dir) ? $dir . "/" : "") . urlencode($file); + return (strlen($dir) ? $dir . "/" : "") . basename($file, ".md"); } -function ls($dir, $invert = false) { - fprintf(OUTPUT, "\n"); } function ml($file) { $pi = pathinfo($file); - if (ctype_upper($pi["filename"][0])) { - fprintf(OUTPUT, "

Methods:

\n"); - $el = $pi["dirname"] . "/" . $pi["filename"]; - ls($el, true); + if (!isset($pi["extension"])) { + return; + } + if ($pi["extension"] !== "md") { + return; + } + $dir = $pi["dirname"] . "/" . $pi["filename"]; + if (($glob = glob("$dir/[_a-z]*.md"))) { + printf("

%s:

\n", !ctype_upper($pi["filename"][0]) ? + "Functions" : "Methods"); + printf("\n"); } } -function md($file) { - $r = fopen($file, "r"); - $md = MarkdownDocument::createFromStream($r); - $md->compile(); - $md->writeHtml(OUTPUT); - unset($md); - fclose($r); - - // BS Markdown seeks around... - fseek(OUTPUT, 0, SEEK_END); - - ml($file); +function md($file, $res) { + $file = rtrim($file, "/"); + if (is_file($file) || is_file($file .= ".md")) { + $pi = pathinfo($file); + + switch (@$pi["extension"]) { + case "md": + $r = fopen($file, "r"); + $md = MarkdownDocument::createFromStream($r); + $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC); + print $md->getHtml(); + fclose($r); + ml($file); + break; + case null: + printf("

%s

", basename($file)); + printf("
%s
\n", htmlspecialchars(file_get_contents($file))); + break; + } + } else { + $res->setResponseCode(404); + printf("

Not Found

\n"); + printf("

Sorry, I could not find %s/%s.

", dirname($file), basename($file, ".md")); + } } +chdir(__DIR__); +$t = ["css"=>"text/css", "js"=>"application/javascript"]; $r = new http\Env\Request; $u = new http\Url($r->getRequestUrl()); -$t = ["css"=>"text/css", "js"=>"application/javascript"]; +$s = new http\Env\Response; +$b = dirname($_SERVER["SCRIPT_NAME"]); +$p = ".". substr($u->path, strlen($b)); -switch($u->path) { -case "/index.js": -case "/index.css": - $s = new http\Env\Response; - $s->setHeader("Content-type", $t[pathinfo($u->path, PATHINFO_EXTENSION)]); - $s->setBody(new http\Message\Body(fopen(basename($u->path), "r"))); +switch($p) { +case "./index.php": + exit; +case "./index.js": +case "./index.css": + $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]); + $s->setBody(new http\Message\Body(fopen($p, "r"))); $s->send(); exit; } -if (is_dir(".".$u->path)) { - ls(".".$u->path); -} else { - md(".".$u->path); -} +ob_start($s); ?> - -<?=$u->path?> - + + <?=ns($p)?> + + + - - - + + +

Quick Markdown Documentation Browser

+

v

+
+ + + + +
+ + + + + + +send();