Improved handling of new-lines in quoted paragraphs on text2html conversion

pull/191/head
Aleksander Machniak 11 years ago
parent f0992426d9
commit c0a5aa5f5f

@ -158,10 +158,10 @@ class rcube_text2html
// split body into single lines // split body into single lines
$text = preg_split('/\r?\n/', $text); $text = preg_split('/\r?\n/', $text);
$quote_level = 0; $quote_level = 0;
$last = -1; $last = null;
// find/mark quoted lines... // wrap quoted lines with <blockquote>
for ($n=0, $cnt=count($text); $n < $cnt; $n++) { for ($n = 0, $cnt = count($text); $n < $cnt; $n++) {
$flowed = false; $flowed = false;
if ($this->config['flowed'] && ord($text[$n][0]) == $flowed_char) { if ($this->config['flowed'] && ord($text[$n][0]) == $flowed_char) {
$flowed = true; $flowed = true;
@ -172,43 +172,71 @@ class rcube_text2html
$q = substr_count($regs[0], '>'); $q = substr_count($regs[0], '>');
$text[$n] = substr($text[$n], strlen($regs[0])); $text[$n] = substr($text[$n], strlen($regs[0]));
$text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); $text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']);
$_length = strlen(str_replace(' ', '', $text[$n]));
if ($q > $quote_level) { if ($q > $quote_level) {
if ($last !== null) {
$text[$last] .= (!$length ? "\n" : '')
. $replacer->get_replacement($replacer->add(
str_repeat('<blockquote>', $q - $quote_level)))
. $text[$n];
unset($text[$n]);
}
else {
$text[$n] = $replacer->get_replacement($replacer->add( $text[$n] = $replacer->get_replacement($replacer->add(
str_repeat('<blockquote>', $q - $quote_level))) . $text[$n]; str_repeat('<blockquote>', $q - $quote_level))) . $text[$n];
$last = $n; $last = $n;
} }
}
else if ($q < $quote_level) { else if ($q < $quote_level) {
$text[$n] = $replacer->get_replacement($replacer->add( $text[$last] .= (!$length ? "\n" : '')
str_repeat('</blockquote>', $quote_level - $q))) . $text[$n]; . $replacer->get_replacement($replacer->add(
str_repeat('</blockquote>', $quote_level - $q)))
. $text[$n];
unset($text[$n]);
}
else {
$last = $n; $last = $n;
} }
} }
else { else {
$text[$n] = $this->_convert_line($text[$n], $flowed || $this->config['wrap']); $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) { if ($quote_level > 0) {
$text[$n] = $replacer->get_replacement($replacer->add( $text[$last] .= (!$length ? "\n" : '')
str_repeat('</blockquote>', $quote_level))) . $text[$n]; . $replacer->get_replacement($replacer->add(
str_repeat('</blockquote>', $quote_level)))
. $text[$n];
unset($text[$n]);
}
else {
$last = $n;
} }
} }
$quote_level = $q; $quote_level = $q;
$length = $_length;
} }
if ($quote_level > 0) { if ($quote_level > 0) {
$text[$n] = $replacer->get_replacement($replacer->add( $text[$last] .= $replacer->get_replacement($replacer->add(
str_repeat('</blockquote>', $quote_level))) . $text[$n]; str_repeat('</blockquote>', $quote_level)));
} }
$text = join("\n", $text); $text = join("\n", $text);
// colorize signature (up to <sig_max_lines> lines) // colorize signature (up to <sig_max_lines> 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); $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") { if ($sp == 0 || $text[$sp-1] == "\n") {
// do not touch blocks with more that X lines // do not touch blocks with more that X lines
if (substr_count($text, "\n", $sp) < $sig_max_lines) { if (substr_count($text, "\n", $sp) < $sig_max_lines) {
@ -223,9 +251,6 @@ class rcube_text2html
// insert url/mailto links and citation tags // insert url/mailto links and citation tags
$text = $replacer->resolve($text); $text = $replacer->resolve($text);
// replace \n before </blockquote>
$text = str_replace("\n</blockquote>", "</blockquote>", $text);
// replace line breaks // replace line breaks
$text = str_replace("\n", $this->config['break'], $text); $text = str_replace("\n", $this->config['break'], $text);
@ -246,7 +271,7 @@ class rcube_text2html
// skip signature separator // skip signature separator
if ($text == '-- ') { if ($text == '-- ') {
return $text; return '--' . $this->config['space'];
} }
// replace HTML special characters // replace HTML special characters
@ -276,7 +301,7 @@ class rcube_text2html
} }
else { else {
// make the whole line non-breakable // make the whole line non-breakable
$text = str_replace(array(' ', '-'), array($nbsp, '-&#8288;'), $text); $text = str_replace(array(' ', '-', '/'), array($nbsp, '-&#8288;', '/&#8288;'), $text);
} }
return $text; return $text;

