Plugin API: Add html2text hook (backport from master)

pull/5330/head
Aleksander Machniak 8 years ago
parent 848410042c
commit ead0846934

@ -1,6 +1,7 @@
CHANGELOG Roundcube Webmail
===========================
- Plugin API: Add html2text hook
- Fix missing emoticons on html-to-text conversion
- Fix random "access to this resource is secured against CSRF" message at logout (#4956)
- Fix missing language name in "Add to Dictionary" request in HTML mode (#4951)

@ -2322,6 +2322,39 @@ class rcmail extends rcube
return file_get_contents($name, false);
}
/**
* Converts HTML content into plain text
*
* @param string $html HTML content
* @param array $options Conversion parameters (width, links, charset)
*
* @return string Plain text
*/
public function html2text($html, $options = array())
{
$default_options = array(
'links' => true,
'width' => 75,
'body' => $html,
'charset' => RCUBE_CHARSET,
);
$options = array_merge($default_options, (array) $options);
// Plugins may want to modify HTML in another/additional way
$options = $this->plugins->exec_hook('html2text', $options);
// Convert to text
if (!$options['abort']) {
$converter = new rcube_html2text($options['body'],
false, $options['links'], $options['width'], $options['charset']);
$options['body'] = rtrim($converter->get_text());
}
return $options['body'];
}
/************************************************************************
********* Deprecated methods (to be removed) *********

@ -643,8 +643,8 @@ function rcmail_compose_header_from($attrib)
$text = $html = $sql_arr['signature'];
if ($sql_arr['html_signature']) {
$h2t = new rcube_html2text($html, false, true);
$text = trim($h2t->get_text());
$text = $RCMAIL->html2text($html, array('links' => false));
$text = trim($text);
}
else {
$t2h = new rcube_text2html($text, false);
@ -858,9 +858,8 @@ function rcmail_compose_part_body($part, $isHtml = false)
if ($part->ctype_secondary == 'html') {
// use html part if it has been used for message (pre)viewing
// decrease line length for quoting
$len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
$txt = new rcube_html2text($body, false, true, $len);
$body = $txt->get_text();
$len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
$body = $RCMAIL->html2text($body, array('width' => $len));
}
else {
if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed') {
@ -1043,7 +1042,7 @@ function rcmail_create_reply_body($body, $bodyIsHtml)
$suffix = '</blockquote>';
}
else {
$suffix = '</blockquote><p></p>';
$suffix = '</blockquote><p><br/></p>';
}
}

@ -884,8 +884,7 @@ function rcmail_print_body($body, $part, $p = array())
$data['body'] = rcube_enriched::to_html($data['body']);
}
$txt = new rcube_html2text($data['body'], false, true);
$body = $txt->get_text();
$body = $RCMAIL->html2text($data['body']);
$part->ctype_secondary = 'plain';
}
// text/html

@ -359,12 +359,8 @@ if ($isHtml) {
$MAIL_MIME->setHTMLBody($plugin['body']);
// replace emoticons
$plugin['body'] = $RCMAIL->replace_emoticons($plugin['body']);
// add a plain text version of the e-mail as an alternative part.
$h2t = new rcube_html2text($plugin['body'], false, true, 0, $message_charset);
$plainTextPart = rcube_mime::wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n", false, $message_charset);
$plainTextPart = $RCMAIL->html2text($plugin['body'], array('width' => 0, 'charset' => $message_charset));
$plainTextPart = rcube_mime::wordwrap($plainTextPart, $LINE_LENGTH, "\r\n", false, $message_charset);
$plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
// make sure all line endings are CRLF (#1486712)

@ -29,12 +29,11 @@ if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) {
// Replace emoticon images with its text representation
$html = $RCMAIL->replace_emoticons($html);
$do_links = (bool) rcube_utils::get_input_value('_do_links', rcube_utils::INPUT_GET);
$width = (int) rcube_utils::get_input_value('_width', rcube_utils::INPUT_GET);
$params['links'] = (bool) rcube_utils::get_input_value('_do_links', rcube_utils::INPUT_GET);
$params['width'] = (int) rcube_utils::get_input_value('_width', rcube_utils::INPUT_GET);
// Convert to text
$converter = new rcube_html2text($html, false, $do_links, $width);
$text = $RCMAIL->html2text($html, $params);
header('Content-Type: text/plain; charset=UTF-8');
print rtrim($converter->get_text());
header('Content-Type: text/plain; charset=' . RCUBE_CHARSET);
print $text;
exit;

Loading…
Cancel
Save