X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=public%2Findex.js;h=7c5a82c24043d6d5da63e41c3dfb5b1b631bd4aa;hb=904fc60f05894d6207e9ae85abda8c94283f7d28;hp=e0f3ccf5eda23820818a7e2e73188c17797a59f9;hpb=d7b9f83d24b7af987618921f555c9e5e2c2f4b95;p=mdref%2Fmdref diff --git a/public/index.js b/public/index.js index e0f3ccf..7c5a82c 100644 --- a/public/index.js +++ b/public/index.js @@ -1,200 +1,207 @@ -function log() { - // console.log.apply(console, arguments); -} +"use strict"; -function is_constant(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() { + var mdref = { + log: function log() { + console.log.apply(console, arguments); + }, + is_constant: function is_constant(s) { + s = s.replace(/v\d+(_\d+)?$/, ""); + if (s.length < 2) { + return false; + } + return s.toUpperCase(s) === s; + }, + is_variable: function is_variable(s) { + return s.substring(0,1) === "$"; + }, + type: function type(s, nn) { + var i, j, t; + // mdref.log("type", s); + // nothing + if (!s.match(/[a-zA-Z]/)) { + return; + } -var is_in_string = false; + switch (s) { + // types + case "void": + case "bool": + case "int": + case "float": + case "string": + case "resource": + case "array": + case "object": + case "callable": + case "mixed": + // Zend/SPL + case "stdClass": + case "Exception": + case "ErrorException": + case "RuntimeException": + case "UnexpectedValueException": + case "DomainException": + case "InvalidArgumentException": + case "BadMethodCallException": + case "Closure": + case "Generator": + case "Countable": + case "Serializable": + case "Traversable": + case "Iterator": + case "IteratorAggregate": + case "ArrayAccess": + case "ArrayObject": + case "ArrayIterator": + case "RecursiveArrayIterator": + case "SeekableIterator": + case "SplObserver": + case "SplSubject": + case "SplObjectStorage": + case "JsonSerializable": + return ""; -function type(s, nn) { - var i, j, t; - //log("type", s); - // nothing - if (!s.match(/[a-zA-Z]/)) { - return; - } - - switch (s) { - // types - case "void": - case "bool": - case "int": - case "float": - case "string": - case "resource": - case "array": - case "object": - case "callable": - case "mixed": - // Zend/SPL - case "stdClass": - case "Exception": - case "ErrorException": - case "RuntimeException": - case "UnexpectedValueException": - case "DomainException": - case "InvalidArgumentException": - case "BadMethodCallException": - 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 ""; - - // keywords - case "is": - if (nn !== "H1") { - return; - } - case "extends": - case "implements": - if (nn === "H1") { - return "
 "; - } - case "class": - case "interface": - case "namespace": - case "public": - case "protected": - case "private": - case "static": - case "final": - case "abstract": - case "self": - case "parent": - // phrases - case "Optional": - case "optional": - return ""; - } - - // class members - if (-1 !== (i = s.indexOf("::"))) { - t = s.substring(i+2); - if (!is_constant(t) && !is_variable(t)) { - // methods - return ""; - } - } - if (-1 !== (j = s.indexOf("\\")) && s.substr(j+1,1) !== "n") { - return ""; - } - - switch (s.toLowerCase()) { - // variables - default: - if (!is_variable(s)) { - break; - } - // special constants - case "null": - case "true": - case "false": - return ""; - } - - // constants - if (is_constant(s)) { - return ""; - } -} + // keywords + case "is": + if (nn !== "H1") { + return; + } + case "extends": + case "implements": + if (nn === "H1") { + return "
 "; + } + case "class": + case "interface": + case "namespace": + case "public": + case "protected": + case "private": + case "static": + case "final": + case "abstract": + case "self": + case "parent": + // phrases + case "Optional": + case "optional": + return ""; + } -function node(s, nn) { - //log("node", s); - - var t; - - if ((t = type(s, nn))) { - return $(t).text(s); - } - return document.createTextNode(s); -} -function wrap(n, nn) { - var $n = $(n) - var a = []; + // class members + if (-1 !== (i = s.indexOf("::"))) { + t = s.substring(i+2); + if (!mdref.is_constant(t) && !mdref.is_variable(t)) { + // methods + return "
"; + } + } + if (-1 !== (j = s.lastIndexOf("\\")) && s.substr(j+1,1) !== "n") { + t = s.substring(j+1); + if (!mdref.is_constant(t) || s.match(/\\/).length <= 1) { + return ""; + } + return ""; + } - $n.text().split(/([^a-zA-Z0-9_\\\$:]+)/).forEach(function(v) { - a.push(node(v, nn)); - }); - $n.replaceWith(a); -} -function walk(i, e) { - log("walk", i, e); + switch (s.toLowerCase()) { + // variables + default: + if (!mdref.is_variable(s)) { + break; + } + // special constants + case "null": + case "true": + case "false": + return ""; + } - $.each($.makeArray(e.childNodes), function(i, n) { - switch (n.nodeName) { - case "A": - case "BR": - case "HR": - break; - case "#text": - wrap(n, e.nodeName); - break; - default: - walk(-1, n); - break; - } - }); -} + // constants + if (mdref.is_constant(s)) { + return ""; + } + }, + wrap: function wrap(n, nn) { + var $n = $(n) + var a = []; -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"); -} + $n.text().split(/([^a-zA-Z0-9_\\\$:]+)/).forEach(function(v) { + var t; + + if ((t = mdref.type(v.replace(/:$/, ""), nn))) { + a.push($(t).text(v)); + } else if (a.length && a[a.length-1].nodeName === "#text") { + /* if we already have a text node and the next is also gonna be a text + * node, then join them, becuase chrome v30+ or something eats whitespace + * for breakfast, lunch and dinner! + */ + a[a.length-1].textContent += v; + } else { + a.push(document.createTextNode(v)); + } + }); + $n.replaceWith(a); + }, + walk: function walk(i, e) { + // mdref.log("walk", i, e); -function hashchange() { - if (location.hash.length > 1) { - var hash = location.hash.substring(1); - var name = is_variable(hash) ? ".var" : ".constant"; - var scrolled = false; - - $(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; + $.each($.makeArray(e.childNodes), function(i, n) { + switch (n.nodeName) { + case "A": + case "BR": + case "HR": + case "EM": + case "CODE": + case "SPAN": + break; + case "#text": + mdref.wrap(n, e.nodeName); + break; + default: + mdref.walk(-1, n); + break; } - blink(c); - } - } : function(i, c) { - if (c.textContent === hash) { - $(window).scrollTop($(c).offset().top - 100); - blink(c); - return false; - } - }); - } -} + }); + }, + blink: function blink(c) { + var $c = $(c); -$(function() { - $("h1,h2,h3,h4,h5,h6,p,li,code").each(walk); - $(window).on("hashchange", hashchange); - hashchange(); + $c.fadeOut("fast").queue(function(next) { + this.style.color = "red"; + next(); + }).fadeIn("fast").fadeOut("slow").queue(function(next) { + this.style.color = ""; + next(); + }).fadeIn("slow"); + }, + hashchange: function hashchange() { + if (location.hash.length > 1) { + var hash = location.hash.substring(1); + var name = mdref.is_variable(hash) ? ".var" : ".constant"; + var scrolled = false; + + $(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; + } + mdref.blink(c); + } + } : function(i, c) { + if (c.textContent === hash) { + $(window).scrollTop($(c).offset().top - 100); + mdref.blink(c); + return false; + } + }); + } + } + }; + + $("h1,h2,h3,h4,h5,h6,p,li,code,td").each(mdref.walk); + $(window).on("hashchange", mdref.hashchange); + mdref.hashchange(); });