Skip to content

Commit 69f42a1

Browse files
authored
lineNumber option (#19)
* line-numbers * travis fix * fix spaces
1 parent 1140ec2 commit 69f42a1

File tree

3 files changed

+86
-28
lines changed

3 files changed

+86
-28
lines changed

src/Renderer/AbstractRenderer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ abstract class AbstractRenderer implements RendererInterface
4343
'tabSize' => 4,
4444
// show a separator between different diff hunks in HTML renderers
4545
'separateBlock' => true,
46+
// show line numbers in HTML renderers
47+
'lineNumbers' => true,
4648
// the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces
4749
// but if you want to visualize them in the backend with " ", you can set this to true
4850
'spacesToNbsp' => false,

src/Renderer/Html/Inline.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,18 @@ protected function redererChanges(array $changes): string
5555
*/
5656
protected function renderTableHeader(): string
5757
{
58+
$colspan = (!$this->options['lineNumbers'] ? ' colspan="2"' : '');
59+
5860
return
5961
'<thead>' .
6062
'<tr>' .
61-
'<th>' . $this->_('old_version') . '</th>' .
62-
'<th>' . $this->_('new_version') . '</th>' .
63-
'<th></th>' .
64-
'<th>' . $this->_('differences') . '</th>' .
63+
($this->options['lineNumbers'] ?
64+
'<th>' . $this->_('old_version') . '</th>' .
65+
'<th>' . $this->_('new_version') . '</th>' .
66+
'<th></th>'
67+
: ''
68+
) .
69+
'<th' . $colspan . '>' . $this->_('differences') . '</th>' .
6570
'</tr>' .
6671
'</thead>';
6772
}
@@ -71,10 +76,12 @@ protected function renderTableHeader(): string
7176
*/
7277
protected function renderTableSeparateBlock(): string
7378
{
79+
$colspan = (!$this->options['lineNumbers'] ? '2' : '4');
80+
7481
return
7582
'<tbody class="skipped">' .
7683
'<tr>' .
77-
'<td colspan="4"></td>' .
84+
'<td colspan="' . $colspan . '"></td>' .
7885
'</tr>' .
7986
'</tbody>';
8087
}
@@ -119,8 +126,11 @@ protected function renderTableEqual(array $change): string
119126

120127
$html .=
121128
'<tr data-type="=">' .
122-
'<th class="n-old">' . $oldLineNum . '</th>' .
123-
'<th class="n-new">' . $newLineNum . '</th>' .
129+
($this->options['lineNumbers'] ?
130+
'<th class="n-old">' . $oldLineNum . '</th>' .
131+
'<th class="n-old">' . $newLineNum . '</th>'
132+
: ''
133+
) .
124134
'<th class="sign"></th>' .
125135
'<td class="old">' . $oldLine . '</td>' .
126136
'</tr>';
@@ -143,8 +153,11 @@ protected function renderTableInsert(array $change): string
143153

144154
$html .=
145155
'<tr data-type="+">' .
146-
'<th></th>' .
147-
'<th class="n-new">' . $newLineNum . '</th>' .
156+
($this->options['lineNumbers'] ?
157+
'<th></th>' .
158+
'<th class="n-new">' . $newLineNum . '</th>'
159+
: ''
160+
) .
148161
'<th class="sign ins">+</th>' .
149162
'<td class="new">' . $newLine . '</td>' .
150163
'</tr>';
@@ -167,8 +180,11 @@ protected function renderTableDelete(array $change): string
167180

168181
$html .=
169182
'<tr data-type="-">' .
170-
'<th class="n-old">' . $oldLineNum . '</th>' .
171-
'<th></th>' .
183+
($this->options['lineNumbers'] ?
184+
'<th class="n-old">' . $oldLineNum . '</th>' .
185+
'<th></th>'
186+
: ''
187+
) .
172188
'<th class="sign del">-</th>' .
173189
'<td class="old">' . $oldLine . '</td>' .
174190
'</tr>';
@@ -191,8 +207,11 @@ protected function renderTableReplace(array $change): string
191207

192208
$html .=
193209
'<tr data-type="-">' .
194-
'<th class="n-old">' . $oldLineNum . '</th>' .
195-
'<th></th>' .
210+
($this->options['lineNumbers'] ?
211+
'<th class="n-old">' . $oldLineNum . '</th>' .
212+
'<th></th>'
213+
: ''
214+
) .
196215
'<th class="sign del">-</th>' .
197216
'<td class="old">' . $oldLine . '</td>' .
198217
'</tr>';
@@ -203,8 +222,11 @@ protected function renderTableReplace(array $change): string
203222

204223
$html .=
205224
'<tr data-type="+">' .
206-
'<th></th>' .
207-
'<th class="n-new">' . $newLineNum . '</th>' .
225+
($this->options['lineNumbers'] ?
226+
'<th></th>' .
227+
'<th class="n-new">' . $newLineNum . '</th>'
228+
: ''
229+
) .
208230
'<th class="sign ins">+</th>' .
209231
'<td class="new">' . $newLine . '</td>' .
210232
'</tr>';

src/Renderer/Html/SideBySide.php

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ protected function redererChanges(array $changes): string
5555
*/
5656
protected function renderTableHeader(): string
5757
{
58+
$colspan = ($this->options['lineNumbers'] ? ' colspan="2"' : '');
59+
5860
return
5961
'<thead>' .
6062
'<tr>' .
61-
'<th colspan="2">' . $this->_('old_version') . '</th>' .
62-
'<th colspan="2">' . $this->_('new_version') . '</th>' .
63+
'<th' . $colspan . '>' . $this->_('old_version') . '</th>' .
64+
'<th' . $colspan . '>' . $this->_('new_version') . '</th>' .
6365
'</tr>' .
6466
'</thead>';
6567
}
@@ -69,10 +71,12 @@ protected function renderTableHeader(): string
6971
*/
7072
protected function renderTableSeparateBlock(): string
7173
{
74+
$colspan = (!$this->options['lineNumbers'] ? '2' : '4');
75+
7276
return
7377
'<tbody class="skipped">' .
7478
'<tr>' .
75-
'<td colspan="4"></td>' .
79+
'<td colspan="' . $colspan . '"></td>' .
7680
'</tr>' .
7781
'</tbody>';
7882
}
@@ -117,9 +121,15 @@ protected function renderTableEqual(array $change): string
117121

118122
$html .=
119123
'<tr>' .
120-
'<th class="n-old">' . $oldLineNum . '</th>' .
124+
($this->options['lineNumbers'] ?
125+
'<th class="n-old">' . $oldLineNum . '</th>'
126+
: ''
127+
) .
121128
'<td class="old">' . $oldLine . '</td>' .
122-
'<th class="n-new">' . $newLineNum . '</th>' .
129+
($this->options['lineNumbers'] ?
130+
'<th class="n-new">' . $newLineNum . '</th>'
131+
: ''
132+
) .
123133
'<td class="new">' . $newLine . '</td>' .
124134
'</tr>';
125135
}
@@ -141,9 +151,15 @@ protected function renderTableInsert(array $change): string
141151

