X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;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, "
".join(" ", cut(head("./$dir/$file"), ["f"=>"1-"]))."
"; + + $path = "$dir/$file"; + + if (is_file($path)) { + $pi = pathinfo($path); + /* ignore files not ending in .md */ + if (!isset($pi["extension"]) || $pi["extension"] != "md") { + continue; + } + if (!is_dir("$dir/".$pi["filename"])) { + continue; + } + } else { + /* ignore directories where an companying file exists */ + if (is_file("$path.md")) { + continue; + } + } + + printf("%s
%s
%s\n", htmlspecialchars(file_get_contents($file))); + break; + } + } else { + http\Env::setResponseCode(404); + printf("
", 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); ?> - -Sorry, I could not find
%s/%s
.
v
+ + + + + + + +send();