diff --git a/example/a.txt b/example/a.txt index 6f3897b2..f3b0f7c2 100644 --- a/example/a.txt +++ b/example/a.txt @@ -1,13 +1,15 @@ - - - Hello World! - - -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ + + Hello World! + + +

Příliš žluťoučký kůň úpěl ďábelské ódy

-

A heading we'll be removing

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

-

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

- +

A heading we'll be removing

+ +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ \ No newline at end of file diff --git a/example/b.txt b/example/b.txt index 5918964d..65992241 100644 --- a/example/b.txt +++ b/example/b.txt @@ -1,14 +1,16 @@ - - - Goodbye Cruel World! - - -

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ + + Goodbye Cruel World! + + +

Příliš žluťoučký kůň úpěl ďábelské ódy -- previous sentence is a pangram for Czech language (see http://en.wikipedia.org/wiki/Pangram)

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

-

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

-

Just a small amount of new text...

- +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ +

Just a small amount of new text...

+ \ No newline at end of file diff --git a/example/example.php b/example/example.php index 234bc2c8..f7a0c8de 100644 --- a/example/example.php +++ b/example/example.php @@ -1,69 +1,80 @@ + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> - - - PHP LibDiff - Examples - - - -

PHP LibDiff - Examples

-
- + + PHP LibDiff - Examples + + + + +

PHP LibDiff - Examples

+

Theme: | + '.$theme_dir.' | '; + } + } + ?>

+
- // Include the diff class - require_once dirname(__FILE__).'/../lib/Diff.php'; + true, - //'ignoreCase' => true, - ); + // Options for generating the diff + $options = array( + 'context' => 1, + //'ignoreWhitespace' => true, + //'ignoreCase' => true, + //'title_a' => 'some other title than "Old Version"', + //'title_b' => 'some other title than "New Version"', + ); - // Initialize the diff class - $diff = new Diff($a, $b, $options); + // Initialize the diff class + $diff = new Diff_Diff($a, $b, $options); - ?> -

Side by Side Diff

- +

Side by Side Diff

+ Render($renderer); + // Generate a side by side diff + echo $diff->render('Html_SideBySide'); - ?> -

Inline Diff

- +

Inline Diff

+ render($renderer); + // Generate an inline diff + echo $diff->render('Html_Inline'); - ?> -

Unified Diff

-

+    

Unified Diff

+
render($renderer));
+    // Generate a unified diff
+    echo htmlspecialchars($diff->render('Text_Unified'));
 
-		?>
-		
-

Context Diff

-

+    
+

Context Diff

