-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.php
executable file
·124 lines (119 loc) · 4.16 KB
/
diff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/*
This file is part of weeklyd3's wiki software.
weeklyd3's wiki software is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
weeklyd3's wiki software is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with weeklyd3's wiki software. If not, see
<https://www.gnu.org/licenses/>.
*/
/* Licensed under GNU AGPL 3.0, from
https://github.com/weeklyd3/nodb-forum/blob/master/libraries/diff.php
*/
function diff(string $old, string $new, string $oldcaption = "Old revision", string $newcaption = "New revision") {
if ($old === $new) { ?>Nothing was changed.<?php return;}
foreach (array(&$old, &$new) as &$string) {
$string = explode("\n", $string);
}
$lines = max(count($old), count($new));
$unifiedVersion = array();
for ($i = 0; $i < $lines; $i++) {
$unifiedEntry = array();
if (isset($old[$i])) {
$unifiedEntry['old'] = $old[$i];
}
if (isset($new[$i])) {
$unifiedEntry['new'] = $new[$i];
}
array_push($unifiedVersion, $unifiedEntry);
}
?><table class="fixed-layout table exempt-from-format" style="width: 100%;">
<tr>
<th><?php echo htmlspecialchars($oldcaption); ?></th>
<th><?php echo htmlspecialchars($newcaption); ?></th>
</tr>
<?php
$rowsSkipped = 0;
foreach ($unifiedVersion as $num => $line) {
$oldLine = isset($line['old']) ? $line['old'] : null;
$newLine = isset($line['new']) ? $line['new'] : null;
if ($oldLine === $newLine) {
$rowsSkipped++;
continue;
}
if ($rowsSkipped !== 0) {
?><tr>
<td colspan="2">
(<?php echo $rowsSkipped; ?> identical row(s) skipped)
</td>
</tr><?php
}
$rowsSkipped = 0;
$inlineDiff = inlineDiff($oldLine, $newLine);
?><tr>
<td><div style="white-space:pre-wrap; font-family: monospace;"><?php echo $inlineDiff['old']; ?></div></td><td><div style="white-space:pre-wrap; font-family: monospace;"><?php echo $inlineDiff['new']; ?></div></td></tr><?php
}
if ($rowsSkipped > 0) {
?><tr><td colspan="2">(<?php echo $rowsSkipped; ?> identical row(s) skipped)</td></tr><?php
} ?>
</table><?php
}
function inlineDiff(?string $oldline, ?string $newline): array {
$inlineDiff = array('old' => '', 'new' => '');
if (!isset($oldline) || !isset($newline)) {
if (!isset($oldline) || $oldline === '') {
$inlineDiff['old'] = '<i>(empty)</i>';
}
if (!isset($newline) || $newline === '') {
$inlineDiff['new'] = '<i>(empty)</i>';
}
}
if ($oldline === "") {
$inlineDiff['old'] = '<i>(blank)</i>';
}
if ($newline === "") {
$inlineDiff['new'] = '<i>(blank)</i>';
}
$old = str_split($oldline);
$new = str_split($newline);
$un = array();
$max = max(count($old), count($new));
for ($j = 0; $j < $max; $j++) {
$oldChar = isset($old[$j]) ? $old[$j] : null;
$newChar = isset($new[$j]) ? $new[$j] : null;
$oldTagOpened = false;
$newTagOpened = false;
if (isset($oldChar) && isset($newChar)) {
if ($oldChar === $newChar) {
$inlineDiff['old'] .= htmlspecialchars($oldChar);
$inlineDiff['new'] .= htmlspecialchars($newChar);
} else {
$oldTagOpened = true;
$newTagOpened = true;
$inlineDiff['old'] .= '<span style="background-color: red; color: white;">' . htmlspecialchars($oldChar);
$inlineDiff['new'] .= '<span style="background-color: lime; color: black;">' . htmlspecialchars($newChar);
}
}
if (isset($oldChar) && !isset($newChar)) {
$oldTagOpened = true;
$inlineDiff['old'] .= '<span style="background-color: red; color: white;">' . htmlspecialchars($oldChar);
}
if (!isset($oldChar) && isset($newChar)) {
$newTagOpened = true;
$inlineDiff['new'] .= '<span style="background-color: lime; color: black;">' . htmlspecialchars($newChar);
}
if ($oldTagOpened) {
$inlineDiff['old'] .= '</span>';
}
if ($newTagOpened) {
$inlineDiff['new'] .= '</span>';
}
}
return $inlineDiff;
}