Index: trunk/extensions/RSS/RSS.php |
— | — | @@ -138,7 +138,8 @@ |
139 | 139 | |
140 | 140 | # Get highlight terms from argument array |
141 | 141 | if ( isset( $args['highlight'] ) ) { |
142 | | - $this->highlight = self::explodeOnSpaces( $args['highlight'] ); |
| 142 | + # mapping to lowercase here so the regex can be case insensitive below. |
| 143 | + $this->highlight = array_flip( array_map( 'strtolower', self::explodeOnSpaces( $args['highlight'] ) ) ); |
143 | 144 | } |
144 | 145 | |
145 | 146 | # Get filter terms from argument array |
— | — | @@ -396,36 +397,31 @@ |
397 | 398 | return false; |
398 | 399 | } |
399 | 400 | |
| 401 | + static function highlightThis( $term, $match ) { |
| 402 | + $styleStart = "<span style='font-weight: bold; background: none repeat scroll 0%% 0%% rgb(%s); color: %s;'>"; |
| 403 | + $styleEnd = "</span>"; |
400 | 404 | |
401 | | - function highlightTerms( $text ) { |
402 | | - $i = 0; |
403 | | - $starttag = 'v8x5u3t3u8h'; |
404 | | - $endtag = 'q8n4f6n4n4x'; |
| 405 | + # bg colors cribbed from Google's highlighting of search teerms |
| 406 | + $bgcolor = array( '255, 255, 102', '160, 255, 255', '153, 255, 153', |
| 407 | + '255, 153, 153', '255, 102, 255', '136, 0, 0', '0, 170, 0', '136, 104, 0', |
| 408 | + '0, 70, 153', '153, 0, 153' ); |
| 409 | + # Spelling out the fg colors instead of using processing time to create this list |
| 410 | + $color = array("black", "black", "black", "black", "black", |
| 411 | + "white", "white", "white", "white", "white" ); |
405 | 412 | |
406 | | - $color[] = 'coral'; |
407 | | - $color[] = 'greenyellow'; |
408 | | - $color[] = 'lightskyblue'; |
409 | | - $color[] = 'gold'; |
410 | | - $color[] = 'violet'; |
411 | | - $count_color = count( $color ); |
| 413 | + $index = $term[strtolower($match[0])] % count( $bgcolor ); |
412 | 414 | |
413 | | - foreach ( $this->highlight as $term ) { |
414 | | - if ( $term ) { |
415 | | - $text = preg_replace( "/\b(\w*?" . preg_quote( $term, '/' ) . "\w*?)\b/i", "$starttag" . "_" . $i . "\\1$endtag", $text ); |
416 | | - $i++; |
417 | | - if ( $i == $count_color ) { |
418 | | - $i = 0; |
419 | | - } |
420 | | - } |
421 | | - } |
| 415 | + return sprintf($styleStart, $bgcolor[$index], $color[$index]). $match[0] .$styleEnd; |
| 416 | + } |
422 | 417 | |
423 | | - # To avoid trouble should someone wants to highlight the terms "span", "style", … |
424 | | - for ( $i = 0; $i < 5; $i++ ) { |
425 | | - $text = preg_replace( "/$starttag" . "_" . preg_quote( $i, '/' ) . "/", |
426 | | - "<span style=\"background-color:" . $color[$i] . "; font-weight: bold;\">", $text ); |
427 | | - $text = preg_replace( "|$endtag|", '</span>', $text ); |
| 418 | + function highlightTerms( $text ) { |
| 419 | + if ( count( $this->highlight ) === 0 ) { |
| 420 | + return $text; |
428 | 421 | } |
| 422 | + # SIGH ... anonymous functions are not available until 5.3 |
| 423 | + $f = create_function('$match', '$term = '.var_export($this->highlight, true).'; return RSS::highlightThis($term, $match);'); |
429 | 424 | |
430 | | - return $text; |
| 425 | + $highlight = '/'. implode( "|", array_map( 'preg_quote', array_keys( $this->highlight ) ) ) . '/i'; |
| 426 | + return preg_replace_callback( $highlight, $f, $text ); |
431 | 427 | } |
432 | 428 | } |
\ No newline at end of file |