Skip to content

Generators/Text: various minor code simplifications #824

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 2 commits into from
Feb 19, 2025
Merged
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
47 changes: 6 additions & 41 deletions src/Generators/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,46 +130,13 @@ protected function getFormattedTextBlock(DOMNode $node)
}

$text = trim($text);
$text = str_replace('<em>', '*', $text);
$text = str_replace('</em>', '*', $text);
$text = str_replace(['<em>', '</em>'], '*', $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;
}

$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.' ';
}
}//end foreach

if ($tempLine !== '') {
$lines[] = rtrim($tempLine);
}
}//end foreach
$nodeLines = array_map('trim', $nodeLines);
$text = implode(PHP_EOL, $nodeLines);

return implode(PHP_EOL, $lines).PHP_EOL.PHP_EOL;
return wordwrap($text, 100, PHP_EOL).PHP_EOL.PHP_EOL;

}//end getFormattedTextBlock()

Expand Down Expand Up @@ -243,8 +210,7 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
$firstTitleLines[] = $tempTitle;
}

$first = str_replace('<em>', '', $first);
$first = str_replace('</em>', '', $first);
$first = str_replace(['<em>', '</em>'], '', $first);
$firstLines = explode("\n", $first);

$second = trim($secondCodeElm->nodeValue);
Expand Down Expand Up @@ -278,8 +244,7 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node)
$secondTitleLines[] = $tempTitle;
}

$second = str_replace('<em>', '', $second);
$second = str_replace('</em>', '', $second);
$second = str_replace(['<em>', '</em>'], '', $second);
$secondLines = explode("\n", $second);

$titleRow = '';
Expand Down
Loading