Skip to content

Commit

Permalink
Generator/Text: allow for multi-line rule explanations
Browse files Browse the repository at this point in the history
While new lines were (accidentally) being respected by the existing code, the line length count was not being reset when a new line character was encountered, causing weird line wrapping.

This has been fixed now by:
* First splitting the received text into individual lines.
* Then doing the line wrapping calculations for each line individually.

Includes:
* Reducing the code complexity by removing an `else` and using an early `continue` instead.
* Removing some duplicate function calls.
  • Loading branch information
jrfnl committed Jul 10, 2019
1 parent 0a42dba commit b109358
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/Generators/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,42 @@ protected function printTextBlock(\DOMNode $node)
$text = str_replace('<em>', '*', $text);
$text = str_replace('</em>', '*', $text);

$lines = [];
$tempLine = '';
$words = explode(' ', $text);
$nodeLines = explode("\n", $text);
$lines = [];

foreach ($nodeLines as $currentLine) {
$currentLine = trim($currentLine);
if ($currentLine === '') {
// The text contained a blank line. Respect this.
$lines[] = '';
continue;
}

foreach ($words as $word) {
if (strlen($tempLine.$word) >= 99) {
if (strlen($tempLine.$word) === 99) {
// Adding the extra space will push us to the edge
// so we are done.
$lines[] = $tempLine.$word;
$tempLine = '';
} else if (strlen($tempLine.$word) === 100) {
$tempLine = '';
$words = explode(' ', $currentLine);

foreach ($words as $word) {
$currentLength = strlen($tempLine.$word);
if ($currentLength < 99) {
$tempLine .= $word.' ';
continue;
}

if ($currentLength === 99 || $currentLength === 100) {
// We are already at the edge, so we are done.
$lines[] = $tempLine.$word;
$tempLine = '';
} else {
$lines[] = rtrim($tempLine);
$tempLine = $word.' ';
}
} else {
$tempLine .= $word.' ';
}//end foreach

if ($tempLine !== '') {
$lines[] = rtrim($tempLine);
}
}//end foreach

if ($tempLine !== '') {
$lines[] = rtrim($tempLine);
}

echo implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;

}//end printTextBlock()
Expand Down

0 comments on commit b109358

Please sign in to comment.