@ -1325,7 +1325,7 @@ div.message-part blockquote
border-left: 2px solid blue; border-left: 2px solid blue;
border-right: 2px solid blue; border-right: 2px solid blue;
background-color: #F6F6F6; background-color: #F6F6F6;
margin: 0; margin: 2px 0;
padding: 0 0.4em; padding: 0 0.4em;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;

@ -1119,7 +1119,7 @@ div.message-part blockquote {
border-left: 2px solid blue; border-left: 2px solid blue;
border-right: 2px solid blue; border-right: 2px solid blue;
background-color: #F6F6F6; background-color: #F6F6F6;
margin: 0; margin: 2px 0;
padding: 0 0.4em; padding: 0 0.4em;
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;

@ -41,6 +41,7 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase
$data[] = array(">aaaa \n>aaaa", "<blockquote>aaaa_<br>aaaa</blockquote>", $options); $data[] = array(">aaaa \n>aaaa", "<blockquote>aaaa_<br>aaaa</blockquote>", $options);
$data[] = array(">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options); $data[] = array(">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options);
$data[] = array(">aaaa \n>bbbb\ncccc dddd", "<blockquote>aaaa_<br>bbbb</blockquote>cccc_dddd", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "<blockquote>aaaa_<br>bbbb</blockquote>cccc_dddd", $options);
$data[] = array("aaaa-bbbb/cccc", "aaaa-&#8288;bbbb/&#8288;cccc", $options);
$options['flowed'] = true; $options['flowed'] = true;
@ -63,6 +64,16 @@ class Framework_Text2Html extends PHPUnit_Framework_TestCase
$data[] = array(">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options); $data[] = array(">aaaa\n>aaaa", "<blockquote>aaaa<br>aaaa</blockquote>", $options);
$data[] = array(">aaaa \n>bbbb\ncccc dddd", "<blockquote>aaaa bbbb</blockquote>cccc_dddd", $options); $data[] = array(">aaaa \n>bbbb\ncccc dddd", "<blockquote>aaaa bbbb</blockquote>cccc_dddd", $options);
$options['flowed'] = false;
$options['wrap'] = true;
$data[] = array(">>aaaa bbbb\n>>\n>>>\n>cccc\n\ndddd eeee",
"<blockquote><blockquote>aaaa bbbb<br><br><blockquote><br></blockquote></blockquote>cccc</blockquote><br>dddd eeee", $options);
$data[] = array("\n>>aaaa\n\ndddd",
"<br><blockquote><blockquote>aaaa</blockquote></blockquote><br>dddd", $options);
$data[] = array("aaaa\n>bbbb\n>cccc\n\ndddd\n>>test",
"aaaa<blockquote>bbbb<br>cccc</blockquote><br>dddd<blockquote><blockquote>test</blockquote></blockquote>", $options);
return $data; return $data;
} }

Loading…
Cancel
Save