Incremental improvements in Html2text formatting
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 19 Feb 2010 18:19:08 +0000 (11:19 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 19 Feb 2010 18:23:16 +0000 (11:23 -0700)
framework/Text_Filter/lib/Horde/Text/Filter/Html2text.php
framework/Text_Filter/test/Horde/Text/Filter/html2text2.phpt

index a224862..46be644 100644 (file)
@@ -75,6 +75,9 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
             // Newlines and tabs.
             '/[\n\t]+/' => ' ',
 
+            // Normalize <br> (remove leading/trailing whitespace)
+            '/\s*<br[^>]*>\s*/i' => '<br>',
+
             // <script>s -- which strip_tags() supposedly has problems with.
             '/<script(?:>|\s[^>]*>).*?<\/script\s*>/i' => '',
 
@@ -135,7 +138,7 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
             '/<div class=rte>(.+?)<\/div> ?/i' => '\\1<br>',
 
             // <br>
-            '/\s*<br[^>]*>\s*/i' => "\n"
+            '/<br[^>]*>/i' => "\n"
         );
 
         $regexp_callback = array(
@@ -191,7 +194,7 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
             }
         }
 
-        return trim($text);
+        return trim(rtrim($text), "\n");
     }
 
     /**
@@ -223,7 +226,7 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
             $text = stripslashes($this->_blockQuote($text));
         }
 
-        $text = trim(strip_tags($text));
+        $text = rtrim(strip_tags($text));
         if ($this->_params['wrap']) {
             $text = wordwrap($text, $this->_params['width'] - 2);
         }
index ebbd3ed..34dbd18 100644 (file)
@@ -16,7 +16,9 @@ $html = <<<EOT
   <li>And again a short line.</li>
 </ul>
 EOT;
+
 echo Horde_Text_Filter::filter($html, 'html2text', array('width' => 50));
+echo "\n\n";
 echo Horde_Text_Filter::filter($html, 'html2text', array('wrap' => false));
 
 ?>
@@ -30,8 +32,6 @@ This is a long line. This is a long line. This is
 a long line.
   * And again a short line.
 
-
-
   * This is a short line.
   * This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line.
   * And again a short line.