Skip to content

Commit 732fa82

Browse files
committed
Merge branch 'feat/json2html' into v6
2 parents 8235504 + c713a0a commit 732fa82

File tree

13 files changed

+196
-119
lines changed

13 files changed

+196
-119
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ $result = DiffHelper::calculate($old, $new, $rendererName);
109109
$differ = new Differ(explode("\n", $old), explode("\n", $new), $differOptions);
110110
$renderer = RendererFactory::make($rendererName, $rendererOptions); // or your own renderer object
111111
$result = $renderer->render($differ);
112+
113+
// use the JSON result to render in HTML
114+
$jsonResult = DiffHelper::calculate($old, $new, 'Json'); // may store the JSON result in your Database
115+
$htmlRenderer = RendererFactory::make('Inline');
116+
$result = $htmlRenderer->renderArray(json_decode($jsonResult, true));
112117
```
113118

114119

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"require": {
2929
"php": "^7.1.3",
3030
"jfcherng/php-mb-string": "^1.3",
31-
"jfcherng/php-sequence-matcher": "^3.1",
31+
"jfcherng/php-sequence-matcher": "^3.2",
3232
"nicmart/string-template": "~0.1"
3333
},
3434
"require-dev": {

composer.lock

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/demo.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
include __DIR__ . '/../vendor/autoload.php';
44

55
use Jfcherng\Diff\DiffHelper;
6+
use Jfcherng\Diff\Factory\RendererFactory;
67

78
?>
89
<!DOCTYPE html>
@@ -60,135 +61,135 @@
6061
<?php
6162

6263
// demo the no-inline-detail diff
63-
$result = DiffHelper::calculate(
64+
$inlineResult = DiffHelper::calculate(
6465
$oldFile,
6566
$newFile,
6667
'Inline',
6768
$diffOptions,
6869
['detailLevel' => 'none'] + $rendererOptions
6970
);
7071

71-
echo $result;
72+
echo $inlineResult;
7273

7374
?>
7475

7576
<h1>Line-level Diff (Default)</h1>
7677
<?php
7778

7879
// demo the word-level diff
79-
$result = DiffHelper::calculate(
80+
$inlineResult = DiffHelper::calculate(
8081
$oldFile,
8182
$newFile,
8283
'Inline',
8384
$diffOptions,
8485
['detailLevel' => 'line'] + $rendererOptions
8586
);
8687

87-
echo $result;
88+
echo $inlineResult;
8889

8990
?>
9091

9192
<h1>Word-level Diff</h1>
9293
<?php
9394

9495
// demo the word-level diff
95-
$result = DiffHelper::calculate(
96+
$inlineResult = DiffHelper::calculate(
9697
$oldFile,
9798
$newFile,
9899
'Inline',
99100
$diffOptions,
100101
['detailLevel' => 'word'] + $rendererOptions
101102
);
102103

103-
echo $result;
104+
echo $inlineResult;
104105

105106
?>
106107

107108
<h1>Character-level Diff</h1>
108109
<?php
109110

110111
// demo the character-level diff
111-
$result = DiffHelper::calculate(
112+
$inlineResult = DiffHelper::calculate(
112113
$oldFile,
113114
$newFile,
114115
'Inline',
115116
$diffOptions,
116117
['detailLevel' => 'char'] + $rendererOptions
117118
);
118119

119-
echo $result;
120+
echo $inlineResult;
120121

121122
?>
122123

123124
<h1>Side by Side Diff</h1>
124125
<?php
125126

126127
// generate a side by side diff
127-
$result = DiffHelper::calculateFiles(
128+
$sideBySideResult = DiffHelper::calculateFiles(
128129
$oldFilePath,
129130
$newFilePath,
130131
'SideBySide',
131132
$diffOptions,
132133
$rendererOptions
133134
);
134135

135-
echo $result;
136+
echo $sideBySideResult;
136137

137138
?>
138139

139140
<h1>Inline Diff</h1>
140141
<?php
141142

142143
// generate an inline diff
143-
$result = DiffHelper::calculateFiles(
144+
$inlineResult = DiffHelper::calculateFiles(
144145
$oldFilePath,
145146
$newFilePath,
146147
'Inline',
147148
$diffOptions,
148149
$rendererOptions
149150
);
150151

151-
echo $result;
152+
echo $inlineResult;
152153

153154
?>
154155

155156
<h1>Unified Diff</h1>
156157
<pre><?php
157158

158159
// generate a unified diff
159-
$result = DiffHelper::calculateFiles(
160+
$unifiedResult = DiffHelper::calculateFiles(
160161
$oldFilePath,
161162
$newFilePath,
162163
'Unified',
163164
$diffOptions,
164165
$rendererOptions
165166
);
166167

167-
echo \htmlspecialchars($result);
168+
echo \htmlspecialchars($unifiedResult);
168169

169170
?></pre>
170171

171172
<h1>Context Diff</h1>
172173
<pre><?php
173174

174175
// generate a context diff
175-
$result = DiffHelper::calculateFiles(
176+
$contextResult = DiffHelper::calculateFiles(
176177
$oldFilePath,
177178
$newFilePath,
178179
'Context',
179180
$diffOptions,
180181
$rendererOptions
181182
);
182183

183-
echo \htmlspecialchars($result);
184+
echo \htmlspecialchars($contextResult);
184185

185186
?></pre>
186187

187188
<h1>JSON Diff</h1>
188189
<pre><?php
189190

190191
// generate a JSON diff
191-
$result = DiffHelper::calculateFiles(
192+
$jsonResult = DiffHelper::calculateFiles(
192193
$oldFilePath,
193194
$newFilePath,
194195
'Json',
@@ -197,12 +198,24 @@
197198
);
198199

199200
$beautified = \json_encode(
200-
\json_decode($result, true),
201+
\json_decode($jsonResult, true),
201202
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT
202203
);
203204

204205
echo $beautified;
205206

206207
?></pre>
208+
209+
<h1>HTML Diff from the Result of JSON Diff</h1>
210+
<pre><?php
211+
212+
$jsonArray = \json_decode($jsonResult, true);
213+
214+
$htmlRenderer = RendererFactory::make('Inline');
215+
$inlineResult = $htmlRenderer->renderArray($jsonArray);
216+
217+
echo $inlineResult;
218+
219+
?></pre>
207220
</body>
208221
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jfcherng\Diff\Exception;
6+
7+
final class UnsupportedFunctionException extends \Exception
8+
{
9+
public function __construct(string $funcName = '', int $code = 0, \Throwable $previous = null)
10+
{
11+
parent::__construct("Unsupported function: {$funcName}", $code, $previous);
12+
}
13+
}

src/Renderer/AbstractRenderer.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,13 @@ final public function renderArray(array $differArray): string
154154
* @param Differ $differ the differ object
155155
*/
156156
abstract protected function renderWoker(Differ $differ): string;
157-
157+
158158
/**
159-
* The worker for array render.
159+
* The real worker for self::renderArray().
160160
*
161161
* @param array $differArray the differ array
162-
*
163-
* @return string
164162
*/
165163
abstract protected function renderArrayWoker(array $differArray): string;
166-
167-
/**
168-
* Woker's base function.
169-
*
170-
* @param array $changes the changes array
171-
*
172-
* @return string
173-
*/
174-
abstract protected function baseWoker(array $changes): string;
175164

176165
/**
177166
* Update the Language object.

src/Renderer/Html/AbstractHtml.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ public function getChanges(Differ $differ): array
127127
return $changes;
128128
}
129129

130+
/**
131+
* {@inheritdoc}
132+
*/
133+
protected function renderWoker(Differ $differ): string
134+
{
135+
return $this->redererChanges($this->getChanges($differ));
136+
}
137+
138+
/**
139+
* {@inheritdoc}
140+
*/
141+
protected function renderArrayWoker(array $differArray): string
142+
{
143+
return $this->redererChanges($this->ensureChangesUseIntTag($differArray));
144+
}
145+
146+
/**
147+
* Render the array of changes.
148+
*
149+
* @param array $changes the changes
150+
*/
151+
abstract protected function redererChanges(array $changes): string;
152+
130153
/**
131154
* Renderer the changed extent.
132155
*
@@ -284,4 +307,40 @@ function (array $matches): string {
284307
$string
285308
);
286309
}
310+
311+
/**
312+
* Make sure the "changes" array uses int "tag".
313+
*
314+
* Internally, we would like always int form for better performance.
315+
*
316+
* @param array $changes the changes
317+
*/
318+
protected function ensureChangesUseIntTag(array $changes): array
319+
{
320+
if (empty($changes)) {
321+
return [];
322+
}
323+
324+
$isTagInt = true;
325+
foreach ($changes as $blocks) {
326+
foreach ($blocks as $change) {
327+
$isTagInt = \is_int($change['tag']);
328+
329+
break 2;
330+
}
331+
}
332+
333+
if (!$isTagInt) {
334+
// convert string tags into their int forms
335+
foreach ($changes as &$blocks) {
336+
foreach ($blocks as &$change) {
337+
$change['tag'] = SequenceMatcher::opStrToInt($change['tag']);
338+
}
339+
}
340+
341+
unset($blocks, $change);
342+
}
343+
344+
return $changes;
345+
}
287346
}

0 commit comments

Comments
 (0)