bradyvercher / gistpress

WordPress plugin to add Gist oEmbed and shortcode support with caching.
GNU General Public License v2.0
143 stars 28 forks source link

Check line-highlighting logic #3

Closed GaryJones closed 11 years ago

GaryJones commented 11 years ago

See http://d.pr/i/caiS - not sure that the line highlighting logic is quite right for ranges, since lines 9 and 11 should also be highlighted in the screenshot.

Debug comes out as:

highlight: Array
(
[0] => 1
[2] => 5
[1] => 10
[3] => 12
[4] => 13
)

(Using my rewritten dev branch, which adds a couple of trim()s in, so might be something I've screwed up.)

GaryJones commented 11 years ago

The problem seems to be related to the way the range() array is added to the existing $highlight array, with values under duplicate keys being dropped. Suspect an array_push() is needed here.

GaryJones commented 11 years ago

Consider this issue solved - will close when committed.

    public function parse_highlight_arg( $line_numbers ) {
        if ( empty( $line_numbers ) ) {
            return null;
        }

        // Determine which lines should be highlighted.
        $highlight = array_map('trim', explode( ',', $line_numbers ));

        // Convert any ranges.
        foreach ( $highlight as $key => $num ) {
            if ( false !== strpos( $num, '-' ) ) {
                unset( $highlight[ $key ] );

                $range = array_map( 'trim', explode( '-', $num ) );
                foreach ( range( $range[0], $range[1] ) as $line )
                    array_push($highlight, $line);
            }
        }
        return array_unique( $highlight );
    }