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 Jun 27, 2019
1 parent 0a42dba commit 0a24da0
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,32 +75,40 @@ protected function printTextBlock(\DOMNode $node)
$text = str_replace('<em>', '*', $text);
$text = str_replace('</em>', '*', $text);

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

foreach ($node_lines as $current_line) {
$current_line = trim($current_line);
if ($current_line === '') {
// 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(' ', $current_line);

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

if ($current_length === 99 || $current_length === 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
}//end foreach

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

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

0 comments on commit 0a24da0

Please sign in to comment.