X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=index.php;h=5c4b909b5057fe201b23b09978d73c0a785222dd;hb=a5291ff36193869a65db4cbf30acf1febe29b15d;hp=b2109846572cbf8c414f5117b29a4a39ddc5c8ba;hpb=333f749f9ae65fecd6b52b06fde3dceda1579470;p=mdref%2Fmdref
diff --git a/index.php b/index.php
index b210984..5c4b909 100644
--- a/index.php
+++ b/index.php
@@ -54,15 +54,33 @@ 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, "/.")));
}
@@ -73,43 +91,27 @@ 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));
}
- foreach (scandir($dir) as $file) {
- /* ignore dot-files */
- if ($file{0} === ".") {
- continue;
- }
-
- $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 (!ctype_upper($file{0}) && !is_dir("$dir/".$pi["filename"])) {
- continue;
- }
- } else {
- /* ignore directories where an companying file exists */
- if (is_file("$path.md")) {
- continue;
- }
+ if (($glob = glob("$dir/[_a-zA-Z]*.md"))) {
+ printf("\n");
+ foreach ($glob as $file) {
+ printf("- ⇒ %s
\n",
+ urlpath($dir, $file),
+ ns("$dir/".basename($file, ".md")));
}
-
- printf("- %s
\n",
- urlpath($dir, $file),
- ns("$dir/".basename($file, ".md")));
+ printf("
\n");
+ }
+ if ($dir !== ".") {
+ printf("
\n");
}
}
@@ -118,88 +120,129 @@ function ls($dir) {
function ml($file) {
$pi = pathinfo($file);
- if (ctype_upper($pi["filename"][0])) {
- 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");
+ 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");
+ foreach ($glob as $file) {
+ printf("%s
%s
\n",
+ urlpath($dir, $file),
+ basename($file, ".md"),
+ @end(head($file, 3)),
+ join(" ", cut(head($file), ["f"=>"1-"]))
+ );
}
+ printf("
\n");
}
}
-function md($file) {
+function md($file, $res) {
$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|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 {
- 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");
+ $res->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;
+$b = dirname($_SERVER["SCRIPT_NAME"]);
+$p = ".". substr($u->path, strlen($b));
-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();