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

@ -785,6 +785,7 @@
api_register_func('api/statuses/show','api_statuses_show', true); api_register_func('api/statuses/show','api_statuses_show', true);
//api_register_func('api/statuses/mentions','api_statuses_mentions', true); //api_register_func('api/statuses/mentions','api_statuses_mentions', true);
//api_register_func('api/statuses/replies','api_statuses_mentions', true);
function api_statuses_user_timeline(&$a, $type){ function api_statuses_user_timeline(&$a, $type){
@ -932,7 +933,7 @@
} }
$status = array( $status = array(
'text' => html2plain(bbcode($item['body']), 0), 'text' => trim($item['title']." \n".html2plain(bbcode($item['body']), 0)),
'truncated' => False, 'truncated' => False,
'created_at'=> api_date($item['created']), 'created_at'=> api_date($item['created']),
'in_reply_to_status_id' => $in_reply_to_status_id, 'in_reply_to_status_id' => $in_reply_to_status_id,
@ -1306,4 +1307,36 @@
api_register_func('api/oauth/request_token', 'api_oauth_request_token', false); api_register_func('api/oauth/request_token', 'api_oauth_request_token', false);
api_register_func('api/oauth/access_token', 'api_oauth_access_token', false); api_register_func('api/oauth/access_token', 'api_oauth_access_token', false);
/*
Not implemented by now:
statuses/public_timeline
statuses/mentions
statuses/replies
statuses/retweets_of_me
statuses/destroy
statuses/retweet
friendships/create
friendships/destroy
friendships/exists
friendships/show
account/update_location
account/update_profile_background_image
account/update_profile_image
favorites
favorites/create
favorites/destroy
blocks/create
blocks/destroy
oauth/authorize
Not implemented in status.net:
statuses/retweeted_to_me
statuses/retweeted_by_me
direct_messages/destroy
account/end_session
account/update_delivery_device
notifications/follow
notifications/leave
blocks/exists
blocks/blocking
*/

View file

@ -5,7 +5,9 @@ function breaklines($line, $level, $wraplength = 75)
{ {
if ($wraplength == 0) if ($wraplength == 0)
return($line); $wraplength = 2000000;
// return($line);
$wraplen = $wraplength-$level; $wraplen = $wraplength-$level;
@ -74,7 +76,26 @@ function quotelevel($message, $wraplength = 75)
return(implode($newlines, "\n")); 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; 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 = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
$message = preg_replace('= [\s]*=i', " ", $message); $message = preg_replace('= [\s]*=i', " ", $message);
// nach <a href="...">...</a> suchen, die ... miteinander vergleichen und bei Gleichheit durch ein einzelnes ... ersetzen. // Collecting all links
$pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is'; $urls = collecturls($message);
preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
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); @$doc->loadHTML($message);
node2bbcode($doc, 'html', array(), '', ''); node2bbcode($doc, 'html', array(), '', '');
@ -124,7 +138,10 @@ function html2plain($html, $wraplength = 75)
node2bbcode($doc, 'u', array(), '_', '_'); 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", ''); 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 // 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'=>'/(.+)/'), ' $1 ', '', true);
node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
//node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', ''); //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
//node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', ''); //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
//node2bbcode($doc, 'img', array(), '', ''); //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 = $doc->saveHTML();
$message = str_replace("[img]", "", $message); if (!$compact) {
$message = str_replace("[/img]", "", $message); $message = str_replace("[img]", "", $message);
$message = str_replace("[/img]", "", $message);
}
// was ersetze ich da? // was ersetze ich da?
// Irgendein stoerrisches UTF-Zeug // Irgendein stoerrisches UTF-Zeug
@ -175,6 +200,14 @@ function html2plain($html, $wraplength = 75)
$message = html_entity_decode($message, ENT_QUOTES, 'UTF-8'); $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 { do {
$oldmessage = $message; $oldmessage = $message;
$message = str_replace("\n\n\n", "\n\n", $message); $message = str_replace("\n\n\n", "\n\n", $message);