-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcode_highlighter.js
More file actions
167 lines (140 loc) · 4.71 KB
/
code_highlighter.js
File metadata and controls
167 lines (140 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Code Highlighter
*
* A rewrite of Dan Webb's original Unobtrusive Code Highlighter. Refactored to
* take advantage of Prototype idioms and to clean out the cruft of legacy
* browser support.
*
* Tested on:
* Firefox 3+
* Safari 3+
*
*
* Usage:
* (1) Include this script on your page.
* (2) Include any number of language parsing definitions. Sample definitions
* are included.
* (3) Assign a class name to any code block you want highlighted. The class
* name should be identical to the language definition name passed to
* `CodeHighlighter.addStyle`.
* (4) Include a stylesheet that highlights your code. You can target parsed
* tokens by name: for example, `pre code .string` will highlight anything
* with a style rule named "string."
*
* Dan's script (and therefore _this_ script) was inspired by star-light,
* written by the cunning Dean Edwards (http://dean.edwards.name/star-light/).
*
*/
(function() {
var _styleSets = [];
var _codeElements;
var _rules = _makeRules();
var DEFAULT_TEMPLATE = new Template(
'<span class="#{className}">#{text}</span>');
var $Codes = Class.create(Enumerable, {
initialize: function(nodes) {
this.nodes = $A(nodes);
},
_each: function() {
return this.nodes._each.apply(this.nodes, arguments);
}
});
function _makeStyleSet(name, rules, ignoreCase, options) {
return {
name: name,
rules: rules,
ignoreCase: ignoreCase || false
};
}
function _makeRules(styleSet) {
var rules = [];
rules.toString = function() {
return this.pluck('exp').join('|');
};
if (styleSet) {
var exp, rule;
for (var ruleName in styleSet.rules) {
rule = styleSet.rules[ruleName];
if (!Object.isString(rule.exp)) {
exp = String(rule.exp).substr(1, String(rule.exp).length - 2);
} else {
exp = rule.exp;
}
rules.push({
className: ruleName,
exp: "(" + exp + ")",
length: (exp.match(/(^|[^\\])\([^?]/g) || "").length + 1,
replacement: rule.replacement || null
});
}
}
return rules;
}
function _parse(text, rules, ignoreCase) {
var re = new RegExp(rules, ignoreCase ? "gi" : "g");
return text.replace(re, function() {
var i = 0, j = 1, rule;
while (rule = rules[i++]) {
if (arguments[j]) {
// If no custom replacement is defined, do the simple replacement.
if (!rule.replacement) {
return DEFAULT_TEMPLATE.evaluate({
className: rule.className, text: arguments[0] });
} else {
// Replace $0 with the className; then do normal replaces.
var str = rule.replacement.replace("$0", rule.className);
for (var k = 1; k <= rule.length - 1; k++) {
str = str.replace("$" + k, arguments[j + k]);
}
return str;
}
} else {
j += rule.length;
}
}
});
}
function _highlightCode(styleSet) {
var parsed;
var stylableElements = _codeElements.select( function(code) {
return code.className.include(styleSet.name);
});
var rules = _makeRules(styleSet);
for (var i = 0, element; element = $(stylableElements[i]); i++) {
text = element.innerHTML;
// EVIL hack to fix IE whitespace badness if it's inside a <pre>
if (Prototype.Browser.IE && element.parentNode.nodeName === 'PRE') {
element = element.parentNode;
text = element.innerHTML;
parsed = text.replace(/(<code[^>]*>)([^<]*)<\/code>/i,
function() {
return arguments[1] + _parse(arguments[2], rules, styleSet.ignoreCase)
+ "</code>";
});
parsed = parsed.replace(/\n(\s*)/g, function() {
return "\n" + (" ".times(arguments[1].length));
});
parsed = parsed.replace(/\t/g,
" ".times(CodeHighlighter.TAB_SIZE));
parsed = parsed.replace(/\n(<\/\w+>)?/g, "<br />$1");
parsed = parsed.replace(/<br \/>[\n\r\s]*<br \/>/g, "<p><br /></p>");
} else {
parsed = _parse(text, rules, styleSet);
}
element.update(parsed);
}
}
function _initialize() {
_codeElements = new $Codes(document.getElementsByTagName('code'));
_styleSets.each(_highlightCode);
}
window.CodeHighlighter = {
TAB_SIZE: 2,
addStyle: function(name, rules, ignoreCase, options) {
_styleSets.push(_makeStyleSet(name, rules, ignoreCase, options));
if (_styleSets.length == 1) {
document.observe("dom:loaded", _initialize);
}
}
};
})();