WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/18/2008 11:38:21 PM (13 years ago)
Author:
ryan
Message:

Post revisions from mdawaffe. see #6775

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/pluggable.php

    r7642 r7747  
    13511351endif;
    13521352
     1353if ( !function_exists( 'wp_text_diff' ) ) :
     1354/**
     1355 * wp_text_diff() - compares two strings and outputs a human readable HTML representation of their difference
     1356 *
     1357 * Basically a wrapper for man diff(1)
     1358 *
     1359 * Must accept an optional third parameter, $args @see wp_parse_args()
     1360 *    (string) title: optional.  If present, titles the diff in a manner compatible with the output
     1361 *
     1362 * Must return the empty string if the two compared strings are found to be equivalent according to whatever metric
     1363 *
     1364 * @since 2.6
     1365 * @uses Text_Diff
     1366 * @uses WP_Text_Diff_Renderer_Table
     1367 *
     1368 * @param string $left_string "old" (left) version of string
     1369 * @param string $right_string "new" (right) version of string
     1370 * @param string|array $args @see wp_parse_args()
     1371 * @return string human readable HTML of string differences.  Empty string if strings are equivalent
     1372 */
     1373function wp_text_diff( $left_string, $right_string, $args = null ) {
     1374    $defaults = array( 'title' => '' );
     1375    $args = wp_parse_args( $args, $defaults );
     1376
     1377    // PEAR Text_Diff is lame; it includes things from include_path rather than it's own path.
     1378    // Not sure of the ramifications of disttributing modified code.
     1379    ini_set('include_path', '.' . PATH_SEPARATOR . ABSPATH . WPINC );
     1380
     1381    if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
     1382        require( ABSPATH . WPINC . '/wp-diff.php' );
     1383
     1384    // Normalize whitespace
     1385    $left_string  = trim($left_string);
     1386    $right_string = trim($right_string);
     1387    $left_string  = str_replace("\r", "\n", $left_string);
     1388    $right_string = str_replace("\r", "\n", $right_string);
     1389    $left_string  = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $left_string );
     1390    $right_string = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $right_string );
     1391   
     1392    $left_lines  = split("\n", $left_string);
     1393    $right_lines = split("\n", $right_string);
     1394
     1395    $text_diff = new Text_Diff($left_lines, $right_lines);
     1396    $renderer  = new WP_Text_Diff_Renderer_Table();
     1397    $diff = $renderer->render($text_diff);
     1398
     1399    ini_restore('include_path');
     1400
     1401    if ( !$diff )
     1402        return '';
     1403
     1404    $r  = "<table class='diff'>\n";
     1405    $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
     1406
     1407    if ( $args['title'] )
     1408        $r .= "<thead><tr><th colspan='4'>$args[title]</th></tr></thead>\n";
     1409
     1410    $r .= "<tbody>\n$diff\n</tbody>\n";
     1411    $r .= "</table>";
     1412
     1413    return $r;
     1414}
     1415endif;
     1416
    13531417?>
Note: See TracChangeset for help on using the changeset viewer.