Skip to content

deduplicate render code #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/jblond/Diff/Renderer/Html/HtmlArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,54 @@ public function mbSubstrReplace(string $string, string $replacement, int $start,
return join($smatches['0']);
}

/**
* @param string|array $changes
* @param SideBySide|Inline $object
* @return string
*/
public function renderHtml($changes, $object)
{
$html = '';
if (empty($changes)) {
return $html;
}

$html .= $object->generateTableHeader();

foreach ($changes as $i => $blocks) {
// If this is a separate block, we're condensing code so output ...,
// indicating a significant portion of the code has been collapsed as
// it is the same
if ($i > 0) {
$html .= $object->generateSkippedTable();
}

foreach ($blocks as $change) {
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
switch ($change['tag']) {
// Equal changes should be shown on both sides of the diff
case 'equal':
$html .= $object->generateTableRowsEqual($change);
break;
// Added lines only on the right side
case 'insert':
$html .= $object->generateTableRowsInsert($change);
break;
// Show deleted lines only on the left side
case 'delete':
$html .= $object->generateTableRowsDelete($change);
break;
// Show modified lines on both sides
case 'replace':
$html .= $object->generateTableRowsReplace($change);
break;
}
$html .= '</tbody>';
}
}
$html .= '</table>';
return $html;
}
/**
* Render and return an array structure suitable for generating HTML
* based differences. Generally called by subclasses that generate a
Expand Down
53 changes: 7 additions & 46 deletions lib/jblond/Diff/Renderer/Html/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,7 @@ class Inline extends HtmlArray
public function render() : string
{
$changes = parent::render();
$html = '';
if (empty($changes)) {
return $html;
}

$html .= $this->generateTableHeader();

foreach ($changes as $i => $blocks) {
// If this is a separate block, we're condensing code so output ...,
// indicating a significant portion of the code has been collapsed as
// it is the same
if ($i > 0) {
$html .= $this->generateSkippedTable();
}

foreach ($blocks as $change) {
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
switch ($change['tag']) {
// Equal changes should be shown on both sides of the diff
case 'equal':
$html .= $this->generateTableRowsEqual($change);
break;
// Added lines only on the right side
case 'insert':
$html .= $this->generateTableRowsInsert($change);
break;
// Show deleted lines only on the left side
case 'delete':
$html .= $this->generateTableRowsDelete($change);
break;
// Show modified lines on both sides
case 'replace':
$html .= $this->generateTableRowsReplace($change);
break;
}
$html .= '</tbody>';
}
}
$html .= '</table>';
return $html;
return parent::renderHtml($changes, $this);
}


Expand All @@ -106,7 +67,7 @@ public function render() : string
*
* @return string Html code representation of the table's header.
*/
private function generateTableHeader() : string
public function generateTableHeader() : string
{
$html = '<table class="Differences DifferencesInline">';
$html .= '<thead>';
Expand All @@ -124,7 +85,7 @@ private function generateTableHeader() : string
*
* @return string Html code representing empty table body.
*/
private function generateSkippedTable() : string
public function generateSkippedTable() : string
{
$html = '<tbody class="Skipped">';
$html .= '<th>&hellip;</th>';
Expand All @@ -140,7 +101,7 @@ private function generateSkippedTable() : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of text with no difference.
*/
private function generateTableRowsEqual(array &$change) : string
public function generateTableRowsEqual(array &$change) : string
{
$html = "";
foreach ($change['base']['lines'] as $no => $line) {
Expand All @@ -161,7 +122,7 @@ private function generateTableRowsEqual(array &$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of added text.
*/
private function generateTableRowsInsert(array &$change) : string
public function generateTableRowsInsert(array &$change) : string
{
$html = "";
foreach ($change['changed']['lines'] as $no => $line) {
Expand All @@ -181,7 +142,7 @@ private function generateTableRowsInsert(array &$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of removed text.
*/
private function generateTableRowsDelete(array &$change) : string
public function generateTableRowsDelete(array &$change) : string
{
$html = "";
foreach ($change['base']['lines'] as $no => $line) {
Expand All @@ -201,7 +162,7 @@ private function generateTableRowsDelete(array &$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of modified.
*/
private function generateTableRowsReplace(array &$change) : string
public function generateTableRowsReplace(array &$change) : string
{
$html = "";

Expand Down
54 changes: 7 additions & 47 deletions lib/jblond/Diff/Renderer/Html/SideBySide.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,47 +57,7 @@ class SideBySide extends HtmlArray
public function render() : string
{
$changes = parent::render();

$html = '';
if (empty($changes)) {
return $html;
}

$html .= $this->generateTableHeader();

foreach ($changes as $i => $blocks) {
// If this is a separate block, we're condensing code so output ...,
// indicating a significant portion of the code has been collapsed as
// it is the same
if ($i > 0) {
$html .= $this->generateSkippedTable();
}

foreach ($blocks as $change) {
$html .= '<tbody class="Change'.ucfirst($change['tag']).'">';
switch ($change['tag']) {
// Equal changes should be shown on both sides of the diff
case 'equal':
$html .= $this->generateTableRowsEqual($change);
break;
// Added lines only on the right side
case 'insert':
$html .= $this->generateTableRowsInsert($change);
break;
// Show deleted lines only on the left side
case 'delete':
$html .= $this->generateTableRowsDelete($change);
break;
// Show modified lines on both sides
case 'replace':
$html .= $this->generateTableRowsReplace($change);
break;
}
$html .= '</tbody>';
}
}
$html .= '</table>';
return $html;
return parent::renderHtml($changes, $this);
}

/**
Expand All @@ -106,7 +66,7 @@ public function render() : string
*
* @return string Html code representation of the table's header.
*/
private function generateTableHeader() : string
public function generateTableHeader() : string
{
$html = '<table class="Differences DifferencesSideBySide">';
$html .= '<thead>';
Expand All @@ -123,7 +83,7 @@ private function generateTableHeader() : string
*
* @return string Html code representing empty table body.
*/
private function generateSkippedTable() : string
public function generateSkippedTable() : string
{
$html = '<tbody class="Skipped">';
$html .= '<th>&hellip;</th><td>&#xA0;</td>';
Expand All @@ -138,7 +98,7 @@ private function generateSkippedTable() : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of text with no difference.
*/
private function generateTableRowsEqual(array &$change) : string
public function generateTableRowsEqual(array &$change) : string
{
$html = "";
foreach ($change['base']['lines'] as $no => $line) {
Expand All @@ -160,7 +120,7 @@ private function generateTableRowsEqual(array &$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of added text.
*/
private function generateTableRowsInsert(array &$change) : string
public function generateTableRowsInsert(array &$change) : string
{
$html = "";
foreach ($change['changed']['lines'] as $no => $line) {
Expand All @@ -181,7 +141,7 @@ private function generateTableRowsInsert(array &$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of removed text.
*/
private function generateTableRowsDelete(array &$change) : string
public function generateTableRowsDelete(array &$change) : string
{
$html = "";
foreach ($change['base']['lines'] as $no => $line) {
Expand All @@ -202,7 +162,7 @@ private function generateTableRowsDelete(array &$change) : string
* @param array &$change Array with data about changes.
* @return string Html code representing one or more rows of modified.
*/
private function generateTableRowsReplace(array &$change) : string
public function generateTableRowsReplace(array &$change) : string
{
$html = "";

Expand Down