diff --git a/example/dark-theme.css b/example/dark-theme.css index fec7395..cd151ac 100644 --- a/example/dark-theme.css +++ b/example/dark-theme.css @@ -138,6 +138,19 @@ a, a:visited { color: #272822; } +.DifferencesUnified .ChangeIgnore .Left, +.DifferencesUnified .ChangeIgnore .Right { + background: #FBF2BF; +} + +.DifferencesUnified .ChangeIgnore .Left.Ignore { + background: #4B4C57; +} + +.DifferencesUnified .ChangeIgnore .Right.Ignore { + background: #4B4C57; +} + /* * HTML Merged Diff */ @@ -166,3 +179,7 @@ a, a:visited { .DifferencesMerged th.ChangeDelete { background-image: linear-gradient(-45deg, #AAAAAA 0%, #EE9999 100%); } + +.DifferencesMerged th.ChangeIgnore { + background-image: linear-gradient(-45deg, #CCCCCC 0%, #4B4C57 100%); +} diff --git a/example/styles.css b/example/styles.css index 57af672..a67d04a 100644 --- a/example/styles.css +++ b/example/styles.css @@ -121,6 +121,19 @@ pre { background: #EE9999; } +.DifferencesUnified .ChangeIgnore .Left, +.DifferencesUnified .ChangeIgnore .Right { + background: #FBF2BF; +} + +.DifferencesUnified .ChangeIgnore .Left.Ignore { + background: #F7F7F7; +} + +.DifferencesUnified .ChangeIgnore .Right.Ignore { + background: #F7F7F7; +} + /* * HTML Merged Diff */ @@ -145,3 +158,7 @@ pre { .DifferencesMerged th.ChangeDelete { background-image: linear-gradient(-45deg, #CCCCCC 0%, #EE9999 100%); } + +.DifferencesMerged th.ChangeIgnore { + background-image: linear-gradient(-45deg, #CCCCCC 0%, #F7F7F7 100%); +} diff --git a/lib/jblond/Diff/Renderer/Html/Merged.php b/lib/jblond/Diff/Renderer/Html/Merged.php index b55889b..dfd89bf 100644 --- a/lib/jblond/Diff/Renderer/Html/Merged.php +++ b/lib/jblond/Diff/Renderer/Html/Merged.php @@ -43,6 +43,10 @@ class Merged extends MainRenderer implements SubRendererInterface * @var string last block of lines which where removed from version 2. */ private $lastDeleted; + /** + * @var string + */ + private $headerClass = ''; /** * Merged constructor. @@ -101,21 +105,17 @@ public function generateBlockHeader(array $changes): string */ public function generateSkippedLines(): string { - $marker = '…'; - $headerClass = ''; - - if ($this->lastDeleted !== null) { - $headerClass = 'ChangeDelete'; - } - - $this->lastDeleted = null; - - return << - $marker + … … HTML; + + $this->headerClass = ''; + $this->lastDeleted = null; + + return $html; } /** @@ -125,22 +125,20 @@ public function generateSkippedLines(): string */ public function generateLinesEqual(array $changes): string { - $html = ''; - $headerClass = ''; + $html = ''; foreach ($changes['base']['lines'] as $lineNo => $line) { $fromLine = $changes['base']['offset'] + $lineNo + 1 + $this->lineOffset; - if (!$lineNo && $this->lastDeleted !== null) { - $headerClass = 'ChangeDelete'; - } - $html .= << - $fromLine + $fromLine $line HTML; + $this->lastDeleted = null; + $this->headerClass = ''; } return $html; @@ -153,22 +151,20 @@ public function generateLinesEqual(array $changes): string */ public function generateLinesInsert(array $changes): string { - $html = ''; - $headerClass = ''; + $html = ''; - foreach ($changes['changed']['lines'] as $lineNo => $line) { + foreach ($changes['changed']['lines'] as $line) { $this->lineOffset++; $toLine = $changes['base']['offset'] + $this->lineOffset; - if (!$lineNo && $this->lastDeleted !== null) { - $headerClass = 'ChangeDelete'; - } $html .= << - $toLine + $toLine $line HTML; + + $this->headerClass = ''; $this->lastDeleted = null; } @@ -197,6 +193,7 @@ public function generateLinesDelete(array $changes): string } $this->lastDeleted = $title; + $this->headerClass = 'ChangeDelete'; return ''; } @@ -208,14 +205,10 @@ public function generateLinesDelete(array $changes): string */ public function generateLinesReplace(array $changes): string { - $html = ''; - $headerClass = ''; + $html = ''; foreach ($changes['base']['lines'] as $lineNo => $line) { $fromLine = $changes['base']['offset'] + $lineNo + 1 + $this->lineOffset; - if (!$lineNo && $this->lastDeleted !== null) { - $headerClass = 'ChangeDelete'; - } // Capture added parts. $addedParts = []; @@ -236,10 +229,11 @@ function ($removedParts) use ($addedParts) { $html .= << - $fromLine + $fromLine $line HTML; + $this->headerClass = ''; $this->lastDeleted = null; } @@ -265,4 +259,30 @@ public function generateDiffFooter(): string { return ''; } + + /** + * @inheritDoc + * + * @return string Modified text. + */ + public function generateLinesIgnore(array $changes): string + { + $baseLineCount = count($changes['base']['lines']); + $changedLineCount = count($changes['changed']['lines']); + + $this->lineOffset -= $baseLineCount; + + $title = "Lines ignored at {$this->options['title2']}: "; + $title .= $changes['changed']['offset'] + 1 . '-' . ($changes['changed']['offset'] + $changedLineCount); + + if ($baseLineCount > $changedLineCount) { + $title = "Lines ignored at {$this->options['title1']}: "; + $title .= $changes['base']['offset'] + 1 . '-' . ($changes['base']['offset'] + $baseLineCount); + } + + $this->lastDeleted = $title; + $this->headerClass = 'ChangeIgnore'; + + return ''; + } } diff --git a/lib/jblond/Diff/Renderer/Html/Unified.php b/lib/jblond/Diff/Renderer/Html/Unified.php index 1569e83..537681d 100644 --- a/lib/jblond/Diff/Renderer/Html/Unified.php +++ b/lib/jblond/Diff/Renderer/Html/Unified.php @@ -219,6 +219,44 @@ public function generateLinesReplace(array $changes): string return $html; } + /** + * @inheritDoc + * + * @return string Html code representing table rows showing modified text. + */ + public function generateLinesIgnore(array $changes): string + { + $html = ''; + + foreach ($changes['base']['lines'] as $lineNo => $line) { + $fromLine = $changes['base']['offset'] + $lineNo + 1; + $html .= << + $fromLine + + + $line + + +HTML; + } + + foreach ($changes['changed']['lines'] as $lineNo => $line) { + $toLine = $changes['changed']['offset'] + $lineNo + 1; + $html .= << + + $toLine + + $line + + +HTML; + } + + return $html; + } + /** * @inheritDoc * diff --git a/lib/jblond/Diff/Renderer/Text/Context.php b/lib/jblond/Diff/Renderer/Text/Context.php index 7b50dc3..e8b8228 100644 --- a/lib/jblond/Diff/Renderer/Text/Context.php +++ b/lib/jblond/Diff/Renderer/Text/Context.php @@ -65,6 +65,7 @@ public function render() // Line differences between versions or lines of version 1 are removed from version 2. // Add all operations to diff-view of version 1, except for insert. $filteredGroups = $this->filterGroups($group, 'insert'); + $filteredGroups = $this->filterGroups($filteredGroups, 'ignore'); foreach ($filteredGroups as [$tag, $start1, $end1, $start2, $end2]) { $diff .= $this->tagMap[$tag] . ' ' . implode( @@ -81,6 +82,7 @@ public function render() // Line differences between versions or lines are inserted into version 2. // Add all operations to diff-view of version 2, except for delete. $filteredGroups = $this->filterGroups($group, 'delete'); + $filteredGroups = $this->filterGroups($filteredGroups, 'ignore'); foreach ($filteredGroups as [$tag, $start1, $end1, $start2, $end2]) { $diff .= $this->tagMap[$tag] . ' ' . implode(