docs & typos
[mdref/mdref] / public / index.js
1 "use strict";
2
3 $(function() {
4 var mdref = {
5 log: function log() {
6 console.log.apply(console, arguments);
7 },
8 is_constant: function is_constant(s) {
9 s = s.replace(/v\d+(_\d+)?$/, "");
10 if (s.length < 2) {
11 return false;
12 }
13 return s.toUpperCase(s) === s;
14 },
15 is_variable: function is_variable(s) {
16 return s.substring(0,1) === "$";
17 },
18 type: function type(s, nn) {
19 var i, j, t;
20 // mdref.log("type", s);
21 // nothing
22 if (!s.match(/[a-zA-Z]/)) {
23 return;
24 }
25
26 switch (s) {
27 // types
28 case "void":
29 case "bool":
30 case "int":
31 case "float":
32 case "string":
33 case "resource":
34 case "array":
35 case "object":
36 case "callable":
37 case "mixed":
38 // Zend/SPL
39 case "stdClass":
40 case "Exception":
41 case "ErrorException":
42 case "RuntimeException":
43 case "UnexpectedValueException":
44 case "DomainException":
45 case "InvalidArgumentException":
46 case "BadMethodCallException":
47 case "Closure":
48 case "Generator":
49 case "Countable":
50 case "Serializable":
51 case "Traversable":
52 case "Iterator":
53 case "IteratorAggregate":
54 case "ArrayAccess":
55 case "ArrayObject":
56 case "ArrayIterator":
57 case "RecursiveArrayIterator":
58 case "SplObserver":
59 case "SplSubject":
60 case "SplObjectStorage":
61 return "<code>";
62
63 // keywords
64 case "is":
65 if (nn !== "H1") {
66 return;
67 }
68 case "extends":
69 case "implements":
70 if (nn === "H1") {
71 return "<br>&nbsp;<em>";
72 }
73 case "class":
74 case "interface":
75 case "namespace":
76 case "public":
77 case "protected":
78 case "private":
79 case "static":
80 case "final":
81 case "abstract":
82 case "self":
83 case "parent":
84 // phrases
85 case "Optional":
86 case "optional":
87 return "<em>";
88 }
89
90 // class members
91 if (-1 !== (i = s.indexOf("::"))) {
92 t = s.substring(i+2);
93 if (!mdref.is_constant(t) && !mdref.is_variable(t)) {
94 // methods
95 return "<a href=\"" + s.replace(/::|\\/g, "/") + "\">";
96 }
97 }
98 if (-1 !== (j = s.indexOf("\\")) && s.substr(j+1,1) !== "n") {
99 return "<a href=\"" + s.replace(/\\/g, "/").replace(/::|$/, "#") + "\">";
100 }
101
102 switch (s.toLowerCase()) {
103 // variables
104 default:
105 if (!mdref.is_variable(s)) {
106 break;
107 }
108 // special constants
109 case "null":
110 case "true":
111 case "false":
112 return "<span class=\"var\">";
113 }
114
115 // constants
116 if (mdref.is_constant(s)) {
117 return "<span class=\"constant\">";
118 }
119 },
120 node: function node(s, nn) {
121 // mdref.log("node", s);
122 var t;
123 if ((t = mdref.type(s, nn))) {
124 return $(t).text(s);
125 }
126 return document.createTextNode(s);
127 },
128 wrap: function wrap(n, nn) {
129 var $n = $(n)
130 var a = [];
131
132 $n.text().split(/([^a-zA-Z0-9_\\\$:]+)/).forEach(function(v) {
133 a.push(mdref.node(v, nn));
134 });
135 $n.replaceWith(a);
136 },
137 walk: function walk(i, e) {
138 // mdref.log("walk", i, e);
139
140 $.each($.makeArray(e.childNodes), function(i, n) {
141 switch (n.nodeName) {
142 case "A":
143 case "BR":
144 case "HR":
145 break;
146 case "#text":
147 mdref.wrap(n, e.nodeName);
148 break;
149 default:
150 mdref.walk(-1, n);
151 break;
152 }
153 });
154 },
155 blink: function blink(c) {
156 var $c = $(c);
157
158 $c.fadeOut("fast").queue(function(next) {
159 this.style.color = "red";
160 next();
161 }).fadeIn("fast").fadeOut("slow").queue(function(next) {
162 this.style.color = "";
163 next();
164 }).fadeIn("slow");
165 },
166 hashchange: function hashchange() {
167 if (location.hash.length > 1) {
168 var hash = location.hash.substring(1);
169 var name = mdref.is_variable(hash) ? ".var" : ".constant";
170 var scrolled = false;
171
172 $(name).each(hash.substring(hash.length-1) === "_" ? function(i, c) {
173 if (c.textContent.substring(0, hash.length) === hash) {
174 if (!scrolled) {
175 $(window).scrollTop($(c).offset().top - 100);
176 scrolled = true;
177 }
178 mdref.blink(c);
179 }
180 } : function(i, c) {
181 if (c.textContent === hash) {
182 $(window).scrollTop($(c).offset().top - 100);
183 mdref.blink(c);
184 return false;
185 }
186 });
187 }
188 }
189 };
190
191 $("h1,h2,h3,h4,h5,h6,p,li,code").each(mdref.walk);
192 $(window).on("hashchange", mdref.hashchange);
193 mdref.hashchange();
194 });