Skip to content

Commit

Permalink
Merge branch 'feature/generator-text-line-wrapping-bug-fix' of https:…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Sep 19, 2019
2 parents 234688d + b109358 commit 9121852
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 9121852

Please sign in to comment.