|
3 | 3 | include __DIR__ . '/../vendor/autoload.php'; |
4 | 4 |
|
5 | 5 | use Jfcherng\Diff\DiffHelper; |
| 6 | +use Jfcherng\Diff\Factory\RendererFactory; |
6 | 7 |
|
7 | 8 | ?> |
8 | 9 | <!DOCTYPE html> |
|
60 | 61 | <?php |
61 | 62 |
|
62 | 63 | // demo the no-inline-detail diff |
63 | | - $result = DiffHelper::calculate( |
| 64 | + $inlineResult = DiffHelper::calculate( |
64 | 65 | $oldFile, |
65 | 66 | $newFile, |
66 | 67 | 'Inline', |
67 | 68 | $diffOptions, |
68 | 69 | ['detailLevel' => 'none'] + $rendererOptions |
69 | 70 | ); |
70 | 71 |
|
71 | | - echo $result; |
| 72 | + echo $inlineResult; |
72 | 73 |
|
73 | 74 | ?> |
74 | 75 |
|
75 | 76 | <h1>Line-level Diff (Default)</h1> |
76 | 77 | <?php |
77 | 78 |
|
78 | 79 | // demo the word-level diff |
79 | | - $result = DiffHelper::calculate( |
| 80 | + $inlineResult = DiffHelper::calculate( |
80 | 81 | $oldFile, |
81 | 82 | $newFile, |
82 | 83 | 'Inline', |
83 | 84 | $diffOptions, |
84 | 85 | ['detailLevel' => 'line'] + $rendererOptions |
85 | 86 | ); |
86 | 87 |
|
87 | | - echo $result; |
| 88 | + echo $inlineResult; |
88 | 89 |
|
89 | 90 | ?> |
90 | 91 |
|
91 | 92 | <h1>Word-level Diff</h1> |
92 | 93 | <?php |
93 | 94 |
|
94 | 95 | // demo the word-level diff |
95 | | - $result = DiffHelper::calculate( |
| 96 | + $inlineResult = DiffHelper::calculate( |
96 | 97 | $oldFile, |
97 | 98 | $newFile, |
98 | 99 | 'Inline', |
99 | 100 | $diffOptions, |
100 | 101 | ['detailLevel' => 'word'] + $rendererOptions |
101 | 102 | ); |
102 | 103 |
|
103 | | - echo $result; |
| 104 | + echo $inlineResult; |
104 | 105 |
|
105 | 106 | ?> |
106 | 107 |
|
107 | 108 | <h1>Character-level Diff</h1> |
108 | 109 | <?php |
109 | 110 |
|
110 | 111 | // demo the character-level diff |
111 | | - $result = DiffHelper::calculate( |
| 112 | + $inlineResult = DiffHelper::calculate( |
112 | 113 | $oldFile, |
113 | 114 | $newFile, |
114 | 115 | 'Inline', |
115 | 116 | $diffOptions, |
116 | 117 | ['detailLevel' => 'char'] + $rendererOptions |
117 | 118 | ); |
118 | 119 |
|
119 | | - echo $result; |
| 120 | + echo $inlineResult; |
120 | 121 |
|
121 | 122 | ?> |
122 | 123 |
|
123 | 124 | <h1>Side by Side Diff</h1> |
124 | 125 | <?php |
125 | 126 |
|
126 | 127 | // generate a side by side diff |
127 | | - $result = DiffHelper::calculateFiles( |
| 128 | + $sideBySideResult = DiffHelper::calculateFiles( |
128 | 129 | $oldFilePath, |
129 | 130 | $newFilePath, |
130 | 131 | 'SideBySide', |
131 | 132 | $diffOptions, |
132 | 133 | $rendererOptions |
133 | 134 | ); |
134 | 135 |
|
135 | | - echo $result; |
| 136 | + echo $sideBySideResult; |
136 | 137 |
|
137 | 138 | ?> |
138 | 139 |
|
139 | 140 | <h1>Inline Diff</h1> |
140 | 141 | <?php |
141 | 142 |
|
142 | 143 | // generate an inline diff |
143 | | - $result = DiffHelper::calculateFiles( |
| 144 | + $inlineResult = DiffHelper::calculateFiles( |
144 | 145 | $oldFilePath, |
145 | 146 | $newFilePath, |
146 | 147 | 'Inline', |
147 | 148 | $diffOptions, |
148 | 149 | $rendererOptions |
149 | 150 | ); |
150 | 151 |
|
151 | | - echo $result; |
| 152 | + echo $inlineResult; |
152 | 153 |
|
153 | 154 | ?> |
154 | 155 |
|
155 | 156 | <h1>Unified Diff</h1> |
156 | 157 | <pre><?php |
157 | 158 |
|
158 | 159 | // generate a unified diff |
159 | | - $result = DiffHelper::calculateFiles( |
| 160 | + $unifiedResult = DiffHelper::calculateFiles( |
160 | 161 | $oldFilePath, |
161 | 162 | $newFilePath, |
162 | 163 | 'Unified', |
163 | 164 | $diffOptions, |
164 | 165 | $rendererOptions |
165 | 166 | ); |
166 | 167 |
|
167 | | - echo \htmlspecialchars($result); |
| 168 | + echo \htmlspecialchars($unifiedResult); |
168 | 169 |
|
169 | 170 | ?></pre> |
170 | 171 |
|
171 | 172 | <h1>Context Diff</h1> |
172 | 173 | <pre><?php |
173 | 174 |
|
174 | 175 | // generate a context diff |
175 | | - $result = DiffHelper::calculateFiles( |
| 176 | + $contextResult = DiffHelper::calculateFiles( |
176 | 177 | $oldFilePath, |
177 | 178 | $newFilePath, |
178 | 179 | 'Context', |
179 | 180 | $diffOptions, |
180 | 181 | $rendererOptions |
181 | 182 | ); |
182 | 183 |
|
183 | | - echo \htmlspecialchars($result); |
| 184 | + echo \htmlspecialchars($contextResult); |
184 | 185 |
|
185 | 186 | ?></pre> |
186 | 187 |
|
187 | 188 | <h1>JSON Diff</h1> |
188 | 189 | <pre><?php |
189 | 190 |
|
190 | 191 | // generate a JSON diff |
191 | | - $result = DiffHelper::calculateFiles( |
| 192 | + $jsonResult = DiffHelper::calculateFiles( |
192 | 193 | $oldFilePath, |
193 | 194 | $newFilePath, |
194 | 195 | 'Json', |
|
197 | 198 | ); |
198 | 199 |
|
199 | 200 | $beautified = \json_encode( |
200 | | - \json_decode($result, true), |
| 201 | + \json_decode($jsonResult, true), |
201 | 202 | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT |
202 | 203 | ); |
203 | 204 |
|
204 | 205 | echo $beautified; |
205 | 206 |
|
206 | 207 | ?></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> |
207 | 220 | </body> |
208 | 221 | </html> |
0 commit comments