SyntaxHighlighter Evolved with highlight-range
Here be dragons! This post is more than three years old. The information and advice it presents may be deprecated, wrong, or unvise. Thread with cation.
Update: As of SyntaxHighlighter Evolved v. 3.0.0 this functionality is included in the plugin.
One of the first plugins i installed for this brand new WordPress
was
SyntaxHighlighter Evolved. I love to code, and knew i would find
a use for it. To test it i used a large C++ file
from and
began testing.cpp.snippets.org
Highlight
worked fine, but i i didn’t support
highlighting a range of lines. This might not be so useful,
as you usually want to highlight small sections, but i rewrote
syntaxhighlighter.php to support ranges.
This expandes into
after beeing parsed by my (line 8-21,25) changes
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
// Sanitize row highlights
if ( false != $atts['highlight'] ) {
if ( false === strpos( $atts['highlight'], ',' ) && false === strpos( $atts['highlight'], '-' ) ) {
$atts['highlight'] = (int) $atts['highlight'];
} else {
$highlights = explode( ',', $atts['highlight'] );
foreach ( $highlights as $key => $highlight ) {
// if this value is a range
if (FALSE !== strpos($highlight, '-',1)) {
// we require 1 digit before the dash,
// if not we ignore it and pass it on
$range = explode('-', $highlight);
// Around here we should probably try to cast
// to int and if-check to sanitize the values
// loop over the range and add it to highlights
for(;$range[0] <= $range[1]; $range[0]++){
$highlights[] = $range[0];
}
// unset the current (range-type) highlight
unset($highlights[$key]);
} else {
$highlights[$key] = (int) $highlight;
if ( empty($highlights[$key]) )
unset($highlights[$key]);
}
}
$atts['highlight'] = implode( ',', $highlights );
}
}
New with
highlight-range-code based on SyntaxHighlighter Evolved version 2.3.8syntaxhighlighter.php