support sub dirs
[mdref/mdref] / index.js
index 2f6577266773498967a13dae88b859d087342a1d..5f384ca09d2d24129d0dab53bbd93eeb76cbf57e 100644 (file)
--- a/index.js
+++ b/index.js
@@ -1,14 +1,24 @@
+function log() {
+       // console.log.apply(console, arguments);
+}
+
 function is_constant(s) {
-       return s.length > 3 &&  s.toUpperCase(s) === s;
+       s = s.replace(/v\d+(_\d+)?$/, "");
+       if (s.length < 2) {
+               return false;
+       }
+       return s.toUpperCase(s) === s;
 }
 
 function is_variable(s) {
        return s.substring(0,1) === "$";
 }
 
-function type(s) {
+var is_in_string = false;
+
+function type(s, nn) {
        var i, j, t;
-       
+       //log("type", s);
        // nothing
        if (!s.match(/[a-zA-Z]/)) {
                return;
@@ -25,20 +35,43 @@ function type(s) {
        case "object":
        case "callable":
        case "mixed":
+       // Zend/SPL
+       case "stdClass":
+       case "Exception":
+       case "ErrorException":
+       case "Closure":
+       case "Generator":
+       case "Countable":
+       case "Serializable":
+       case "Traversable":
+       case "Iterator":
+       case "IteratorAggregate":
+       case "ArrayAccess":
+       case "ArrayObject":
+       case "ArrayIterator":
+       case "RecursiveArrayIterator":
+       case "SplObserver":
+       case "SplSubject":
+       case "SplObjectStorage":
                return "<code>";
                
        // keywords
+       case "extends":
+       case "implements":
+               if (nn === "H1") {
+                       return "<br>&nbsp;<em>";
+               }
        case "class":
        case "interface":
        case "namespace":
-       case "extends":
-       case "implements":
        case "public":
        case "protected":
        case "private":
        case "static":
        case "final":
        case "abstract":
+       case "self":
+       case "parent":
        // phrases
        case "Optional":
        case "optional":
@@ -50,11 +83,11 @@ function type(s) {
                t = s.substring(i+2);
                if (!is_constant(t) && !is_variable(t)) {
                        // methods
-                       return "<a href=\"/" + s.replace(/::|\\/g, "/") + "\">";
+                       return "<a href=\"" + s.replace(/::|\\/g, "/") + "\">";
                }
        }
-       if (-1 !== (j = s.indexOf("\\"))) {
-               return "<a href=\"/" + s.replace(/\\/g, "/").replace(/::|$/, "#") + "\">";
+       if (-1 !== (j = s.indexOf("\\")) && s.substr(j+1,1) !== "n") {
+               return "<a href=\"" + s.replace(/\\/g, "/").replace(/::|$/, "#") + "\">";
        }
        
        switch (s.toLowerCase()) {
@@ -75,59 +108,75 @@ function type(s) {
                return "<span class=\"constant\">";
        }
 }
-function node(s) {
-       //console.log("node", s);
+
+function node(s, nn) {
+       //log("node", s);
        
        var t;
        
-       if ((t = type(s))) {
+       if ((t = type(s, nn))) {
                return $(t).text(s);
        }
        return document.createTextNode(s);
 }
-function wrap(n) {
+function wrap(n, nn) {
        var $n = $(n)
        var a = [];
 
-       $n.text().split(/([^a-zA-Z_\\\$:]+)/).forEach(function(v) {
-               a.push(node(v));
+       $n.text().split(/([^a-zA-Z0-9_\\\$:]+)/).forEach(function(v) {
+               a.push(node(v, nn));
        });
        $n.replaceWith(a);
 }
 function walk(i, e) {
-       //console.log("walk", i, e);
+       log("walk", i, e);
 
-       e && $.each(e.childNodes, function(i, n) {
-               //console.log(n.nodeName);
+       $.each($.makeArray(e.childNodes), function(i, n) {
                switch (n.nodeName) {
                case "A":
+               case "BR":
+               case "HR":
                        break;
                case "#text":
-                       wrap(n);
+                       wrap(n, e.nodeName);
                        break;
                default:
-                       walk(n);
+                       walk(-1, n);
+                       break;
                }
        });
 }
 
+function blink(c) {
+       var $c = $(c);
+       
+       $c.fadeOut("fast").queue(function(next) {
+               this.style.color = "red";
+               next();
+       }).fadeIn("fast").fadeOut("slow").queue(function(next) {
+               this.style.color = "";
+               next();
+       }).fadeIn("slow");
+}
+
 function hashchange() {
        if (location.hash.length > 1) {
                var hash = location.hash.substring(1);
+               var name = is_variable(hash) ? ".var" : ".constant";
+               var scrolled = false;
                
-               $(is_variable(hash) ? ".var" : ".constant").each(function(i, c) {
-                       
+               $(name).each(hash.substring(hash.length-1) === "_" ? function(i, c) {
+                       if (c.textContent.substring(0, hash.length) === hash) {
+                               if (!scrolled) {
+                                       $(window).scrollTop($(c).offset().top - 100);
+                                       scrolled = true;
+                               }
+                               blink(c);
+                       }
+               } : function(i, c) {
                        if (c.textContent === hash) {
-                               var $c = $(c);
-                               
-                               $(window).scrollTop($c.offset().top - 100);
-                               $c.fadeOut("slow").queue(function(next) {
-                                       this.style.color = "red";
-                                       next();
-                               }).fadeIn("fast").fadeOut("fast").queue(function(next) {
-                                       this.style.color = "";
-                                       next();
-                               }).fadeIn("slow");
+                               $(window).scrollTop($(c).offset().top - 100);
+                               blink(c);
                                return false;
                        }
                });