Skip to content

Commit a433fe6

Browse files
committed
simplify and reduce duplication in syntax highlighter code
1 parent 29e90b8 commit a433fe6

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

root/static/js/syntaxhighlighter.js

+23-32
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ $(function () {
44
var all_lines = [];
55
for (var i = 0; i < lines.length; i++) {
66
var line = lines[i];
7-
var res = line.match(/^\s*(\d+)\s*(-\s*(\d+)\s*)?$/);
7+
var res = line.match(/^\s*(\d+)\s*(?:-\s*(\d+)\s*)?$/);
88
if (res) {
99
var start = res[1]*1;
10-
var end = (res[3] || res[1])*1;
10+
var end = (res[2] || res[1])*1;
1111
for (var l = start; l <= end; l++) {
1212
all_lines.push(l);
1313
}
@@ -16,6 +16,14 @@ $(function () {
1616
return all_lines;
1717
}
1818

19+
function findLines (el, lines) {
20+
var selector = $.map(
21+
parseLines(lines),
22+
function (i, line) { return '.number' + line }
23+
).join(', ');
24+
return el.find('.syntaxhighlighter .line').filter(selector);
25+
}
26+
1927
var hashLines = /^#L(\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*)$/;
2028

2129
// Allow tilde in url (#1118). Orig: /\w+:\/\/[\w-.\/?%&=:@;#]*/g,
@@ -92,36 +100,27 @@ $(function () {
92100
}
93101

94102
/* set perl as the default type in pod */
95-
$(".pod pre > code").each(function(index, source) {
103+
$(".pod pre > code").each(function(index, code) {
96104
var have_lang;
97-
if (source.className) {
98-
var classes = source.className.split(/\s+/);
99-
for (var i = 0; i < classes.length; i++) {
100-
if (classes[i].match(/^language-(.*)/)) {
101-
return;
102-
}
103-
}
105+
if (code.className && code.className.match(/(?:\s|^)language-\S+/)) {
106+
return;
104107
}
105-
source.className = 'language-perl';
108+
$(code).addClass('language-perl');
106109
});
107110

108-
$(".content pre > code").each(function(index, source) {
109-
var code = $(source);
110-
var pre = code.parent();
111+
$(".content pre > code").each(function(index, code) {
112+
var pre = $(code).parent();
111113

112114
var config = {
113115
'gutter' : false,
114116
'toolbar' : false,
115117
'quick-code' : false,
116118
'tab-size' : 8
117119
};
118-
if (source.className) {
119-
var classes = source.className.split(/\s+/);
120-
for (var i = 0; i < classes.length; i++) {
121-
var res = classes[i].match(/^language-(.*)/);
122-
if (res) {
123-
config.brush = res[1];
124-
}
120+
if (code.className) {
121+
var res = code.className.match(/(?:\s|^)language-(\S+)/)) {
122+
if (res) {
123+
config.brush = res[1];
125124
}
126125
}
127126
if (!config.brush) {
@@ -140,15 +139,11 @@ $(function () {
140139
config.highlight = parseLines(lines);
141140
}
142141

143-
SyntaxHighlighter.highlight(config, source);
142+
SyntaxHighlighter.highlight(config, code);
144143

145144
var pod_lines = pre.attr('data-pod-lines');
146145
if (pod_lines) {
147-
var selector = $.map(
148-
parseLines(pod_lines),
149-
function (e, i) { return '.number' + e }
150-
).join(', ');
151-
pre.find('.syntaxhighlighter .line').filter(selector).addClass('pod-line');
146+
findLines(pre, pod_lines).addClass('pod-line');
152147
}
153148
});
154149

@@ -180,12 +175,8 @@ $(function () {
180175
var lineMatch;
181176
if (lineMatch = document.location.hash.match(hashLines) ) {
182177
source.attr('data-line', lineMatch[1]);
183-
var selector = $.map(
184-
parseLines(lineMatch[1]),
185-
function (e, i) { return '.number' + e }
186-
).join(', ');
187178
source.find('.highlighted').removeClass('highlighted');
188-
source.find('.syntaxhighlighter .line').filter(selector).addClass('highlighted');
179+
findLines(source, lineMatch[1]).addClass('highlighted');
189180
}
190181
});
191182
}

0 commit comments

Comments
 (0)