New parameter in html2plain for plain text exports. More compatibility in API

This commit is contained in:
Michael Vogel 2012-03-21 10:08:40 +01:00
parent 3393d80ac1
commit 792edfe9c8
2 changed files with 82 additions and 16 deletions

View file

@ -5,7 +5,9 @@ function breaklines($line, $level, $wraplength = 75)
{
if ($wraplength == 0)
return($line);
$wraplength = 2000000;
// return($line);
$wraplen = $wraplength-$level;
@ -74,7 +76,26 @@ function quotelevel($message, $wraplength = 75)
return(implode($newlines, "\n"));
}
function html2plain($html, $wraplength = 75)
function collecturls($message) {
$pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
$urls = array();
foreach ($result as $treffer) {
// A list of some links that should be ignored
$list = array("/user/", "/tag/", "/profile/", "/search?search=", "mailto:", "/u/", "/node/",
"//facebook.com/profile.php?id=", "//plus.google.com/");
foreach ($list as $listitem)
if (strpos($treffer[1], $listitem) !== false)
$ignore = true;
if (!$ignore)
$urls[$treffer[1]] = $treffer[1];
}
return($urls);
}
function html2plain($html, $wraplength = 75, $compact = false)
{
global $lang;
@ -97,16 +118,9 @@ function html2plain($html, $wraplength = 75)
$message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
$message = preg_replace('= [\s]*=i', " ", $message);
// nach <a href="...">...</a> suchen, die ... miteinander vergleichen und bei Gleichheit durch ein einzelnes ... ersetzen.
$pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
// Collecting all links
$urls = collecturls($message);
foreach ($result as $treffer) {
if ($treffer[1] == $treffer[2]) {
$search = '<a href="'.$treffer[1].'" target="external-link">'.$treffer[1].'</a>';
$message = str_replace($search, $treffer[1], $message);
}
}
@$doc->loadHTML($message);
node2bbcode($doc, 'html', array(), '', '');
@ -124,7 +138,10 @@ function html2plain($html, $wraplength = 75)
node2bbcode($doc, 'u', array(), '_', '_');
*/
node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
if ($compact)
node2bbcode($doc, 'blockquote', array(), "»", "«");
else
node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
node2bbcode($doc, 'br', array(), "\n", '');
@ -151,15 +168,23 @@ function html2plain($html, $wraplength = 75)
// Problem: there is no reliable way to detect if it is a link to a tag or profile
//node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', '', true);
node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
//node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
//node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
//node2bbcode($doc, 'img', array(), '', '');
node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
if (!$compact)
node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
else
node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '', '');
node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), ' $1 ', '', true);
$message = $doc->saveHTML();
$message = str_replace("[img]", "", $message);
$message = str_replace("[/img]", "", $message);
if (!$compact) {
$message = str_replace("[img]", "", $message);
$message = str_replace("[/img]", "", $message);
}
// was ersetze ich da?
// Irgendein stoerrisches UTF-Zeug
@ -175,6 +200,14 @@ function html2plain($html, $wraplength = 75)
$message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
if (!$compact) {
$counter = 1;
foreach ($urls as $id=>$url)
if (strpos($message, $url) == false)
$message .= "\n".$url." ";
//$message .= "\n[".($counter++)."] ".$url;
}
do {
$oldmessage = $message;
$message = str_replace("\n\n\n", "\n\n", $message);