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

Customise "old" and "new" labels #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
//'ignoreCase' => true,
);

// Options for the renderer (change labels)
$renderer_options = array(
'old'=> 'Production',
'new'=> 'Development'
);

// Initialize the diff class
$diff = new Diff($a, $b, $options);

Expand All @@ -36,6 +42,13 @@
$renderer = new Diff_Renderer_Html_SideBySide;
echo $diff->Render($renderer);

?>
<h2>Side by Side Diff with custom labels</h2>
<?php

// Generate a side by side diff with custom labels
echo $diff->Render($renderer, $renderer_options);

?>
<h2>Inline Diff</h2>
<?php
Expand All @@ -45,6 +58,13 @@
$renderer = new Diff_Renderer_Html_Inline;
echo $diff->render($renderer);

?>
<h2>Inline Diff with custom labels</h2>
<?php

// Generate an inline diff with custom labels
echo $diff->render($renderer, $renderer_options);

?>
<h2>Unified Diff</h2>
<pre><?php
Expand Down
12 changes: 9 additions & 3 deletions lib/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class Diff
'context' => 3,
'ignoreNewLines' => false,
'ignoreWhitespace' => false,
'ignoreCase' => false
'ignoreCase' => false,
'old' => false,
'new' => false,
);

/**
Expand Down Expand Up @@ -98,10 +100,14 @@ public function __construct($a, $b, $options=array())
* @param 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(Diff_Renderer_Abstract $renderer,$renderer_options=false)
{
$renderer->diff = $this;
return $renderer->render();
if($renderer_options) {
return $renderer->render($renderer_options);
} else {
return $renderer->render();
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Diff_Renderer_Html_Inline extends Diff_Renderer_Html_Array
*
* @return string The generated inline diff.
*/
public function render()
public function render($renderer_options=array())
{
$changes = parent::render();
$html = '';
Expand All @@ -61,8 +61,8 @@ public function render()
$html .= '<table class="Differences DifferencesInline">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>Old</th>';
$html .= '<th>New</th>';
$html .= '<th>'.(!empty($renderer_options['old']) ? htmlspecialchars($renderer_options['old']) :'Old').'</th>';
$html .= '<th>'.(!empty($renderer_options['new']) ? htmlspecialchars($renderer_options['new']) :'New').'</th>';
$html .= '<th>Differences</th>';
$html .= '</tr>';
$html .= '</thead>';
Expand Down
6 changes: 3 additions & 3 deletions lib/Diff/Renderer/Html/SideBySide.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Diff_Renderer_Html_SideBySide extends Diff_Renderer_Html_Array
*
* @return string The generated side by side diff.
*/
public function render()
public function render($renderer_options=array())
{
$changes = parent::render();

Expand All @@ -62,8 +62,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">'.(!empty($renderer_options['old']) ? htmlspecialchars($renderer_options['old']) :'Old Version').'</th>';
$html .= '<th colspan="2">'.(!empty($renderer_options['new']) ? htmlspecialchars($renderer_options['new']) :'New Version').'</th>';
$html .= '</tr>';
$html .= '</thead>';
foreach($changes as $i => $blocks) {
Expand Down