Skip to content

Commit 064a3b3

Browse files
committed
Add renderer option: outputTagAsString
Signed-off-by: Jack Cherng <[email protected]>
1 parent 23c59f8 commit 064a3b3

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ $templateOptions = [
7373
'spacesToNbsp' => false,
7474
// HTML template tab width (negative = do not convert into spaces)
7575
'tabSize' => 4,
76+
// internally, ops (tags) are all int type but this is not good for human reading.
77+
// set this to "true" to convert them into string form before outputting.
78+
'outputTagAsString' => true,
7679
];
7780

7881
// one-line simple usage

UPGRADING_v5.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515

1616
- `<th class="f-num">` (from-number) becomes `<th class="n-new">` (number-new).
1717
- `<th class="t-num">` (to-number) becomes `<th class="n-old">` (number-old).
18+
19+
- The `tag` in `Json` template is now in `int` form by default.
20+
To get previous behavior, set the renderer option `outputTagAsString` to `true`.

example/demo.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
'spacesToNbsp' => false,
4141
// HTML template tab width (negative = do not convert into spaces)
4242
'tabSize' => 4,
43+
// internally, ops (tags) are all int type but this is not good for human reading.
44+
// set this to "true" to convert them into string form before outputting.
45+
'outputTagAsString' => false,
4346
];
4447

4548
?>
@@ -181,7 +184,7 @@
181184
$new_file,
182185
'Json',
183186
$diffOptions,
184-
$templateOptions
187+
['outputTagAsString' => true] + $templateOptions
185188
);
186189

187190
$beautified = \json_encode(

src/Renderer/AbstractRenderer.php

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ abstract class AbstractRenderer implements RendererInterface
5050
// the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
5151
// but if you want to visualize them in the backend with "&nbsp;", you can set this to true
5252
'spacesToNbsp' => false,
53+
// internally, ops (tags) are all int type but this is not good for human reading.
54+
// set this to "true" to convert them into string form before outputting.
55+
'outputTagAsString' => false,
5356
];
5457

5558
/**

src/Renderer/Html/Json.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Jfcherng\Diff\Renderer\Html;
66

7+
use Jfcherng\Diff\SequenceMatcher;
8+
79
/**
810
* Json diff generator.
911
*/
@@ -26,12 +28,32 @@ final class Json extends AbstractHtml
2628
*/
2729
public function render(): string
2830
{
31+
$changes = $this->getChanges();
32+
33+
if ($this->options['outputTagAsString']) {
34+
$this->convertTagToString($changes);
35+
}
36+
2937
return \json_encode(
30-
$this->getChanges(),
38+
$changes,
3139
\JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES
3240
);
3341
}
3442

43+
/**
44+
* Convert tags of changes to their string form for better readability.
45+
*
46+
* @param array $changes the changes
47+
*/
48+
protected function convertTagToString(array &$changes): void
49+
{
50+
foreach ($changes as &$blocks) {
51+
foreach ($blocks as &$change) {
52+
$change['tag'] = SequenceMatcher::opIntToStr($change['tag']);
53+
}
54+
}
55+
}
56+
3557
/**
3658
* {@inheritdoc}
3759
*/

tests/DiffTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Jfcherng\Diff\Test;
66

77
use Jfcherng\Diff\Diff;
8+
use Jfcherng\Diff\SequenceMatcher;
89
use PHPUnit\Framework\TestCase;
910

1011
/**
@@ -37,10 +38,10 @@ public function getGroupedOpcodesDataProvider(): array
3738
,
3839
[
3940
[
40-
['eq', 0, 1, 0, 1],
41-
['del', 1, 2, 1, 1],
42-
['eq', 2, 4, 1, 3],
43-
['ins', 4, 4, 3, 4],
41+
[SequenceMatcher::OP_EQ, 0, 1, 0, 1],
42+
[SequenceMatcher::OP_DEL, 1, 2, 1, 1],
43+
[SequenceMatcher::OP_EQ, 2, 4, 1, 3],
44+
[SequenceMatcher::OP_INS, 4, 4, 3, 4],
4445
],
4546
],
4647
],

0 commit comments

Comments
 (0)