6334108c3888036c669315abd09891b4f0fb512c
[mdref/mdref] / index.js
1 function type(s) {
2
3 // nothing
4 if (!s.match(/[a-zA-Z]/)) {
5 return;
6 }
7
8 switch (s) {
9 // types
10 case "void":
11 case "bool":
12 case "int":
13 case "float":
14 case "string":
15 case "array":
16 case "object":
17 case "callable":
18 return "<code>";
19
20 // keywords
21 case "class":
22 case "interface":
23 case "namespace":
24 case "extends":
25 case "implements":
26 case "public":
27 case "protected":
28 case "private":
29 case "static":
30 case "final":
31 case "abstract":
32 // phrases
33 case "Optional":
34 case "optional":
35 return "<em>";
36 }
37
38 var is_namespace, is_method;
39
40 if ((is_method = (s.indexOf("::") !== -1)) || (is_namespace = (s.indexOf("\\") !== -1))) {
41 return "<a href=\"/" + s.replace(/::|\\/g, "/") + (is_method ? ".md":"") + "\">";
42 }
43
44 switch (s.toLowerCase()) {
45 // variables
46 default:
47 if (s.substring(0,1) !== "$") {
48 break;
49 }
50 // special constants
51 case "null":
52 case "true":
53 case "false":
54 return "<span class=\"var\">";
55 }
56
57 // constants
58 if (s.toUpperCase() === s) {
59 return "<code>";
60 }
61 }
62 function node(s) {
63 //console.log("node", s);
64
65 var t;
66
67 if ((t = type(s))) {
68 return $(t).text(s);
69 }
70 return document.createTextNode(s);
71 }
72 function wrap(n) {
73 var $n = $(n)
74 var a = [];
75
76 $n.text().split(/([^a-zA-Z_\\\$:]+)/).forEach(function(v) {
77 a.push(node(v));
78 });
79 $n.replaceWith(a);
80 }
81 function walk(i, e) {
82 //console.log("walk", i, e);
83
84 e && $.each(e.childNodes, function(i, n) {
85 //console.log(n.nodeName);
86 switch (n.nodeName) {
87 case "A":
88 break;
89 case "#text":
90 wrap(n);
91 break;
92 default:
93 walk(n);
94 }
95 });
96 }
97 $(document).ready(function() {
98 //console.log("ready");
99
100 $("h1,h2,h3,h4,h5,h6,p,li,code").each(walk);
101 });