From 7353fab31669a0d2d6df7317afbce4c62b5f42ed Mon Sep 17 00:00:00 2001 From: alecpl Date: Fri, 29 Aug 2008 08:40:21 +0000 Subject: - Added PRE handling in html2text class (#1484740) --- program/lib/html2text.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'program/lib') diff --git a/program/lib/html2text.php b/program/lib/html2text.php index af8e936cc..c42a5359e 100644 --- a/program/lib/html2text.php +++ b/program/lib/html2text.php @@ -31,6 +31,7 @@ * * *************************************************************************/ +/* 2008/08/29: Added PRE handling by A.L.E.C */ /** * Takes HTML and converts it to formatted, plain text. @@ -198,6 +199,37 @@ class html2text '' ); + /** + * List of preg* regular expression patterns to search for in PRE body, + * used in conjunction with $pre_replace. + * + * @var array $pre_search + * @access public + * @see $pre_replace + */ + $pre_search = array( + "/\n/", + "/\t/", + '/ /', + '/]*>/', + '/<\/pre>/' + ); + + /** + * List of pattern replacements corresponding to patterns searched for PRE body. + * + * @var array $pre_replace + * @access public + * @see $pre_search + */ + $pre_replace = array( + '
', + '    ', + ' ', + '', + '' + ); + /** * Contains a list of HTML tags to allow in the resulting text. * @@ -375,6 +407,9 @@ class html2text $text = trim(stripslashes($this->html)); + // Convert
+	$this->_convert_pre($text);
+
         // Run our defined search-and-replace
         $text = preg_replace($this->search, $this->replace, $text);
 
@@ -444,6 +479,21 @@ class html2text
               
       return $display . ' [' . ($index+1) . ']';
       }
+
+    /**
+     *  Helper function for PRE body conversion.
+     *
+     *  @param string HTML content
+     *  @access private
+    */
+    function _convert_pre(&$text)
+      {
+      while(preg_match('/]*>(.*)<\/pre>/ismU', $text, $matches))
+        {
+          $result = preg_replace($this->pre_search, $this->pre_replace, $matches[1]);
+	  $text = preg_replace('/]*>.*<\/pre>/ismU', '

' . $result . '
', $text); + } + } } -?> \ No newline at end of file +?> -- cgit v1.2.3