SyntaxHighlighter Evolved with highlight-range

Written on Wednesday, 2010-07-07 17:08. Tagged code, english, php, and wordpress

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 cpp.snippets.org and began testing.

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.

[code language="php" firstline="740" gutter="true" highlight="748-761,765"][/code]

This expandes into

<pre class="brush: php; auto-links: false; first-line: 740; gutter: true;
highlight: [765,748,749,750,751,752,753,754,755,756,757,758,759,760,761];
html-script: false; light: false; pad-line-numbers: false;
smart-tabs: true; tab-size: 4; toolbar: true; wrap-lines: true;"></pre>

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 syntaxhighlighter.php with highlight-range-code based on SyntaxHighlighter Evolved version 2.3.8

See also Syntaxhighlighter Evolved with inline-highlight


Activate comments