142152
$html .=
143153
'<tr>' .
144-
'<th></th>' .
154+
($this->options['lineNumbers'] ?
155+
'<th></th>'
156+
: ''
157+
) .
145158
'<td class="old"></td>' .
146-
'<th class="n-new">' . $newLineNum . '</th>' .
159+
($this->options['lineNumbers'] ?
160+
'<th class="n-new">' . $newLineNum . '</th>'
161+
: ''
162+
) .
147163
'<td class="new">' . $newLine . '</td>' .
148164
'</tr>';
149165
}
@@ -165,9 +181,15 @@ protected function renderTableDelete(array $change): string
165181

166182
$html .=
167183
'<tr>' .
168-
'<th class="n-old">' . $oldLineNum . '</th>' .
184+
($this->options['lineNumbers'] ?
185+
'<th class="n-old">' . $oldLineNum . '</th>'
186+
: ''
187+
) .
169188
'<td class="old">' . $oldLine . '</td>' .
170-
'<th></th>' .
189+
($this->options['lineNumbers'] ?
190+
'<th></th>'
191+
: ''
192+
) .
171193
'<td class="new"></td>' .
172194
'</tr>';
173195
}
@@ -198,9 +220,15 @@ protected function renderTableReplace(array $change): string
198220

199221
$html .=
200222
'<tr>' .
201-
'<th class="n-old">' . $oldLineNum . '</th>' .
223+
($this->options['lineNumbers'] ?
224+
'<th class="n-old">' . $oldLineNum . '</th>'
225+
: ''
226+
) .
202227
'<td class="old"><span>' . $oldLine . '</span></td>' .
203-
'<th class="n-new">' . $newLineNum . '</th>' .
228+
($this->options['lineNumbers'] ?
229+
'<th class="n-new">' . $newLineNum . '</th>'
230+
: ''
231+
) .
204232
'<td class="new">' . $newLine . '</td>' .
205233
'</tr>';
206234
}
@@ -218,9 +246,15 @@ protected function renderTableReplace(array $change): string
218246

219247
$html .=
220248
'<tr>' .
221-
'<th class="n-old">' . $oldLineNum . '</th>' .
249+
($this->options['lineNumbers'] ?
250+
'<th class="n-old">' . $oldLineNum . '</th>'
251+
: ''
252+
) .
222253
'<td class="old"><span>' . $oldLine . '</span></td>' .
223-
'<th class="n-new">' . $newLineNum . '</th>' .
254+
($this->options['lineNumbers'] ?
255+
'<th class="n-new">' . $newLineNum . '</th>'
256+
: ''
257+
) .
224258
'<td class="new">' . $newLine . '</td>' .
225259
'</tr>';
226260
}

0 commit comments

Comments
 (0)