+
render($renderer));
-		?>
-		
- + // Generate a context diff + echo htmlspecialchars($diff->render('Text_Context')); + ?> +
+ \ No newline at end of file diff --git a/example/styles.css b/example/themes/default/php-diff.css similarity index 100% rename from example/styles.css rename to example/themes/default/php-diff.css diff --git a/example/themes/default/screenshot.png b/example/themes/default/screenshot.png new file mode 100644 index 00000000..084d4a79 Binary files /dev/null and b/example/themes/default/screenshot.png differ diff --git a/example/themes/jdlx/context-break.png b/example/themes/jdlx/context-break.png new file mode 100644 index 00000000..56109ad5 Binary files /dev/null and b/example/themes/jdlx/context-break.png differ diff --git a/example/themes/jdlx/php-diff.css b/example/themes/jdlx/php-diff.css new file mode 100644 index 00000000..7e59a336 --- /dev/null +++ b/example/themes/jdlx/php-diff.css @@ -0,0 +1,134 @@ +/** +* php-diff theme jdlx +* @author http://rexdev.de +* @see https://github.com/jdlx +*/ + +body { + background: none repeat scroll 0 0 silver; + font-family: Lucida Grande,Helvetica,Arial,sans-serif; + font-size: 12px; + color:#404040; + text-shadow: 0 1px 1px rgba(255, 255, 255, 0.9); + margin: 0; + padding: 10px; + } +hr { + height:0; + background:none; + border:0; + border-bottom:1px solid gray; + box-shadow: 0 3px 4px rgba(0, 0, 0, 1); + margin:20px 0 20px 0; + } +#Info-Block { + margin-left: 4.5em; + } +.Differences { + border-collapse: collapse; + border-spacing: 0; + empty-cells: show; + width: 100%; + border-radius: 4px 4px 4px 4px; + box-shadow: 0 1px 10px rgba(0, 0, 0, 0.8); + color:black; + } +.Differences thead { + background: url("top-edge.png") repeat-x scroll 0 100% #efefef; + } +.Differences tbody { + background: none repeat scroll 0 0 white; + } +.Differences thead th { + color: #000000; + height: 3em; + padding-left: 4.5em; + text-align: left; + } +.Differences tbody th { + background: none repeat scroll 0 0 #ECECEC; + border-bottom: 1px solid silver; + border-right: 1px solid #CFCFCF; + color: silver; + font-size: 12px; + padding: 1px 2px; + text-align: right; + vertical-align: top; + width: 4em; + } +.Differences tbody.Skipped th { + border-right: 0 none; + } +.Differences td { + font-family: monospace; + font-size: 12px; + padding: 1px 2px; + } +.Differences tbody.ChangeReplace { + border-bottom: 1px solid #A0B9E3; + border-top: 1px solid #A0B9E3; + } +.Differences tbody.ChangeInsert { + background: url("shadow.png") repeat-x scroll 0 0 transparent; + } +.Differences tbody.ChangeDelete { + background: url("shadow.png") repeat-x scroll 0 0 transparent; + } +.DifferencesSideBySide .ChangeInsert td.Left { + } +.DifferencesSideBySide .ChangeInsert td.Right { + background: none repeat scroll 0 0 #CCFFCC; + } +.DifferencesSideBySide .ChangeDelete td.Left { + background: none repeat scroll 0 0 #F9DADA; + } +.DifferencesSideBySide .ChangeDelete td.Right { + } +.DifferencesSideBySide .ChangeReplace .Left { + background: none repeat scroll 0 0 #F1F4FA; + } +.DifferencesSideBySide .ChangeReplace .Right { + background: none repeat scroll 0 0 #F1F4FA; + } +.Differences ins, .Differences del { + text-decoration: none; + } +.DifferencesSideBySide .ChangeReplace ins, .DifferencesSideBySide .ChangeReplace del { + background: none repeat scroll 0 0 #C8D5ED; + } +.Differences .Skipped { + background: url("context-break.png") repeat-x scroll 0 50% white; + height: 30px; + } +.Differences .Skipped tr th { + background-color: #ECECEC; + opacity: 0; + } +.DifferencesInline .ChangeReplace .Left, .DifferencesInline .ChangeDelete .Left { + background: none repeat scroll 0 0 #FFDDDD; + } +.DifferencesInline .ChangeReplace .Right, .DifferencesInline .ChangeInsert .Right { + background: none repeat scroll 0 0 #DDFFDD; + } +.DifferencesInline .ChangeReplace ins { + background: none repeat scroll 0 0 #99EE99; + } +.DifferencesInline .ChangeReplace del { + background: none repeat scroll 0 0 #EE9999; + } +pre { + overflow: auto; + width: 98%; + background:#fff; + color:#000; + padding:1%; + border-radius: 4px 4px 4px 4px; + box-shadow: 0 1px 10px rgba(0, 0, 0, 0.8); + } +h2 { + margin-top:30px; + } +a,a:visited { + color:#d11803; + text-decoration:none; + } \ No newline at end of file diff --git a/example/themes/jdlx/screenshot.png b/example/themes/jdlx/screenshot.png new file mode 100644 index 00000000..b9353c81 Binary files /dev/null and b/example/themes/jdlx/screenshot.png differ diff --git a/example/themes/jdlx/shadow.png b/example/themes/jdlx/shadow.png new file mode 100644 index 00000000..8ece95a0 Binary files /dev/null and b/example/themes/jdlx/shadow.png differ diff --git a/example/themes/jdlx/top-edge.png b/example/themes/jdlx/top-edge.png new file mode 100644 index 00000000..9bf2d3fc Binary files /dev/null and b/example/themes/jdlx/top-edge.png differ diff --git a/lib/Diff.php b/lib/Diff/Diff.php similarity index 86% rename from lib/Diff.php rename to lib/Diff/Diff.php index 35305c03..f8887d68 100644 --- a/lib/Diff.php +++ b/lib/Diff/Diff.php @@ -43,7 +43,7 @@ * @link http://github.com/chrisboulton/php-diff */ -class Diff +class Diff_Diff { /** * @var array The "old" sequence to use as the basis for the comparison. @@ -67,13 +67,15 @@ class Diff 'context' => 3, 'ignoreNewLines' => false, 'ignoreWhitespace' => false, - 'ignoreCase' => false + 'ignoreCase' => false, + 'title_a'=>'Old Version', + 'title_b'=>'New Version' ); /** * @var array Array of the options that have been applied for generating the diff. */ - private $options = array(); + public $options = array(); /** * The constructor. @@ -92,15 +94,36 @@ public function __construct($a, $b, $options=array()) /** * Render a diff using the supplied rendering class and return it. * - * @param object $renderer An instance of the rendering object to use for generating the diff. + * @param string|object $renderer An instance of the rendering object to use for generating the diff. * @return mixed The generated diff. Exact return value depends on the rendered. */ - public function render(Diff_Renderer_Abstract $renderer) + public function render($renderer) { + if (!is_object($renderer)) + { + $renderer = 'Diff_Renderer_' . $renderer; + $renderer = new $renderer(); + } + $renderer->diff = $this; return $renderer->render(); } + /** + * Render a diff using the named rendering class and return it. + * + * @param string $renderer An instance of the rendering object to use for generating the diff. + * @return mixed The generated diff. Exact return value depends on the rendered. + */ + public function renderText($renderer) + { + $renderer = 'Diff_Renderer_Text_' . $renderer; + $renderer = new $renderer(); + + $renderer->diff = $this; + return $renderer->render(); + } + /** * Get a range of lines from $start to $end from the first comparison string * and return them as an array. If no values are supplied, the entire string @@ -168,9 +191,8 @@ public function getGroupedOpcodes() return $this->groupedCodes; } - require_once dirname(__FILE__).'/Diff/SequenceMatcher.php'; $sequenceMatcher = new Diff_SequenceMatcher($this->a, $this->b, null, $this->options); - $this->groupedCodes = $sequenceMatcher->getGroupedOpcodes(); + $this->groupedCodes = $sequenceMatcher->getGroupedOpcodes($this->options['context']); return $this->groupedCodes; } } \ No newline at end of file diff --git a/lib/Diff/Renderer/Html/Array.php b/lib/Diff/Renderer/Html/Array.php index 0b3f1d50..32949d79 100644 --- a/lib/Diff/Renderer/Html/Array.php +++ b/lib/Diff/Renderer/Html/Array.php @@ -40,8 +40,6 @@ * @link http://github.com/chrisboulton/php-diff */ -require_once dirname(__FILE__).'/../Abstract.php'; - class Diff_Renderer_Html_Array extends Diff_Renderer_Abstract { /** diff --git a/lib/Diff/Renderer/Html/Inline.php b/lib/Diff/Renderer/Html/Inline.php index 60e8005a..718b4de3 100644 --- a/lib/Diff/Renderer/Html/Inline.php +++ b/lib/Diff/Renderer/Html/Inline.php @@ -40,8 +40,6 @@ * @link http://github.com/chrisboulton/php-diff */ -require_once dirname(__FILE__).'/Array.php'; - class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array { /** @@ -53,6 +51,9 @@ class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array public function render() { $changes = parent::render(); + $title_a = $this->diff->options['title_a']; + $title_b = $this->diff->options['title_b']; + $html = ''; if(empty($changes)) { return $html; @@ -61,8 +62,8 @@ public function render() $html .= ''; $html .= ''; $html .= ''; - $html .= ''; - $html .= ''; + $html .= ''; + $html .= ''; $html .= ''; $html .= ''; $html .= ''; @@ -128,8 +129,8 @@ public function render() foreach($change['changed']['lines'] as $no => $line) { $toLine = $change['changed']['offset'] + $no + 1; $html .= ''; - $html .= ''; $html .= ''; + $html .= ''; $html .= ''; $html .= ''; } diff --git a/lib/Diff/Renderer/Html/SideBySide.php b/lib/Diff/Renderer/Html/SideBySide.php index 307af1c3..ee8a4fb6 100644 --- a/lib/Diff/Renderer/Html/SideBySide.php +++ b/lib/Diff/Renderer/Html/SideBySide.php @@ -40,8 +40,6 @@ * @link http://github.com/chrisboulton/php-diff */ -require_once dirname(__FILE__).'/Array.php'; - class Diff_Renderer_Html_SideBySide extends Diff_Renderer_Html_Array { /** @@ -53,6 +51,8 @@ class Diff_Renderer_Html_SideBySide extends Diff_Renderer_Html_Array public function render() { $changes = parent::render(); + $title_a = $this->diff->options['title_a']; + $title_b = $this->diff->options['title_b']; $html = ''; if(empty($changes)) { @@ -62,8 +62,8 @@ public function render() $html .= '
OldNew'.$title_a.''.$title_b.'Differences
'.$toLine.' '.$toLine.''.$line.'
'; $html .= ''; $html .= ''; - $html .= ''; - $html .= ''; + $html .= ''; + $html .= ''; $html .= ''; $html .= ''; foreach($changes as $i => $blocks) { diff --git a/lib/Diff/Renderer/Text/Context.php b/lib/Diff/Renderer/Text/Context.php index 0f672f91..b9ccc137 100644 --- a/lib/Diff/Renderer/Text/Context.php +++ b/lib/Diff/Renderer/Text/Context.php @@ -40,8 +40,6 @@ * @link http://github.com/chrisboulton/php-diff */ -require_once dirname(__FILE__).'/../Abstract.php'; - class Diff_Renderer_Text_Context extends Diff_Renderer_Abstract { /** diff --git a/lib/Diff/Renderer/Text/Unified.php b/lib/Diff/Renderer/Text/Unified.php index e94d951d..8c4b13cf 100644 --- a/lib/Diff/Renderer/Text/Unified.php +++ b/lib/Diff/Renderer/Text/Unified.php @@ -40,8 +40,6 @@ * @link http://github.com/chrisboulton/php-diff */ -require_once dirname(__FILE__).'/../Abstract.php'; - class Diff_Renderer_Text_Unified extends Diff_Renderer_Abstract { /** diff --git a/lib/Diff/SequenceMatcher.php b/lib/Diff/SequenceMatcher.php index e819e810..399b22d5 100644 --- a/lib/Diff/SequenceMatcher.php +++ b/lib/Diff/SequenceMatcher.php @@ -119,7 +119,7 @@ public function setSequences($a, $b) public function setSeq1($a) { if(!is_array($a)) { - $a = str_split($a); + $a = preg_split('//u', $a, -1, PREG_SPLIT_NO_EMPTY); } if($a == $this->a) { return; @@ -139,7 +139,7 @@ public function setSeq1($a) public function setSeq2($b) { if(!is_array($b)) { - $b = str_split($b); + $b = preg_split('//u', $b, -1, PREG_SPLIT_NO_EMPTY); } if($b == $this->b) { return; @@ -729,7 +729,7 @@ private function tupleSort($a, $b) } } - if(count($a) == $count($b)) { + if(count($a) == count($b)) { return 0; } else if(count($a) < count($b)) {
Old VersionNew Version'.$title_a.''.$title_b.'