simplify
[mdref/mdref] / index.php
index 1e09d32ae1a2993beaad7aec0547a3829fe1e366..9bdeceaaccb438c086de2c55882072f6dbe43125 100644 (file)
--- a/index.php
+++ b/index.php
@@ -91,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));
        }
@@ -127,7 +127,7 @@ function ls($dir) {
                                }
                        }
                        
-                       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")));
                }
@@ -154,15 +154,12 @@ function ml($file) {
                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",
+                       foreach (glob("$dir/[_a-z]*.md") as $file) {
+                               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-"]))
+                                       @end(head($file, 3)),
+                                       join(" ", cut(head($file), ["f"=>"1-"]))
                                );
                        }
                        printf("</ul>\n");
@@ -173,15 +170,12 @@ function ml($file) {
                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",
+                       foreach (glob("$dir/[_a-z]*.md") as $file) {
+                               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-"]))
+                                       @end(head($file, 3)),
+                                       join(" ", cut(head($file), ["f"=>"1-"]))
                                );
                        }
                        printf("</ul>\n");
@@ -189,7 +183,7 @@ function ml($file) {
        }
 }
 
-function md($file) {
+function md($file, $res) {
        $file = rtrim($file, "/");
        if (is_file($file) || is_file($file .= ".md")) {
                $pi = pathinfo($file);
@@ -209,23 +203,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":
@@ -246,11 +236,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">
@@ -267,15 +255,31 @@ ob_start($s);
                        ob_end_flush();
                ?></pre>
        <?php else: ?>
-               <?php if (!md($p)): ?>
-               <?php endif; ?>
+               <?php md($p, $s); ?>
        <?php endif; ?>
+       
+       <div id="disqus_thread"></div>
+       
        <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>
+               <?php if ($p !== "./") : ?>
+               <a href="https://github.com/m6w6/mdref/edit/master/<?=trim($p,"/")?>.md">Edit</a>
+               <?php endif; ?>
        </footer>
-       <script src="/index.js"></script>
+       <script src="index.js"></script>
+       <?php if ($_SERVER["SERVER_NAME"] != "localhost") : ?>
+    <script>
+        var disqus_shortname = 'mdref';
+        var disqus_identifier = '<?=$p?>';
+        (function() {
+            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
+            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
+            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
+        })();
+    </script>
+    <?php endif; ?>
 </body>
 </html>
 <?php