From 5f78da3187bd4a4a93d5b2b80a3881f7e26c0756 Mon Sep 17 00:00:00 2001 From: NirvashPrime <37520562+NirvashPrime@users.noreply.github.com> Date: Wed, 9 Oct 2019 13:46:01 -0400 Subject: [PATCH] Update Html2Text.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adjusts handling of empty lines with a new option — keep_empty_lines --- src/Html2Text.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Html2Text.php b/src/Html2Text.php index 5153cef..0d37538 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -6,8 +6,9 @@ class Html2Text { public static function defaultOptions() { return array( - 'ignore_errors' => false, - 'drop_links' => false, + 'ignore_errors' => false, + 'drop_links' => false, + 'keep_empty_lines' => false, ); } @@ -59,7 +60,7 @@ public static function convert($html, $options = array()) { $output = static::iterateOverNode($doc, null, false, $is_office_document, $options); // process output for whitespace/newlines - $output = static::processWhitespaceNewlines($output); + $output = static::processWhitespaceNewlines($output, $options['keep_empty_lines']); return $output; } @@ -99,9 +100,10 @@ static function zwnjCodes() { * Remove leading or trailing spaces and excess empty lines from provided multiline text * * @param string $text multiline text any number of leading or trailing spaces or excess lines + * @param boolean $remove_empty_lines remove unnecessary empty lines or not * @return string the fixed text */ - static function processWhitespaceNewlines($text) { + static function processWhitespaceNewlines($text, $keep_empty_lines = false) { // remove excess spaces around tabs $text = preg_replace("/ *\t */im", "\t", $text); @@ -127,7 +129,9 @@ static function processWhitespaceNewlines($text) { $text = static::fixNewLines($text); // remove unnecessary empty lines - $text = preg_replace("/\n\n\n*/im", "\n\n", $text); + if (!$keep_empty_lines) { + $text = preg_replace("/\n\n\n*/im", "\n\n", $text); + } return $text; } @@ -482,7 +486,7 @@ static function iterateOverNode($node, $prevName = null, $in_pre = false, $is_of case "blockquote": // process quoted text for whitespace/newlines - $output = static::processWhitespaceNewlines($output); + $output = static::processWhitespaceNewlines($output, $options['keep_empty_lines']); // add leading newline $output = "\n" . $output;