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