Skip to content

Commit 968c8e2

Browse files
committed
Added ability to set title for Inline renderer
1 parent a8ccfc6 commit 968c8e2

File tree

8 files changed

+194
-5
lines changed

8 files changed

+194
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# [2.0.4](https://github.com/phalcongelist/php-diff/releases/tag/v2.0.4) (2016-XX-XX)
22

33
* Added ability to set title for `SideBySide` renderer
4+
* Added ability to set title for `Inline` renderer
45

56
# [2.0.3](https://github.com/phalcongelist/php-diff/releases/tag/v2.0.3) (2016-07-18)
67

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"type": "library",
44
"description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).",
55
"license": "BSD-3-Clause",
6+
"keywords": [
7+
"php", "diff", "phalcon"
8+
],
69
"authors": [
710
{
811
"name": "Phalcon Team",

example2/a.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
4+
<title>Hello World!</title>
5+
</head>
6+
<body>
7+
<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>
8+
9+
<h2>A heading we'll be removing</h2>
10+
11+
<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>
12+
</body>
13+
</html>

example2/b.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
4+
<title>Goodbye Cruel World!</title>
5+
</head>
6+
<body>
7+
<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>
8+
9+
10+
<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>
11+
12+
<p>Just a small amount of new text...</p>
13+
</body>
14+
</html>

example2/example.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Phalcon\Diff;
4+
use Phalcon\Diff\Render\Html\Inline;
5+
use Phalcon\Diff\Render\Text\Unified;
6+
use Phalcon\Diff\Render\Text\Context;
7+
use Phalcon\Diff\Render\Html\SideBySide;
8+
9+
// Include the diff class
10+
require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php';
11+
12+
// Include two sample files for comparison
13+
$a = explode("\n", file_get_contents(dirname(__FILE__) . '/a.txt'));
14+
$b = explode("\n", file_get_contents(dirname(__FILE__) . '/b.txt'));
15+
16+
// Options for generating the diff
17+
$options = [
18+
// 'ignoreWhitespace' => true,
19+
// 'ignoreCase' => true,
20+
];
21+
22+
// Initialize the diff class
23+
$diff = new Diff($a, $b, $options);
24+
?>
25+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
26+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
27+
<html>
28+
<head>
29+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
30+
<title>Phalcon Diff - Examples</title>
31+
<link rel="stylesheet" href="styles.css" type="text/css" charset="utf-8"/>
32+
</head>
33+
<body>
34+
<h1>Phalcon Diff - Examples</h1>
35+
<hr />
36+
37+
<h2>Side by Side Diff</h2>
38+
<?php echo $diff->render(new SideBySide);?>
39+
40+
<h2>Inline Diff</h2>
41+
<?php echo $diff->render(new Inline); ?>
42+
43+
<h2>Unified Diff</h2>
44+
<pre><?php echo htmlspecialchars($diff->render(new Unified)); ?></pre>
45+
46+
<h2>Context Diff</h2>
47+
<pre><?php echo htmlspecialchars($diff->render(new Context)); ?></pre>
48+
</body>
49+
</html>

example2/styles.css

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
body {
2+
background: #fff;
3+
font-family: Arial;
4+
font-size: 12px;
5+
}
6+
.Differences {
7+
width: 100%;
8+
border-collapse: collapse;
9+
border-spacing: 0;
10+
empty-cells: show;
11+
}
12+
13+
.Differences thead th {
14+
text-align: left;
15+
border-bottom: 1px solid #000;
16+
background: #aaa;
17+
color: #000;
18+
padding: 4px;
19+
}
20+
.Differences tbody th {
21+
text-align: right;
22+
background: #ccc;
23+
width: 4em;
24+
padding: 1px 2px;
25+
border-right: 1px solid #000;
26+
vertical-align: top;
27+
font-size: 13px;
28+
}
29+
30+
.Differences td {
31+
padding: 1px 2px;
32+
font-family: Consolas, monospace;
33+
font-size: 13px;
34+
}
35+
36+
.DifferencesSideBySide .ChangeInsert td.Left {
37+
background: #dfd;
38+
}
39+
40+
.DifferencesSideBySide .ChangeInsert td.Right {
41+
background: #cfc;
42+
}
43+
44+
.DifferencesSideBySide .ChangeDelete td.Left {
45+
background: #f88;
46+
}
47+
48+
.DifferencesSideBySide .ChangeDelete td.Right {
49+
background: #faa;
50+
}
51+
52+
.DifferencesSideBySide .ChangeReplace .Left {
53+
background: #fe9;
54+
}
55+
56+
.DifferencesSideBySide .ChangeReplace .Right {
57+
background: #fd8;
58+
}
59+
60+
.Differences ins, .Differences del {
61+
text-decoration: none;
62+
}
63+
64+
.DifferencesSideBySide .ChangeReplace ins, .DifferencesSideBySide .ChangeReplace del {
65+
background: #fc0;
66+
}
67+
68+
.Differences .Skipped {
69+
background: #f7f7f7;
70+
}
71+
72+
.DifferencesInline .ChangeReplace .Left,
73+
.DifferencesInline .ChangeDelete .Left {
74+
background: #fdd;
75+
}
76+
77+
.DifferencesInline .ChangeReplace .Right,
78+
.DifferencesInline .ChangeInsert .Right {
79+
background: #dfd;
80+
}
81+
82+
.DifferencesInline .ChangeReplace ins {
83+
background: #9e9;
84+
}
85+
86+
.DifferencesInline .ChangeReplace del {
87+
background: #e99;
88+
}
89+
90+
pre {
91+
width: 100%;
92+
overflow: auto;
93+
}

src/Diff/Renderer/Html/Inline.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
*/
2626
class Inline extends BaseArray
2727
{
28+
private $oldTitle = 'Old';
29+
private $newTitle = 'New';
30+
private $diffTitle = 'Differences';
31+
2832
/**
2933
* Render a and return diff with changes between the two sequences
3034
* displayed inline (under each other)
@@ -40,12 +44,24 @@ public function render()
4044
return $html;
4145
}
4246

47+
if (isset($this->options['oldTitle'])) {
48+
$this->oldTitle = $this->options['oldTitle'];
49+
}
50+
51+
if (isset($this->options['newTitle'])) {
52+
$this->newTitle = $this->options['newTitle'];
53+
}
54+
55+
if (isset($this->options['diffTitle'])) {
56+
$this->diffTitle = $this->options['diffTitle'];
57+
}
58+
4359
$html .= '<table class="Differences DifferencesInline">';
4460
$html .= '<thead>';
4561
$html .= '<tr>';
46-
$html .= '<th>Old</th>';
47-
$html .= '<th>New</th>';
48-
$html .= '<th>Differences</th>';
62+
$html .= '<th>' . htmlspecialchars($this->oldTitle) . '</th>';
63+
$html .= '<th>' . htmlspecialchars($this->newTitle) . '</th>';
64+
$html .= '<th>' . htmlspecialchars($this->diffTitle) . '</th>';
4965
$html .= '</tr>';
5066
$html .= '</thead>';
5167
foreach ($changes as $i => $blocks) {

src/Diff/Renderer/Html/SideBySide.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public function render()
5454
$html .= '<table class="Differences DifferencesSideBySide">';
5555
$html .= '<thead>';
5656
$html .= '<tr>';
57-
$html .= '<th colspan="2">' . $this->oldTitle . '</th>';
58-
$html .= '<th colspan="2">' . $this->newTitle . '</th>';
57+
$html .= '<th colspan="2">' . htmlspecialchars($this->oldTitle) . '</th>';
58+
$html .= '<th colspan="2">' . htmlspecialchars($this->newTitle) . '</th>';
5959
$html .= '</tr>';
6060
$html .= '</thead>';
6161

0 commit comments

Comments
 (0)