X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=inline;f=index.php;h=93454eceddd5abfb2dd306e4bb90420805fb4cc7;hb=46d9b15be0fb214993462c7fa1187896b949d00a;hp=b2109846572cbf8c414f5117b29a4a39ddc5c8ba;hpb=66028830117ba6dd4e14a69580ed4b3e858a5241;p=mdref%2Fmdref
diff --git a/index.php b/index.php
index b210984..93454ec 100644
--- a/index.php
+++ b/index.php
@@ -54,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;
@@ -73,16 +73,17 @@ function urlpath($dir, $file) {
function ls($dir) {
$dir = rtrim(is_dir($dir) ? $dir : dirname($dir) ."/". basename($dir, ".md"), "/");
printf("
\n");
- printf("- Home
\n");
+ printf("- ⇐ Home
\n");
if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
- printf("- %s
\n",
+ printf("- ⇑ %s
\n",
urlpath($dir, ".."),
ns($dn));
}
if (is_dir($dir)) {
if ($dir !== ".") {
- printf("- %s
\n", ns($dir));
+ printf("\n- %s
\n", ns($dir));
}
+ printf("\n");
foreach (scandir($dir) as $file) {
/* ignore dot-files */
if ($file{0} === ".") {
@@ -97,7 +98,7 @@ function ls($dir) {
if (!isset($pi["extension"]) || $pi["extension"] != "md") {
continue;
}
- if (!ctype_upper($file{0}) && !is_dir("$dir/".$pi["filename"])) {
+ if (!is_dir("$dir/".$pi["filename"])) {
continue;
}
} else {
@@ -107,10 +108,14 @@ function ls($dir) {
}
}
- printf("- %s
\n",
+ printf("- ⇒ %s
\n",
urlpath($dir, $file),
ns("$dir/".basename($file, ".md")));
}
+ printf("
\n");
+ if ($dir !== ".") {
+ printf("
\n");
+ }
}
printf("
\n");
@@ -118,66 +123,62 @@ function ls($dir) {
function ml($file) {
$pi = pathinfo($file);
- if (ctype_upper($pi["filename"][0])) {
+ 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");
- $dir = $pi["dirname"] . "/" . $pi["filename"];
- if (is_dir($dir)) {
- printf("\n");
- foreach (scandir($dir) as $file) {
- if (!is_file("$dir/$file") || ctype_upper($file{0})) {
- continue;
- }
- printf("%s
%s
\n",
- basename($file, ".md"),
- join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
- );
+ printf("\n");
+ foreach (scandir($dir) as $file) {
+ if (!is_file("$dir/$file") || ctype_upper($file{0})) {
+ continue;
}
- printf("
\n");
+ printf("%s
%s
\n",
+ urlpath($dir, $file),
+ basename($file, ".md"),
+ @end(head("$dir/$file", 3)),
+ join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
+ );
}
+ printf("
\n");
}
}
function md($file) {
$file = rtrim($file, "/");
if (is_file($file) || is_file($file .= ".md")) {
- $r = fopen($file, "r");
- $md = MarkdownDocument::createFromStream($r);
- $md->compile();
- echo $md->getHtml();
- fclose($r);
- ml($file);
+ $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 {
- printf("Quick Markdown Doc Browser
\n");
- printf("v0.1.0
\n");
- printf("");
- ob_start(function($s) {
- return nl2br(htmlspecialchars($s));
- });
- readfile("LICENSE");
- ob_end_flush();
- printf("
\n");
+ http\Env::setResponseCode(404);
+ printf("Not Found
\n");
+ printf("Sorry, I could not find %s/%s
.
", dirname($file), basename($file, ".md"));
}
}
-
function index($pn) {
?>
-
-
-
-
- =ns($pn)?>
-
-
-
-
-
-
-
-
-
"text/css", "js"=>"application/javascript"];
$r = new http\Env\Request;
$u = new http\Url($r->getRequestUrl());
$s = new http\Env\Response;
+$p = ".". $u->path;
-switch($u->path) {
-case "/index.js":
-case "/index.css":
- $s->setHeader("Content-type", $t[pathinfo($u->path, PATHINFO_EXTENSION)]);
- $s->setBody(new http\Message\Body(fopen(basename($u->path), "r")));
- $s->send();
+switch($p) {
+case "./index.php":
exit;
-default:
- ob_start($s);
- index(".".$u->path);
- ob_end_flush();
+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();
- break;
+ exit;
}
+ob_start($s);
+
?>
+
+
+
+
+ =ns($p)?>
+
+
+
+
+
+
+
+
+
+ Quick Markdown Documentation Browser
+ v
+
+
+
+
+
+
+
+
+
+send();