Html2text filter will use horde/Text_Filter, if available, to wrap text
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 2 Sep 2010 17:56:00 +0000 (11:56 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 2 Sep 2010 17:56:00 +0000 (11:56 -0600)
framework/Text_Filter/lib/Horde/Text/Filter/Html2text.php
framework/Text_Filter/package.xml

index e9affe5..693dd01 100644 (file)
@@ -79,6 +79,7 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
     {
         $this->_indent = 0;
         $this->_linkList = array();
+        $this->_params['_bq'] = false;
 
         return $text;
     }
@@ -95,8 +96,10 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
         try {
             $dom = new Horde_Domhtml($text, $this->_params['charset']);
             $text = Horde_String::convertCharset($this->_node($dom->dom, $dom->dom), null, $this->_params['charset']);
+            $dom_convert = true;
         } catch (Exception $e) {
             $text = strip_tags(preg_replace("/\<br\s*\/?\>/i", "\n", $text));
+            $dom_convert = false;
         }
 
         /* Bring down number of empty lines to 2 max, and remove trailing
@@ -105,7 +108,15 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
 
         /* Wrap the text to a readable format. */
         if ($this->_params['width']) {
-            $text = wordwrap($text, $this->_params['width']);
+            if ($dom_convert &&
+                $this->_params['_bq'] &&
+                class_exists('Horde_Text_Flowed')) {
+                $flowed = new Horde_Text_Flowed($text, $this->_params['charset']);
+                $flowed->setOptLength($this->_params['width']);
+                $text = $flowed->toFlowed();
+            } else {
+                $text = wordwrap($text, $this->_params['width']);
+            }
         }
 
         /* Add link list. */
@@ -216,9 +227,11 @@ class Horde_Text_Filter_Html2text extends Horde_Text_Filter_Base
                         break;
 
                     case 'blockquote':
+                        $this->_params['_bq'] = true;
                         foreach (explode("\n", $this->_node($doc, $child)) as $line) {
                             $quote = '>';
-                            if (($line[0] != '>') &&
+                            if ($line &&
+                                ($line[0] != '>') &&
                                 ($line[0] != ' ' || $line[0] != "\t")) {
                                 $quote .= ' ';
                             }
index a5d26df..0e6129c 100644 (file)
@@ -34,7 +34,8 @@
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes> * Add callback parameter to the Html2text filter.
+ <notes>* Html2text filter will use horde/Text_Filter, if available, to wrap text.
+ * Add callback parameter to the Html2text filter.
  * XSS filter now uses PHP DOM parser to process incoming text.
  * Remove Horde/Core dependency.
  * Add Horde_Text_Filter_Exception::.
     <name>Text_Filter_Csstidy</name>
     <channel>pear.horde.org</channel>
    </package>
+   <package>
+    <name>Text_Flowed</name>
+    <channel>pear.horde.org</channel>
+   </package>
    <extension>
     <name>dom</name>
    </extension>