SyntaxHighlighter Evolved with highlight-range   1 comment

Posted at 7:08 pm in code,english,php,wordpress

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.

1

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;">

after beeing parsed by my (highlighted) changes

// Sanitize row highlights
if ( false != $atts['highlight'] ) {
@h@        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

Written by Anders Einar on July 7th, 2010

One Response to 'SyntaxHighlighter Evolved with highlight-range'

Subscribe to comments with RSS or TrackBack to 'SyntaxHighlighter Evolved with highlight-range'.

  1. Great idea! I’ll include it in the next version. :)

    Alex (Viper007Bond)

    11 Jul 10 at 04:59

Leave a Reply