From 74bb1fa72f9bd87f5b29715f084631edf3159241 Mon Sep 17 00:00:00 2001 From: Marcin Drazek Date: Fri, 8 Jul 2022 16:06:37 +0200 Subject: [PATCH] one_newline_tags option --- src/Html2Text.php | 16 ++++++++++------ tests/Html2TextTest.php | 4 ++++ tests/lists-one-newline-ul.html | 24 ++++++++++++++++++++++++ tests/lists-one-newline-ul.txt | 16 ++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 tests/lists-one-newline-ul.html create mode 100644 tests/lists-one-newline-ul.txt diff --git a/src/Html2Text.php b/src/Html2Text.php index ffeb377..a40ddd2 100644 --- a/src/Html2Text.php +++ b/src/Html2Text.php @@ -8,6 +8,7 @@ public static function defaultOptions() { return array( 'ignore_errors' => false, 'drop_links' => false, + 'one_newline_tags' => [] ); } @@ -258,7 +259,6 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, $name = strtolower($node->nodeName); $nextName = static::nextChildName($node); - // start whitespace switch ($name) { case "hr": @@ -285,8 +285,12 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, case "ol": case "ul": case "pre": - // add two newlines - $output = "\n\n"; + if(in_array($name, $options['one_newline_tags'])){ + $output = "\n"; + }else{ + // add two newlines + $output = "\n\n"; + } break; case "td": @@ -334,7 +338,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, $output = ""; break; } - + // debug //$output .= "[$name,$nextName]"; @@ -350,7 +354,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, while ($n != null) { $text = static::iterateOverNode($n, $previousSiblingName, $in_pre || $name == 'pre', $is_office_document, $options); - + // Pass current node name to next child, as previousSibling does not appear to get populated if ($n instanceof \DOMDocumentType || $n instanceof \DOMProcessingInstruction @@ -499,7 +503,7 @@ static function iterateOverNode($node, $prevName, $in_pre, $is_office_document, default: // do nothing } - + return $output; } } diff --git a/tests/Html2TextTest.php b/tests/Html2TextTest.php index bf32b1b..230bb99 100644 --- a/tests/Html2TextTest.php +++ b/tests/Html2TextTest.php @@ -136,5 +136,9 @@ function testBasicDropLinks() { function testAnchorsDropLinks() { $this->doTestWithResults("anchors", "anchors.no-links", array('drop_links' => true)); } + + function testOneNewlineTagsOption() { + $this->doTestWithResults("lists-one-newline-ul", "lists-one-newline-ul", array('one_newline_tags' => ['ul'])); + } } \ No newline at end of file diff --git a/tests/lists-one-newline-ul.html b/tests/lists-one-newline-ul.html new file mode 100644 index 0000000..7189a30 --- /dev/null +++ b/tests/lists-one-newline-ul.html @@ -0,0 +1,24 @@ +

List tests

+ +

+Add some lists. +

+ +
    +
  1. one
  2. +
  3. two +
  4. three +
+ +
An unordered list
+ + + \ No newline at end of file diff --git a/tests/lists-one-newline-ul.txt b/tests/lists-one-newline-ul.txt new file mode 100644 index 0000000..fa246e8 --- /dev/null +++ b/tests/lists-one-newline-ul.txt @@ -0,0 +1,16 @@ +List tests + +Add some lists. + +- one +- two +- three + +An unordered list +- one +- two +- three + +- one +- two +- three \ No newline at end of file