Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corrects the column placement of line numbers for right side replacements #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions example/a.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<title>Hello World!</title>
</head>
<body>
<p>Příliš žluťoučký kůň úpěl ďábelské ódy</p>

<p>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.</p>

<h2>A heading we'll be removing</h2>
Expand Down
2 changes: 2 additions & 0 deletions example/b.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<title>Goodbye Cruel World!</title>
</head>
<body>
<p>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)</p>

<p>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.</p>


Expand Down
2 changes: 2 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
$options = array(
//'ignoreWhitespace' => true,
//'ignoreCase' => true,
//'title_a' => 'some other title than "Old Version"',
//'title_b' => 'some other title than "New Version"',
);

// Initialize the diff class
Expand Down
9 changes: 6 additions & 3 deletions lib/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ class Diff
'context' => 3,
'ignoreNewLines' => false,
'ignoreWhitespace' => false,
'ignoreCase' => false
'ignoreCase' => false,
'title_a'=>'Old Version',
'title_b'=>'New Version',
'labelDifferences'=>'Differences'
);

/**
* @var array Array of the options that have been applied for generating the diff.
*/
private $options = array();
public $options = array();

/**
* The constructor.
Expand Down Expand Up @@ -170,7 +173,7 @@ public function getGroupedOpcodes()

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;
}
}
12 changes: 8 additions & 4 deletions lib/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ 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'];
$label_differences = $this->diff->options['labelDifferences'];

$html = '';
if(empty($changes)) {
return $html;
Expand All @@ -61,9 +65,9 @@ public function render()
$html .= '<table class="Differences DifferencesInline">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>Old</th>';
$html .= '<th>New</th>';
$html .= '<th>Differences</th>';
$html .= '<th>'.$title_a.'</th>';
$html .= '<th>'.$title_b.'</th>';
$html .= '<th>'.$label_differences.'</th>';
$html .= '</tr>';
$html .= '</thead>';
foreach($changes as $i => $blocks) {
Expand Down Expand Up @@ -128,8 +132,8 @@ public function render()
foreach($change['changed']['lines'] as $no => $line) {
$toLine = $change['changed']['offset'] + $no + 1;
$html .= '<tr>';
$html .= '<th>'.$toLine.'</th>';
$html .= '<th>&nbsp;</th>';
$html .= '<th>'.$toLine.'</th>';
$html .= '<td class="Right"><span>'.$line.'</span></td>';
$html .= '</tr>';
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Diff/Renderer/Html/SideBySide.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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)) {
Expand All @@ -62,8 +64,8 @@ public function render()
$html .= '<table class="Differences DifferencesSideBySide">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th colspan="2">Old Version</th>';
$html .= '<th colspan="2">New Version</th>';
$html .= '<th colspan="2">'.$title_a.'</th>';
$html .= '<th colspan="2">'.$title_b.'</th>';
$html .= '</tr>';
$html .= '</thead>';
foreach($changes as $i => $blocks) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Diff/SequenceMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down