[dev] Patch: smartTabExpansion...

Marc Jauvin marc at register4less.com
Fri May 30 11:19:42 PDT 2003


Here is the latest version with the 'smartExpandTabs()' function within the
'Text::' class

Files modified:
  - /horde/lib/Text.php
  - /horde/imp/lib/MIME/Viewer/plain.php

Please commit (or give comment if there are problems with it).


--
Marc Jauvin
450-441-5458
http://register4less.com
-------------- next part --------------
Index: Text.php
===================================================================
RCS file: /repository/horde/lib/Text.php,v
retrieving revision 1.56
diff -u -r1.56 Text.php
--- Text.php	9 May 2003 15:04:45 -0000	1.56
+++ Text.php	30 May 2003 18:15:56 -0000
@@ -512,4 +512,28 @@
         }
     }
 
+    /**
+     * Expand tabs into spaces
+     *
+     * @param string $text                 The text to expand
+     *
+     * @param optional integer $tabstop    Expand Tabs into that many spaces.
+     *
+     * @param optional string $break_char  Character(s) to use when breaking
+     *                                     lines.
+     * @return string  the text after tab expansion
+     */
+    function smartExpandTabs($text, $tabstop=8, $break_char="\n")
+    {
+        $lines = explode($break_char, $text);
+        for ($i = 0; $i < sizeof($lines); $i++) {
+            while( ($pos=strpos($lines[$i], "\t")) !== false) {
+                $n_space = $tabstop - ($pos % $tabstop);
+                $new_str = str_repeat(" ", $n_space);
+                $lines[$i] = substr_replace($lines[$i], $new_str, $pos, 1);
+            }
+        }
+        return implode("\n", $lines);
+    }
+
 }
-------------- next part --------------
Index: plain.php
===================================================================
RCS file: /repository/imp/lib/MIME/Viewer/plain.php,v
retrieving revision 1.21
diff -u -r1.21 plain.php
--- plain.php	16 May 2003 15:26:13 -0000	1.21
+++ plain.php	30 May 2003 18:15:01 -0000
@@ -84,8 +84,10 @@
         // Filter bad language.
         $text = IMP::filterText($text);
 
+        // Expand Tabs with respect to tab stops
+        $text = Text::smartExpandTabs($text);
+
         // Wordwrap.
-        $text = str_replace("\t", '        ', $text);
         $text = str_replace('  ', ' &nbsp;', $text);
         $text = str_replace("\n ", "\n&nbsp;", $text);
         if ($text{0} == ' ') {
@@ -153,5 +155,4 @@
             return parent::getCharset();
         }
     }
-
 }


More information about the dev mailing list