diff --git a/lib/html2text.php b/lib/html2text.php index 56112ea86fb..27f01e04ece 100644 --- a/lib/html2text.php +++ b/lib/html2text.php @@ -211,7 +211,7 @@ class html2text '-', '*', '£', - 'EUR', // Euro sign. � ? + 'EUR', // Euro sign. € ? ' ' // Runs of spaces, post-handling ); @@ -229,6 +229,7 @@ class html2text '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i', // '/<(th)[^>]*>(.*?)<\/th>/i', // and + '/<(img)[^>]*alt=\"([^>"]+)\"[^>]*>/i', // with alt ); /** @@ -565,6 +566,8 @@ class html2text return $this->_strtoupper("\n\n". $matches[2] ."\n\n"); case 'a': return $this->_build_link_list($matches[3], $matches[4]); + case 'img': + return '[' . $matches[2] . ']'; } } diff --git a/lib/html2text_readme.txt b/lib/html2text_readme.txt index 8ed6ca73f33..2e2c077e074 100644 --- a/lib/html2text_readme.txt +++ b/lib/html2text_readme.txt @@ -3,3 +3,31 @@ html2text.php is an unmodified copy of a file shipped with the RoundCube project http://trac.roundcube.net/log/trunk/roundcubemail/program/lib/html2text.php -- Francois Marier 2009-05-22 + + +Modifications +-------------- + +1- Don't just strip images, replace them with their alt text. + +index b7e3e3e..96ef508 100644 +--- a/lib/html2text.php ++++ b/lib/html2text.php +@@ -237,6 +237,7 @@ class html2text + '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i', + // + '/<(th)[^>]*>(.*?)<\/th>/i', // and ++ '/<(img)[^>]*alt=\"([^>"]+)\"[^>]*>/i', // with alt + ); + + /** +@@ -574,6 +575,8 @@ class html2text + return $this->_strtoupper("\n\n". $matches[2] ."\n\n"); + case 'a': + return $this->_build_link_list($matches[3], $matches[4]); ++ case 'img': ++ return '[' . $matches[2] . ']'; + } + } + + -- Tim Hunt 2010-08-04 \ No newline at end of file diff --git a/lib/simpletest/testweblib.php b/lib/simpletest/testweblib.php index 7bf5843a99a..a5a3c61c15a 100644 --- a/lib/simpletest/testweblib.php +++ b/lib/simpletest/testweblib.php @@ -285,6 +285,19 @@ END; $this->assertEqual($fast_enough, true, 'Timing test: ' . $new_time . 'secs (new) < ' . $old_time . 'secs (old)'); } + + public function test_html_to_text_simple() { + $this->assertEqual("\n\n_Hello_ WORLD!", html_to_text('

Hello world!

')); + } + + public function test_html_to_text_image() { + $this->assertEqual('[edit]', html_to_text('edit')); + } + + public function test_html_to_text_nowrap() { + $long = "Here is a long string, more than 75 characters long, since by default html_to_text wraps text at 75 chars."; + $this->assertEqual($long, html_to_text($long, 0)); + } } diff --git a/lib/weblib.php b/lib/weblib.php index cb3409cfcd0..8bbc9c6c046 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1766,17 +1766,18 @@ function markdown_to_html($text) { /** * Given HTML text, make it into plain text using external function * - * @global object * @param string $html The text to be converted. - * @return string + * @param integer $width Width to wrap the text at. (optional, default 75 which + * is a good value for email. 0 means do not limit line length.) + * @return string plain text equivalent of the HTML. */ -function html_to_text($html) { +function html_to_text($html, $width = 75) { global $CFG; require_once($CFG->libdir .'/html2text.php'); - $h2t = new html2text($html); + $h2t = new html2text($html, false, true, $width); $result = $h2t->get_text(); return $result;