support sub dirs
[mdref/mdref] / index.php
index 93454eceddd5abfb2dd306e4bb90420805fb4cc7..7ae8e478efa5eac7432ab39c0d68dd2e4b524cd4 100644 (file)
--- a/index.php
+++ b/index.php
@@ -62,7 +62,25 @@ function head($file, $lines = 1) {
        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,9 +91,9 @@ function urlpath($dir, $file) {
 function ls($dir) {
        $dir = rtrim(is_dir($dir) ? $dir : dirname($dir) ."/". basename($dir, ".md"), "/");
        printf("<ul>\n");
-       printf("<li>&lArr; <a href=/>Home</a></li>\n");
+       printf("<li>&lArr; <a href=>Home</a></li>\n");
        if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
-               printf("<li>&uArr; <a href=/%s>%s</a></li>\n", 
+               printf("<li>&uArr; <a href=%s>%s</a></li>\n", 
                        urlpath($dir, ".."),
                        ns($dn));
        }
@@ -98,17 +116,18 @@ function ls($dir) {
                                if (!isset($pi["extension"]) || $pi["extension"] != "md") {
                                        continue;
                                }
-                               if (!is_dir("$dir/".$pi["filename"])) {
+                               /* ignore files where an accompanying directory exists */
+                               if (is_dir("$dir/".$pi["filename"])) {
                                        continue;
                                }
                        } else {
-                               /* ignore directories where an companying file exists */
-                               if (is_file("$path.md")) {
+                               /* ignore directories where no accompanying file exists */
+                               if (!is_file("$path.md")) {
                                        continue;
                                }
                        }
                        
-                       printf("<li>&rArr; <a href=\"/%s\">%s</a></li>\n", 
+                       printf("<li>&rArr; <a href=\"%s\">%s</a></li>\n", 
                                urlpath($dir, $file),
                                ns("$dir/".basename($file, ".md")));
                }
@@ -130,28 +149,47 @@ function ml($file) {
                return;
        }
        if (!ctype_upper($pi["filename"][0])) {
-               return;
-       }
-       $dir = $pi["dirname"] . "/" . $pi["filename"];
-       if (is_dir($dir)) {
-               printf("<h2>Methods:</h2>\n");
-               printf("<ul>\n");
-               foreach (scandir($dir) as $file) {
-                       if (!is_file("$dir/$file") || ctype_upper($file{0})) {
-                               continue;
+               // namespaced functions
+               $dir = $pi["dirname"] . "/" . $pi["filename"];
+               if (is_dir($dir)) {
+                       printf("<h2>Functions:</h2>\n");
+                       printf("<ul>\n");
+                       foreach (scandir($dir) as $file) {
+                               if ($file{0} === "." || !is_file("$dir/$file") || ctype_upper($file{0})) {
+                                       continue;
+                               }
+                               printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
+                                       urlpath($dir, $file),
+                                       basename($file, ".md"),
+                                       @end(head("$dir/$file", 3)),
+                                       join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
+                               );
                        }
-                       printf("<li><h3><a href=\"/%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
-                               urlpath($dir, $file),
-                               basename($file, ".md"),
-                               @end(head("$dir/$file", 3)),
-                               join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
-                       );
+                       printf("</ul>\n");
+               }
+       } else {
+               // methods
+               $dir = $pi["dirname"] . "/" . $pi["filename"];
+               if (is_dir($dir)) {
+                       printf("<h2>Methods:</h2>\n");
+                       printf("<ul>\n");
+                       foreach (scandir($dir) as $file) {
+                               if ($file{0} === "." || !is_file("$dir/$file") || ctype_upper($file{0})) {
+                                       continue;
+                               }
+                               printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
+                                       urlpath($dir, $file),
+                                       basename($file, ".md"),
+                                       @end(head("$dir/$file", 3)),
+                                       join(" ", cut(head("$dir/$file"), ["f"=>"1-"]))
+                               );
+                       }
+                       printf("</ul>\n");
                }
-               printf("</ul>\n");
        }
 }
 
-function md($file) {
+function md($file, $res) {
        $file = rtrim($file, "/");
        if (is_file($file) || is_file($file .= ".md")) {
                $pi = pathinfo($file);
@@ -160,7 +198,7 @@ function md($file) {
                case "md":
                        $r = fopen($file, "r");
                        $md = MarkdownDocument::createFromStream($r);
-                       $md->compile(MarkdownDocument::AUTOLINK);
+                       $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
                        print str_replace("<br/>","<br />",$md->getHtml());
                        fclose($r);
                        ml($file);
@@ -171,23 +209,19 @@ function md($file) {
                        break;
                }
        } else {
-               http\Env::setResponseCode(404);
+               $res->setResponseCode(404);
                printf("<h1>Not Found</h1>\n");
                printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
        }
 }
 
-function index($pn) {
-       ?>
-       <?php
-}
-
-chdir($_SERVER["DOCUMENT_ROOT"]);
+chdir(__DIR__);
 $t = ["css"=>"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":
@@ -208,11 +242,9 @@ ob_start($s);
 <head>
        <meta charset="utf-8">
        <title><?=ns($p)?></title>
-       <link rel="stylesheet" href="/index.css">
+       <base href="<?=$b?>/">
+       <link rel="stylesheet" href="index.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
-       <?php if (!extension_loaded("discount") || !getenv("DISCOUNT")) : ?>
-       <script src="/markdown.js"></script>
-       <?php endif; ?>
 </head>
 <body>
        <div class="sidebar">
@@ -229,15 +261,14 @@ ob_start($s);
                        ob_end_flush();
                ?></pre>
        <?php else: ?>
-               <?php if (!md($p)): ?>
-               <?php endif; ?>
+               <?php md($p, $s); ?>
        <?php endif; ?>
        <footer>
-               <a href="/VERSION">Version</a>
-               <a href="/AUTHORS">Authors</a>
-               <a href="/LICENSE">License</a>
+               <a href="VERSION">Version</a>
+               <a href="AUTHORS">Authors</a>
+               <a href="LICENSE">License</a>
        </footer>
-       <script src="/index.js"></script>
+       <script src="index.js"></script>
 </body>
 </html>
 <?php