From aa2fbc85017b0f76d631e3a8727b79737e33d780 Mon Sep 17 00:00:00 2001 From: hasan-ozbey <48382593+hasan-ozbey@users.noreply.github.com> Date: Fri, 7 Feb 2020 07:13:43 +0300 Subject: [PATCH 1/3] line-numbers --- src/Renderer/AbstractRenderer.php | 2 + src/Renderer/Html/Inline.php | 78 ++++++++++++++++-------- src/Renderer/Html/SideBySide.php | 98 +++++++++++++++++++++++-------- 3 files changed, 132 insertions(+), 46 deletions(-) diff --git a/src/Renderer/AbstractRenderer.php b/src/Renderer/AbstractRenderer.php index a1688054..1e1ab804 100644 --- a/src/Renderer/AbstractRenderer.php +++ b/src/Renderer/AbstractRenderer.php @@ -43,6 +43,8 @@ abstract class AbstractRenderer implements RendererInterface 'tabSize' => 4, // show a separator between different diff hunks in HTML renderers 'separateBlock' => true, + // show line numbers in HTML renderers + 'lineNumbers' => true, // the frontend HTML could use CSS "white-space: pre;" to visualize consecutive whitespaces // but if you want to visualize them in the backend with " ", you can set this to true 'spacesToNbsp' => false, diff --git a/src/Renderer/Html/Inline.php b/src/Renderer/Html/Inline.php index b69d7433..02993da6 100644 --- a/src/Renderer/Html/Inline.php +++ b/src/Renderer/Html/Inline.php @@ -55,13 +55,18 @@ protected function redererChanges(array $changes): string */ protected function renderTableHeader(): string { + $colspan = (!$this->options['lineNumbers'] ? ' colspan="2"' : ''); + return '' . '' . - '' . $this->_('old_version') . '' . - '' . $this->_('new_version') . '' . - '' . - '' . $this->_('differences') . '' . + ($this->options['lineNumbers'] ? + '' . $this->_('old_version') . '' . + '' . $this->_('new_version') . '' . + '' + : '' + ) . + '' . $this->_('differences') . '' . '' . ''; } @@ -71,10 +76,12 @@ protected function renderTableHeader(): string */ protected function renderTableSeparateBlock(): string { + $colspan = (!$this->options['lineNumbers'] ? '2' : '4'); + return '' . '' . - '' . + '' . '' . ''; } @@ -112,15 +119,20 @@ protected function renderTableEqual(array $change): string // the old and the new may not be exactly the same // because of ignoreCase, ignoreWhitespace, etc foreach ($change['old']['lines'] as $no => $oldLine) { - // hmm... but this is a inline renderer - // we could only pick a line from the old or the new to show - $oldLineNum = $change['old']['offset'] + $no + 1; - $newLineNum = $change['new']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + // hmm... but this is a inline renderer + // we could only pick a line from the old or the new to show + $oldLineNum = $change['old']['offset'] + $no + 1; + $newLineNum = $change['new']['offset'] + $no + 1; + } $html .= '' . - '' . $oldLineNum . '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' . + '' . $newLineNum . '' + : '' + ) . '' . '' . $oldLine . '' . ''; @@ -139,12 +151,17 @@ protected function renderTableInsert(array $change): string $html = ''; foreach ($change['new']['lines'] as $no => $newLine) { - $newLineNum = $change['new']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $newLineNum = $change['new']['offset'] + $no + 1; + } $html .= '' . - '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . + '' . $newLineNum . '' + : '' + ) . '+' . '' . $newLine . '' . ''; @@ -163,12 +180,17 @@ protected function renderTableDelete(array $change): string $html = ''; foreach ($change['old']['lines'] as $no => $oldLine) { - $oldLineNum = $change['old']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $oldLineNum = $change['old']['offset'] + $no + 1; + } $html .= '' . - '' . $oldLineNum . '' . - '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' . + '' + : '' + ) . '-' . '' . $oldLine . '' . ''; @@ -187,24 +209,34 @@ protected function renderTableReplace(array $change): string $html = ''; foreach ($change['old']['lines'] as $no => $oldLine) { - $oldLineNum = $change['old']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $oldLineNum = $change['old']['offset'] + $no + 1; + } $html .= '' . - '' . $oldLineNum . '' . - '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' . + '' + : '' + ) . '-' . '' . $oldLine . '' . ''; } foreach ($change['new']['lines'] as $no => $newLine) { - $newLineNum = $change['new']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $newLineNum = $change['new']['offset'] + $no + 1; + } $html .= '' . - '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . + '' . $newLineNum . '' + : '' + ) . '+' . '' . $newLine . '' . ''; diff --git a/src/Renderer/Html/SideBySide.php b/src/Renderer/Html/SideBySide.php index f1df2400..3946bcc9 100644 --- a/src/Renderer/Html/SideBySide.php +++ b/src/Renderer/Html/SideBySide.php @@ -55,11 +55,13 @@ protected function redererChanges(array $changes): string */ protected function renderTableHeader(): string { + $colspan = ($this->options['lineNumbers'] ? ' colspan="2"' : ''); + return '' . '' . - '' . $this->_('old_version') . '' . - '' . $this->_('new_version') . '' . + '' . $this->_('old_version') . '' . + '' . $this->_('new_version') . '' . '' . ''; } @@ -69,10 +71,12 @@ protected function renderTableHeader(): string */ protected function renderTableSeparateBlock(): string { + $colspan = (!$this->options['lineNumbers'] ? '2' : '4'); + return '' . '' . - '' . + '' . '' . ''; } @@ -112,14 +116,22 @@ protected function renderTableEqual(array $change): string foreach ($change['old']['lines'] as $no => $oldLine) { $newLine = $change['new']['lines'][$no]; - $oldLineNum = $change['old']['offset'] + $no + 1; - $newLineNum = $change['new']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $oldLineNum = $change['old']['offset'] + $no + 1; + $newLineNum = $change['new']['offset'] + $no + 1; + } $html .= '' . - '' . $oldLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' + : '' + ) . '' . $oldLine . '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $newLineNum . '' + : '' + ) . '' . $newLine . '' . ''; } @@ -137,13 +149,21 @@ protected function renderTableInsert(array $change): string $html = ''; foreach ($change['new']['lines'] as $no => $newLine) { - $newLineNum = $change['new']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $newLineNum = $change['new']['offset'] + $no + 1; + } $html .= '' . - '' . + ($this->options['lineNumbers'] ? + '' + : '' + ) . '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $newLineNum . '' + : '' + ) . '' . $newLine . '' . ''; } @@ -161,13 +181,21 @@ protected function renderTableDelete(array $change): string $html = ''; foreach ($change['old']['lines'] as $no => $oldLine) { - $oldLineNum = $change['old']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $oldLineNum = $change['old']['offset'] + $no + 1; + } $html .= '' . - '' . $oldLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' + : '' + ) . '' . $oldLine . '' . - '' . + ($this->options['lineNumbers'] ? + '' + : '' + ) . '' . ''; } @@ -186,41 +214,65 @@ protected function renderTableReplace(array $change): string if (\count($change['old']['lines']) >= \count($change['new']['lines'])) { foreach ($change['old']['lines'] as $no => $oldLine) { - $oldLineNum = $change['old']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $oldLineNum = $change['old']['offset'] + $no + 1; + } if (isset($change['new']['lines'][$no])) { - $newLineNum = $change['old']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $newLineNum = $change['old']['offset'] + $no + 1; + } $newLine = '' . $change['new']['lines'][$no] . ''; } else { - $newLineNum = ''; + if ($this->options['lineNumbers']) { + $newLineNum = ''; + } $newLine = ''; } $html .= '' . - '' . $oldLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' + : '' + ) . '' . $oldLine . '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $newLineNum . '' + : '' + ) . '' . $newLine . '' . ''; } } else { foreach ($change['new']['lines'] as $no => $newLine) { - $newLineNum = $change['new']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $newLineNum = $change['new']['offset'] + $no + 1; + } if (isset($change['old']['lines'][$no])) { - $oldLineNum = $change['old']['offset'] + $no + 1; + if ($this->options['lineNumbers']) { + $oldLineNum = $change['old']['offset'] + $no + 1; + } $oldLine = '' . $change['old']['lines'][$no] . ''; } else { - $oldLineNum = ''; + if ($this->options['lineNumbers']) { + $oldLineNum = ''; + } $oldLine = ''; } $html .= '' . - '' . $oldLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $oldLineNum . '' + : '' + ) . '' . $oldLine . '' . - '' . $newLineNum . '' . + ($this->options['lineNumbers'] ? + '' . $newLineNum . '' + : '' + ) . '' . $newLine . '' . ''; } From 9bfe7ff344c4b2b0d9186840696a8de597d90e4f Mon Sep 17 00:00:00 2001 From: hasan-ozbey <48382593+hasan-ozbey@users.noreply.github.com> Date: Fri, 7 Feb 2020 08:20:53 +0300 Subject: [PATCH 2/3] travis fix --- src/Renderer/Html/Inline.php | 26 +++++++--------------- src/Renderer/Html/SideBySide.php | 38 +++++++++----------------------- 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/src/Renderer/Html/Inline.php b/src/Renderer/Html/Inline.php index 02993da6..3cff16bc 100644 --- a/src/Renderer/Html/Inline.php +++ b/src/Renderer/Html/Inline.php @@ -119,12 +119,10 @@ protected function renderTableEqual(array $change): string // the old and the new may not be exactly the same // because of ignoreCase, ignoreWhitespace, etc foreach ($change['old']['lines'] as $no => $oldLine) { - if ($this->options['lineNumbers']) { - // hmm... but this is a inline renderer - // we could only pick a line from the old or the new to show - $oldLineNum = $change['old']['offset'] + $no + 1; - $newLineNum = $change['new']['offset'] + $no + 1; - } + // hmm... but this is a inline renderer + // we could only pick a line from the old or the new to show + $oldLineNum = $change['old']['offset'] + $no + 1; + $newLineNum = $change['new']['offset'] + $no + 1; $html .= '' . @@ -151,9 +149,7 @@ protected function renderTableInsert(array $change): string $html = ''; foreach ($change['new']['lines'] as $no => $newLine) { - if ($this->options['lineNumbers']) { - $newLineNum = $change['new']['offset'] + $no + 1; - } + $newLineNum = $change['new']['offset'] + $no + 1; $html .= '' . @@ -180,9 +176,7 @@ protected function renderTableDelete(array $change): string $html = ''; foreach ($change['old']['lines'] as $no => $oldLine) { - if ($this->options['lineNumbers']) { - $oldLineNum = $change['old']['offset'] + $no + 1; - } + $oldLineNum = $change['old']['offset'] + $no + 1; $html .= '' . @@ -209,9 +203,7 @@ protected function renderTableReplace(array $change): string $html = ''; foreach ($change['old']['lines'] as $no => $oldLine) { - if ($this->options['lineNumbers']) { - $oldLineNum = $change['old']['offset'] + $no + 1; - } + $oldLineNum = $change['old']['offset'] + $no + 1; $html .= '' . @@ -226,9 +218,7 @@ protected function renderTableReplace(array $change): string } foreach ($change['new']['lines'] as $no => $newLine) { - if ($this->options['lineNumbers']) { - $newLineNum = $change['new']['offset'] + $no + 1; - } + $newLineNum = $change['new']['offset'] + $no + 1; $html .= '' . diff --git a/src/Renderer/Html/SideBySide.php b/src/Renderer/Html/SideBySide.php index 3946bcc9..b9240a8b 100644 --- a/src/Renderer/Html/SideBySide.php +++ b/src/Renderer/Html/SideBySide.php @@ -116,10 +116,8 @@ protected function renderTableEqual(array $change): string foreach ($change['old']['lines'] as $no => $oldLine) { $newLine = $change['new']['lines'][$no]; - if ($this->options['lineNumbers']) { - $oldLineNum = $change['old']['offset'] + $no + 1; - $newLineNum = $change['new']['offset'] + $no + 1; - } + $oldLineNum = $change['old']['offset'] + $no + 1; + $newLineNum = $change['new']['offset'] + $no + 1; $html .= '' . @@ -149,9 +147,7 @@ protected function renderTableInsert(array $change): string $html = ''; foreach ($change['new']['lines'] as $no => $newLine) { - if ($this->options['lineNumbers']) { - $newLineNum = $change['new']['offset'] + $no + 1; - } + $newLineNum = $change['new']['offset'] + $no + 1; $html .= '' . @@ -181,9 +177,7 @@ protected function renderTableDelete(array $change): string $html = ''; foreach ($change['old']['lines'] as $no => $oldLine) { - if ($this->options['lineNumbers']) { - $oldLineNum = $change['old']['offset'] + $no + 1; - } + $oldLineNum = $change['old']['offset'] + $no + 1; $html .= '' . @@ -214,19 +208,13 @@ protected function renderTableReplace(array $change): string if (\count($change['old']['lines']) >= \count($change['new']['lines'])) { foreach ($change['old']['lines'] as $no => $oldLine) { - if ($this->options['lineNumbers']) { - $oldLineNum = $change['old']['offset'] + $no + 1; - } + $oldLineNum = $change['old']['offset'] + $no + 1; if (isset($change['new']['lines'][$no])) { - if ($this->options['lineNumbers']) { - $newLineNum = $change['old']['offset'] + $no + 1; - } + $newLineNum = $change['old']['offset'] + $no + 1; $newLine = '' . $change['new']['lines'][$no] . ''; } else { - if ($this->options['lineNumbers']) { - $newLineNum = ''; - } + $newLineNum = ''; $newLine = ''; } @@ -246,19 +234,13 @@ protected function renderTableReplace(array $change): string } } else { foreach ($change['new']['lines'] as $no => $newLine) { - if ($this->options['lineNumbers']) { - $newLineNum = $change['new']['offset'] + $no + 1; - } + $newLineNum = $change['new']['offset'] + $no + 1; if (isset($change['old']['lines'][$no])) { - if ($this->options['lineNumbers']) { - $oldLineNum = $change['old']['offset'] + $no + 1; - } + $oldLineNum = $change['old']['offset'] + $no + 1; $oldLine = '' . $change['old']['lines'][$no] . ''; } else { - if ($this->options['lineNumbers']) { - $oldLineNum = ''; - } + $oldLineNum = ''; $oldLine = ''; } From eae13f67f955c118c9c2673c4e6accbc561cd9bb Mon Sep 17 00:00:00 2001 From: hasan-ozbey <48382593+hasan-ozbey@users.noreply.github.com> Date: Fri, 7 Feb 2020 11:58:13 +0300 Subject: [PATCH 3/3] fix spaces --- src/Renderer/Html/Inline.php | 4 ++-- src/Renderer/Html/SideBySide.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Renderer/Html/Inline.php b/src/Renderer/Html/Inline.php index 3cff16bc..f02948ae 100644 --- a/src/Renderer/Html/Inline.php +++ b/src/Renderer/Html/Inline.php @@ -66,7 +66,7 @@ protected function renderTableHeader(): string '' : '' ) . - '' . $this->_('differences') . '' . + '' . $this->_('differences') . '' . '' . ''; } @@ -81,7 +81,7 @@ protected function renderTableSeparateBlock(): string return '' . '' . - '' . + '' . '' . ''; } diff --git a/src/Renderer/Html/SideBySide.php b/src/Renderer/Html/SideBySide.php index b9240a8b..e80be691 100644 --- a/src/Renderer/Html/SideBySide.php +++ b/src/Renderer/Html/SideBySide.php @@ -60,8 +60,8 @@ protected function renderTableHeader(): string return '' . '' . - '' . $this->_('old_version') . '' . - '' . $this->_('new_version') . '' . + '' . $this->_('old_version') . '' . + '' . $this->_('new_version') . '' . '' . ''; } @@ -76,7 +76,7 @@ protected function renderTableSeparateBlock(): string return '' . '' . - '' . + '' . '' . ''; }