diff --git a/lib/html2text.php b/lib/html2text.php index b7e3e3e9ec420..0c29cce8fd99d 100644 --- a/lib/html2text.php +++ b/lib/html2text.php @@ -219,7 +219,7 @@ class html2text '-', '*', '£', - 'EUR', // Euro sign. € ? + 'EUR', // Euro sign. € ? ' ' // Runs of spaces, post-handling ); @@ -237,6 +237,7 @@ class html2text '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i', // '/<(th)[^>]*>(.*?)<\/th>/i', // and + '/<(img)[^>]*alt=\"([^>"]+)\"[^>]*>/i', // with alt ); /** @@ -574,6 +575,8 @@ function _preg_callback($matches) 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 1d16b200387c6..340fd42646540 100644 --- a/lib/html2text_readme.txt +++ b/lib/html2text_readme.txt @@ -36,3 +36,28 @@ instead of: -- Francois Marier 2009-05-22 + + +2- 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 b9dcbf56d7a10..eaee67b1c2e24 100644 --- a/lib/simpletest/testweblib.php +++ b/lib/simpletest/testweblib.php @@ -238,5 +238,18 @@ function test_convert_urls_into_links() { $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 1d625e13860fe..7e63178df311e 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2288,17 +2288,18 @@ function markdown_to_html($text) { /** * Given HTML text, make it into plain text using external function * - * @uses $CFG * @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;