X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=index.php;h=93454eceddd5abfb2dd306e4bb90420805fb4cc7;hb=841dfdc712d783355b282da9991d0de2a9c584ef;hp=160fed53b3418e5a0a30fb4f80e4e594c5fbf86d;hpb=13943464c255c79c2038c4c63ad5af34331af088;p=mdref%2Fmdref diff --git a/index.php b/index.php index 160fed5..93454ec 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,9 +54,9 @@ 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; @@ -69,98 +67,180 @@ function ns($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; + } + if (!ctype_upper($pi["filename"][0])) { + return; + } + $dir = $pi["dirname"] . "/" . $pi["filename"]; + if (is_dir($dir)) { + printf("

Methods:

\n"); + printf("\n"); } } function md($file) { - $r = fopen($file, "r"); - $md = MarkdownDocument::createFromStream($r); - $md->compile(); - $md->writeHtml(OUTPUT); - unset($md); - fclose($r); + $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); + print str_replace("
","
",$md->getHtml()); + fclose($r); + ml($file); + break; + case null: + printf("

%s

", basename($file)); + printf("
%s
\n", htmlspecialchars(file_get_contents($file))); + break; + } + } else { + http\Env::setResponseCode(404); + printf("

Not Found

\n"); + printf("

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

", dirname($file), basename($file, ".md")); + } +} - // BS Markdown seeks around... - fseek(OUTPUT, 0, SEEK_END); - - ml($file); +function index($pn) { + ?> + "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; +$p = ".". $u->path; -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();