X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=index.php;h=5c4b909b5057fe201b23b09978d73c0a785222dd;hb=a5291ff36193869a65db4cbf30acf1febe29b15d;hp=5c4f5a77e4513ae8f7c8c75b1b5653e26d0f4e17;hpb=ff5748653c7dc73c8dc80725dce38bb3119ddac3;p=mdref%2Fmdref
diff --git a/index.php b/index.php
index 5c4f5a7..5c4b909 100644
--- a/index.php
+++ b/index.php
@@ -62,31 +62,25 @@ function head($file, $lines = 1) {
return $ld;
}
-function tail($file, $lines = 1) {
- $bs = 512;
- $fs = 0;
- $ld = [];
- if (is_resource($file) || ($file = fopen($file, "r"))) {
- fseek($file, 0, SEEK_END);
- $fs = ftell($file);
- $fp = $fs;
- $ls = "";
- while ($fp > 0 && count($ld) < $lines) {
- do {
- fseek($file, -min($fp, $bs), SEEK_CUR);
- $fp = ftell($file);
- $ls = fread($file, $fs-$fp) . $ls;
- } while ($fp > 0 && -1 === ($eol = strrpos($ls, "\n", $ls)));
-
- array_unshift($ld, substr($ls, $eol));
- $ls = substr($ls, 0, $eol-1);
+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 $ld;
-}
-
-function ns($file) {
+ return $ns;
return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
}
@@ -97,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");
}
}
@@ -142,70 +120,67 @@ 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",
- urlpath($dir, $file),
- basename($file, ".md"),
- @end(head("$dir/$file", 3)),
- 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")) {
- if (extension_loaded("discount") && getenv("DISCOUNT")) {
+ $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());
+ $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
+ print $md->getHtml();
fclose($r);
- } else {
- printf("\n",
- rawurlencode(file_get_contents($file)));
+ ml($file);
+ break;
+ case null:
+ printf("%s
", basename($file));
+ printf("
%s
\n", htmlspecialchars(file_get_contents($file)));
+ break;
}
- ml($file);
} 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) {
- ?>
- "text/css", "js"=>"application/javascript"];
$r = new http\Env\Request;
$u = new http\Url($r->getRequestUrl());
$s = new http\Env\Response;
-$p = ".". $u->path;
+$b = dirname($_SERVER["SCRIPT_NAME"]);
+$p = ".". substr($u->path, strlen($b));
switch($p) {
+case "./index.php":
+ exit;
case "./index.js":
-case "./markdown.js":
case "./index.css":
$s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
$s->setBody(new http\Message\Body(fopen($p, "r")));
@@ -221,18 +196,50 @@ ob_start($s);
=ns($p)?>
-
+
+
-
-
-
-
-
+
+ Quick Markdown Documentation Browser
+ v
+
+
+
+
+
+
+
+
+
+
+
+