Add option to not wrap unquoted lines when converting to flowed
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 30 Sep 2010 22:24:39 +0000 (16:24 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 30 Sep 2010 22:57:46 +0000 (16:57 -0600)
framework/Text_Flowed/lib/Horde/Text/Flowed.php
framework/Text_Flowed/test/Horde/Text/Flowed/FlowedTest.php

index 2ba93ba..a099283 100644 (file)
@@ -159,14 +159,19 @@ class Horde_Text_Flowed
      * text as described in RFC 2646.
      *
      * @param boolean $quote  Add level of quoting to each line?
+     * @param array $opts     Additional options:
+     * <pre>
+     * 'nowrap' - (boolean) If true, does not wrap unquoted lines.
+     *            DEFAULT: false
+     * </pre>
      *
      * @return string  The text converted to RFC 2646 'flowed' format.
      */
-    public function toFlowed($quote = false)
+    public function toFlowed($quote = false, array $opts = array())
     {
         $txt = '';
 
-        $this->_reformat(true, $quote);
+        $this->_reformat(true, $quote, empty($opts['nowrap']));
         reset($this->_output);
         while (list(,$line) = each($this->_output)) {
             $txt .= $line['text'] . "\n";
@@ -181,8 +186,9 @@ class Horde_Text_Flowed
      *
      * @param boolean $toflowed  Convert to flowed?
      * @param boolean $quote     Add level of quoting to each line?
+     * @param boolean $wrap      Wrap unquoted lines?
      */
-    protected function _reformat($toflowed, $quote)
+    protected function _reformat($toflowed, $quote, $wrap = true)
     {
         $format_type = implode('|', array($toflowed, $quote));
         if ($format_type == $this->_formattype) {
@@ -262,7 +268,9 @@ class Horde_Text_Flowed
             if (empty($line)) {
                 /* Line is empty. */
                 $this->_output[] = array('text' => $quotestr, 'level' => $num_quotes);
-            } elseif (empty($this->_maxlength) || ((Horde_String::length($line, $this->_charset) + $num_quotes) <= $this->_maxlength)) {
+            } elseif ((!$wrap && !$num_quotes) ||
+                      empty($this->_maxlength) ||
+                      ((Horde_String::length($line, $this->_charset) + $num_quotes) <= $this->_maxlength)) {
                 /* Line does not require rewrapping. */
                 $this->_output[] = array('text' => $quotestr . $this->_stuff($line, $num_quotes, $toflowed), 'level' => $num_quotes);
             } else {
index d4e3e41..20eb098 100644 (file)
@@ -45,6 +45,28 @@ class Horde_Text_Flowed_FlowedTest extends PHPUnit_Framework_TestCase
         );
     }
 
+    public function testFlowedWrap()
+    {
+        $text = <<<EOT
+>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
+EOT;
+        $expected = <<<EOT
+> 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
+
+EOT;
+
+        $flowed = new Horde_Text_Flowed($text);
+        $flowed->setMaxLength(70);
+        $this->assertEquals(
+            $expected,
+            $flowed->toFlowed(false, array('nowrap' => true))
+        );
+
+    }
+
     public function testFlowedToFixed()
     {
         $flowed = new Horde_Text_Flowed(">line 1 \n>line 2 \n>line 3");