From eda92ed4c0d2735144df8fa2136584de69634bdb Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sun, 11 May 2014 11:03:45 +0200 Subject: Improved display of plain text messages and text to HTML conversion (#1488937) Now instead of
 we use 
styled with monospace font. We replace whitespace characters with non-breaking spaces where needed. I.e. plain text is always unwrappable, until it uses format=flowed, in such a case only flowed paragraphs are wrappable. Also conversion of text to HTML in compose editor was modified in the same way. --- program/lib/Roundcube/rcube_text2html.php | 277 ++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 program/lib/Roundcube/rcube_text2html.php (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php new file mode 100644 index 000000000..5da771ac8 --- /dev/null +++ b/program/lib/Roundcube/rcube_text2html.php @@ -0,0 +1,277 @@ + | + +-----------------------------------------------------------------------+ + */ + +/** + * Converts plain text to HTML + * + * @package Framework + * @subpackage Utils + */ +class rcube_text2html +{ + /** + * Contains the HTML content after conversion. + * + * @var string $html + */ + protected $html; + + /** + * Contains the plain text. + * + * @var string $text + */ + protected $text; + + /** + * Configuration + * + * @var array $config + */ + protected $config = array( + // non-breaking space + 'space' => "\xC2\xA0", + // enables format=flowed parser + 'flowed' => false, + // enables wrapping for non-flowed text + 'wrap' => false, + // line-break tag + 'break' => "
\n", + // prefix and suffix (wrapper element) + 'begin' => '
', + 'end' => '', + // enables links replacement + 'links' => true, + ); + + + /** + * Constructor. + * + * If the plain text source string (or file) is supplied, the class + * will instantiate with that source propagated, all that has + * to be done it to call get_html(). + * + * @param string $source Plain text + * @param boolean $from_file Indicates $source is a file to pull content from + * @param array $config Class configuration + */ + function __construct($source = '', $from_file = false, $config = array()) + { + if (!empty($source)) { + $this->set_text($source, $from_file); + } + + if (!empty($config) && is_array($config)) { + $this->config = array_merge($this->config, $config); + } + } + + /** + * Loads source text into memory, either from $source string or a file. + * + * @param string $source Plain text + * @param boolean $from_file Indicates $source is a file to pull content from + */ + function set_text($source, $from_file = false) + { + if ($from_file && file_exists($source)) { + $this->text = file_get_contents($source); + } + else { + $this->text = $source; + } + + $this->_converted = false; + } + + /** + * Returns the HTML content. + * + * @return string HTML content + */ + function get_html() + { + if (!$this->_converted) { + $this->_convert(); + } + + return $this->html; + } + + /** + * Prints the HTML. + */ + function print_html() + { + print $this->get_html(); + } + + /** + * Workhorse function that does actual conversion (calls _converter() method). + */ + protected function _convert() + { + $text = stripslashes($this->text); + + // Convert TXT to HTML + $this->html = $this->_converter($text); + $this->_converted = true; + } + + /** + * Workhorse function that does actual conversion. + * + * @param string Plain text + */ + protected function _converter($text) + { + // make links and email-addresses clickable + $attribs = array('link_attribs' => array('rel' => 'noreferrer', 'target' => '_blank')); + $replacer = new rcmail_string_replacer($attribs); + + if ($this->config['flowed']) { + $flowed_char = 0x01; + $text = rcube_mime::unfold_flowed($text, chr($flowed_char)); + } + + // search for patterns like links and e-mail addresses and replace with tokens + if ($this->config['links']) { + $text = $replacer->replace($text); + } + + // split body into single lines + $text = preg_split('/\r?\n/', $text); + $quote_level = 0; + $last = -1; + + // find/mark quoted lines... + for ($n=0, $cnt=count($text); $n < $cnt; $n++) { + $flowed = false; + if ($this->config['flowed'] && ord($text[0]) == $flowed_char) { + $flowed = true; + $text[$n] = substr($text[$n], 1); + } + + if ($text[$n][0] == '>' && preg_match('/^(>+ {0,1})+/', $text[$n], $regs)) { + $q = substr_count($regs[0], '>'); + $text[$n] = substr($text[$n], strlen($regs[0])); + $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); + + if ($q > $quote_level) { + $text[$n] = $replacer->get_replacement($replacer->add( + str_repeat('
', $q - $quote_level))) . $text[$n]; + $last = $n; + } + else if ($q < $quote_level) { + $text[$n] = $replacer->get_replacement($replacer->add( + str_repeat('
', $quote_level - $q))) . $text[$n]; + $last = $n; + } + } + else { + $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); + $q = 0; + + if ($quote_level > 0) { + $text[$n] = $replacer->get_replacement($replacer->add( + str_repeat('', $quote_level))) . $text[$n]; + } + } + + $quote_level = $q; + } + + if ($quote_level > 0) { + $text[$n] = $replacer->get_replacement($replacer->add( + str_repeat('', $quote_level))) . $text[$n]; + } + + $text = join("\n", $text); + + // colorize signature (up to lines) + $len = strlen($text); + $sig_max_lines = rcube::get_instance()->config->get('sig_max_lines', 15); + + while (($sp = strrpos($text, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) { + if ($sp == 0 || $text[$sp-1] == "\n") { + // do not touch blocks with more that X lines + if (substr_count($text, "\n", $sp) < $sig_max_lines) { + $text = substr($text, 0, max(0, $sp)) + .''.substr($text, $sp).''; + } + + break; + } + } + + // insert url/mailto links and citation tags + $text = $replacer->resolve($text); + + // replace \n before + $text = str_replace("\n", "", $text); + + // replace line breaks + $text = str_replace("\n", $this->config['break'], $text); + + return $this->config['begin'] . $text . $this->config['end']; + } + + /** + * Converts spaces in line of text + */ + protected function _convert_line($text, $is_flowed) + { + static $table; + + if (empty($table)) { + $table = get_html_translation_table(HTML_SPECIALCHARS); + unset($table['?']); + } + + // skip signature separator + if ($text == '-- ') { + return $text; + } + + // replace HTML special characters + $text = strtr($text, $table); + + $nbsp = $this->config['space']; + + // replace some whitespace characters + $text = str_replace(array("\r", "\t"), array('', ' '), $text); + + // replace spaces with non-breaking spaces + if ($is_flowed) { + $text = preg_replace_callback('/(^|[^ ])( +)/', function($matches) { + if (!strlen($matches[2])) { + return str_repeat($nbsp, strlen($matches[2])); + } + else { + return $matches[1] . ' ' . str_repeat($nbsp, strlen($matches[2])-1); + } + }, $text); + } + else { + $text = str_replace(' ', $nbsp, $text); + } + + return $text; + } +} -- cgit v1.2.3 From d9d276ea700186a11c6525fec375160a76229b76 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Mon, 12 May 2014 10:20:42 +0200 Subject: Improve _convert_line() performance --- program/lib/Roundcube/rcube_text2html.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 5da771ac8..6032726b0 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -259,14 +259,20 @@ class rcube_text2html // replace spaces with non-breaking spaces if ($is_flowed) { - $text = preg_replace_callback('/(^|[^ ])( +)/', function($matches) { - if (!strlen($matches[2])) { - return str_repeat($nbsp, strlen($matches[2])); - } - else { - return $matches[1] . ' ' . str_repeat($nbsp, strlen($matches[2])-1); - } - }, $text); + $pos = 0; + $diff = 0; + $len = strlen($nbsp); + $copy = $text; + + while (($pos = strpos($text, ' ', $pos)) !== false) { + if ($pos == 0 || $text[$pos-1] == ' ') { + $copy = substr_replace($copy, $nbsp, $pos + $diff, 1); + $diff += $len - 1; + } + $pos++; + } + + $text = $copy; } else { $text = str_replace(' ', $nbsp, $text); -- cgit v1.2.3 From d41367492dbbd7fdb074cd1374044917fc2e82df Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Mon, 12 May 2014 11:19:27 +0200 Subject: Fix flowed lines recognition --- program/lib/Roundcube/rcube_text2html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 6032726b0..9ca3f960f 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -163,7 +163,7 @@ class rcube_text2html // find/mark quoted lines... for ($n=0, $cnt=count($text); $n < $cnt; $n++) { $flowed = false; - if ($this->config['flowed'] && ord($text[0]) == $flowed_char) { + if ($this->config['flowed'] && ord($text[$n][0]) == $flowed_char) { $flowed = true; $text[$n] = substr($text[$n], 1); } -- cgit v1.2.3 From e0881f985d558a1084ccc1c50702c4867b94f4c1 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Mon, 12 May 2014 11:22:06 +0200 Subject: Disable wrapping non-flowed lines on dash character --- program/lib/Roundcube/rcube_text2html.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 9ca3f960f..e0502c42b 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -275,7 +275,8 @@ class rcube_text2html $text = $copy; } else { - $text = str_replace(' ', $nbsp, $text); + // make the whole line non-breakable + $text = str_replace(array(' ', '-'), array($nbsp, '-⁠'), $text); } return $text; -- cgit v1.2.3 From e2b4760e846e8b74f2f674e1fa25d82ba21e7a2e Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Mon, 12 May 2014 19:08:25 +0200 Subject: Fix invalid closing tag --- program/lib/Roundcube/rcube_text2html.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 5da771ac8..cb4390e5d 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -54,7 +54,7 @@ class rcube_text2html 'break' => "
\n", // prefix and suffix (wrapper element) 'begin' => '
', - 'end' => '', + 'end' => '
', // enables links replacement 'links' => true, ); -- cgit v1.2.3 From f0992426d9c5af5046c76a2da86183d0c3a40084 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Tue, 13 May 2014 19:40:00 +0200 Subject: Bring back the old behaviour where text messages without format=flowed are auto-wrapped. Make it the default in text2html class. --- program/lib/Roundcube/rcube_text2html.php | 2 +- program/steps/mail/func.inc | 3 ++- tests/Framework/Text2Html.php | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 60016fffd..8bcda301c 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -49,7 +49,7 @@ class rcube_text2html // enables format=flowed parser 'flowed' => false, // enables wrapping for non-flowed text - 'wrap' => false, + 'wrap' => true, // line-break tag 'break' => "
\n", // prefix and suffix (wrapper element) diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc index ac0d7fc5f..a1d1a4163 100644 --- a/program/steps/mail/func.inc +++ b/program/steps/mail/func.inc @@ -874,7 +874,8 @@ function rcmail_print_body($part, $p = array()) */ function rcmail_plain_body($body, $flowed = false) { - $text2html = new rcube_text2html($body, false, array('flowed' => $flowed)); + $options = array('flowed' => $flowed, 'wrap' => !$flowed); + $text2html = new rcube_text2html($body, false, $options); $body = $text2html->get_html(); return $body; diff --git a/tests/Framework/Text2Html.php b/tests/Framework/Text2Html.php index 91dabf2b7..af2604d8e 100644 --- a/tests/Framework/Text2Html.php +++ b/tests/Framework/Text2Html.php @@ -19,6 +19,7 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase 'break' => '
', 'links' => false, 'flowed' => false, + 'wrap' => false, 'space' => '_', // replace UTF-8 non-breaking space for simpler testing ); -- cgit v1.2.3 From c0a5aa5f5ff38ac7b8a650b07c134b7b86deb27f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Thu, 15 May 2014 10:41:35 +0200 Subject: Improved handling of new-lines in quoted paragraphs on text2html conversion --- program/lib/Roundcube/rcube_text2html.php | 65 +++++++++++++++++++++---------- skins/classic/mail.css | 2 +- skins/larry/mail.css | 2 +- tests/Framework/Text2Html.php | 11 ++++++ 4 files changed, 58 insertions(+), 22 deletions(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 8bcda301c..363f1b21f 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -158,10 +158,10 @@ class rcube_text2html // split body into single lines $text = preg_split('/\r?\n/', $text); $quote_level = 0; - $last = -1; + $last = null; - // find/mark quoted lines... - for ($n=0, $cnt=count($text); $n < $cnt; $n++) { + // wrap quoted lines with
+ for ($n = 0, $cnt = count($text); $n < $cnt; $n++) { $flowed = false; if ($this->config['flowed'] && ord($text[$n][0]) == $flowed_char) { $flowed = true; @@ -172,43 +172,71 @@ class rcube_text2html $q = substr_count($regs[0], '>'); $text[$n] = substr($text[$n], strlen($regs[0])); $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); + $_length = strlen(str_replace(' ', '', $text[$n])); if ($q > $quote_level) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('
', $q - $quote_level))) . $text[$n]; - $last = $n; + if ($last !== null) { + $text[$last] .= (!$length ? "\n" : '') + . $replacer->get_replacement($replacer->add( + str_repeat('
', $q - $quote_level))) + . $text[$n]; + + unset($text[$n]); + } + else { + $text[$n] = $replacer->get_replacement($replacer->add( + str_repeat('
', $q - $quote_level))) . $text[$n]; + + $last = $n; + } } else if ($q < $quote_level) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('
', $quote_level - $q))) . $text[$n]; + $text[$last] .= (!$length ? "\n" : '') + . $replacer->get_replacement($replacer->add( + str_repeat('
', $quote_level - $q))) + . $text[$n]; + + unset($text[$n]); + } + else { $last = $n; } } else { $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); - $q = 0; + $q = 0; + $_length = strlen(str_replace(' ', '', $text[$n])); if ($quote_level > 0) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('
', $quote_level))) . $text[$n]; + $text[$last] .= (!$length ? "\n" : '') + . $replacer->get_replacement($replacer->add( + str_repeat('
', $quote_level))) + . $text[$n]; + + unset($text[$n]); + } + else { + $last = $n; } } $quote_level = $q; + $length = $_length; } if ($quote_level > 0) { - $text[$n] = $replacer->get_replacement($replacer->add( - str_repeat('', $quote_level))) . $text[$n]; + $text[$last] .= $replacer->get_replacement($replacer->add( + str_repeat('', $quote_level))); } $text = join("\n", $text); // colorize signature (up to lines) - $len = strlen($text); + $len = strlen($text); + $sig_sep = "--" . $this->config['space'] . "\n"; $sig_max_lines = rcube::get_instance()->config->get('sig_max_lines', 15); - while (($sp = strrpos($text, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) { + while (($sp = strrpos($text, $sig_sep, $sp ? -$len+$sp-1 : 0)) !== false) { if ($sp == 0 || $text[$sp-1] == "\n") { // do not touch blocks with more that X lines if (substr_count($text, "\n", $sp) < $sig_max_lines) { @@ -223,9 +251,6 @@ class rcube_text2html // insert url/mailto links and citation tags $text = $replacer->resolve($text); - // replace \n before - $text = str_replace("\n", "", $text); - // replace line breaks $text = str_replace("\n", $this->config['break'], $text); @@ -246,7 +271,7 @@ class rcube_text2html // skip signature separator if ($text == '-- ') { - return $text; + return '--' . $this->config['space']; } // replace HTML special characters @@ -276,7 +301,7 @@ class rcube_text2html } else { // make the whole line non-breakable - $text = str_replace(array(' ', '-'), array($nbsp, '-⁠'), $text); + $text = str_replace(array(' ', '-', '/'), array($nbsp, '-⁠', '/⁠'), $text); } return $text; diff --git a/skins/classic/mail.css b/skins/classic/mail.css index 47faa29af..6409b6b9b 100644 --- a/skins/classic/mail.css +++ b/skins/classic/mail.css @@ -1325,7 +1325,7 @@ div.message-part blockquote border-left: 2px solid blue; border-right: 2px solid blue; background-color: #F6F6F6; - margin: 0; + margin: 2px 0; padding: 0 0.4em; overflow: hidden; text-overflow: ellipsis; diff --git a/skins/larry/mail.css b/skins/larry/mail.css index 7afb14fba..8306afd9f 100644 --- a/skins/larry/mail.css +++ b/skins/larry/mail.css @@ -1119,7 +1119,7 @@ div.message-part blockquote { border-left: 2px solid blue; border-right: 2px solid blue; background-color: #F6F6F6; - margin: 0; + margin: 2px 0; padding: 0 0.4em; overflow: hidden; text-overflow: ellipsis; diff --git a/tests/Framework/Text2Html.php b/tests/Framework/Text2Html.php index af2604d8e..8d091d5c9 100644 --- a/tests/Framework/Text2Html.php +++ b/tests/Framework/Text2Html.php @@ -41,6 +41,7 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase $data[] = array(">aaaa \n>aaaa", "
aaaa_
aaaa
", $options); $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa_
bbbb
cccc_dddd", $options); + $data[] = array("aaaa-bbbb/cccc", "aaaa-⁠bbbb/⁠cccc", $options); $options['flowed'] = true; @@ -63,6 +64,16 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase $data[] = array(">aaaa\n>aaaa", "
aaaa
aaaa
", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "
aaaa bbbb
cccc_dddd", $options); + $options['flowed'] = false; + $options['wrap'] = true; + + $data[] = array(">>aaaa bbbb\n>>\n>>>\n>cccc\n\ndddd eeee", + "
aaaa bbbb


cccc

dddd eeee", $options); + $data[] = array("\n>>aaaa\n\ndddd", + "
aaaa

dddd", $options); + $data[] = array("aaaa\n>bbbb\n>cccc\n\ndddd\n>>test", + "aaaa
bbbb
cccc

dddd
test
", $options); + return $data; } -- cgit v1.2.3 From 9cc5a522df25d7f98e578dc483e0ff067b6c1ded Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sat, 30 Aug 2014 12:33:42 +0200 Subject: Fix so rcube_text2html class does not depend on rcmail_string_replacer --- program/lib/Roundcube/rcube_text2html.php | 4 +++- program/steps/mail/func.inc | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'program/lib/Roundcube/rcube_text2html.php') diff --git a/program/lib/Roundcube/rcube_text2html.php b/program/lib/Roundcube/rcube_text2html.php index 46c2b7e9a..0afc6d110 100644 --- a/program/lib/Roundcube/rcube_text2html.php +++ b/program/lib/Roundcube/rcube_text2html.php @@ -57,6 +57,8 @@ class rcube_text2html 'end' => '
', // enables links replacement 'links' => true, + // string replacer class + 'replacer' => 'rcube_string_replacer', ); @@ -141,7 +143,7 @@ class rcube_text2html { // make links and email-addresses clickable $attribs = array('link_attribs' => array('rel' => 'noreferrer', 'target' => '_blank')); - $replacer = new rcmail_string_replacer($attribs); + $replacer = new $this->config['replacer']($attribs); if ($this->config['flowed']) { $flowed_char = 0x01; diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc index 41fe28e7a..a7c483bba 100644 --- a/program/steps/mail/func.inc +++ b/program/steps/mail/func.inc @@ -920,7 +920,7 @@ function rcmail_print_body($part, $p = array()) */ function rcmail_plain_body($body, $flowed = false) { - $options = array('flowed' => $flowed, 'wrap' => !$flowed); + $options = array('flowed' => $flowed, 'wrap' => !$flowed, 'replacer' => 'rcmail_string_replacer'); $text2html = new rcube_text2html($body, false, $options); $body = $text2html->get_html(); -- cgit v1.2.3