3 require_once("include/oembed.php");
4 require_once("include/event.php");
5 require_once("library/markdown.php");
6 require_once("include/html2bbcode.php");
7 require_once("include/bbcode.php");
8 require_once("library/html-to-markdown/HTML_To_Markdown.php");
11 // we don't want to support a bbcode specific markdown interpreter
12 // and the markdown library we have is pretty good, but provides HTML output.
13 // So we'll use that to convert to HTML, then convert the HTML back to bbcode,
14 // and then clean up a few Diaspora specific constructs.
16 function diaspora2bb($s) {
18 $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
21 //$s = str_replace("\r","",$s);
23 // The parser has problems with unbalanced HTML elements
24 $doc = new DOMDocument();
25 $doc->preserveWhiteSpace = false;
26 $s = mb_convert_encoding($s, 'HTML-ENTITIES', "UTF-8");
27 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
28 $encoding = '<?xml encoding="UTF-8">';
29 @$doc->loadHTML($encoding.$doctype."<html><body>".$s."</body></html>");
30 $doc->encoding = 'UTF-8';
31 $s = $doc->saveHTML();
32 $s = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $s);
35 // The parser has problems with unbalanced html elements
36 $s = str_replace(array("<br/>", "</p>", "<p>", '<p dir="ltr">'),array("<br />", "<br />", "<br />", "<br />"),$s);
38 // Escaping the hash tags
39 $s = preg_replace('/\#([^\s\#])/','#$1',$s);
43 $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
45 $s = str_replace('#','#',$s);
49 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
50 $s = str_replace('♲',html_entity_decode('♲',ENT_QUOTES,'UTF-8'),$s);
52 // Convert everything that looks like a link to a link
53 $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
55 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
56 $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
57 $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
58 $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
59 $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
60 // remove duplicate adjacent code tags
61 $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
63 // Don't show link to full picture (until it is fixed)
64 $s = scale_external_images($s, false);
69 function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
73 $OriginalText = $Text;
75 // Since Diaspora is creating a summary for links, this function removes them before posting
77 $Text = bb_remove_share_information($Text);
80 * Transform #tags, strip off the [url] and replace spaces with underscore
82 $URLSearchString = "^\[\]";
83 $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
84 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
88 // Converting images with size parameters to simple images. Markdown doesn't know it.
89 $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
91 // Convert it to HTML - don't try oembed
93 $Text = bbcode($Text, $preserve_nl, false, 3);
95 // Add all tags that maybe were removed
96 if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",$OriginalText, $tags)) {
98 foreach($tags[2] as $tag)
99 if (!strpos($Text, "#".$tag))
100 $tagline .= "#".$tag." ";
102 $Text = $Text."<br />".$tagline;
106 $Text = bbcode($Text, $preserve_nl, false, 4);
108 // If a link is followed by a quote then there should be a newline before it
109 // Maybe we should make this newline at every time before a quote.
110 $Text = str_replace(array("</a><blockquote>"), array("</a><br><blockquote>"), $Text);
112 $stamp1 = microtime(true);
114 // Now convert HTML to Markdown
115 $Text = new HTML_To_Markdown($Text);
117 $a->save_timestamp($stamp1, "parser");
119 // Libertree has a problem with escaped hashtags - Diaspora doesn't seem to.
121 $Text = str_replace(array('\#'), array('#'), $Text);
123 // Remove any leading or trailing whitespace, as this will mess up
124 // the Diaspora signature verification and cause the item to disappear
127 call_hooks('bb2diaspora',$Text);
132 function unescape_underscores_in_links($m) {
133 $y = str_replace('\\_','_', $m[2]);
134 return('[' . $m[1] . '](' . $y . ')');
137 function format_event_diaspora($ev) {
141 if(! ((is_array($ev)) && count($ev)))
144 $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
146 $o = 'Friendica event notification:' . "\n";
148 $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) . '**' . "\n";
150 $o .= t('Starts:') . ' ' . '['
151 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC',
152 $ev['start'] , $bd_format ))
153 : day_translate(datetime_convert('UTC', 'UTC',
154 $ev['start'] , $bd_format)))
155 . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
157 if(! $ev['nofinish'])
158 $o .= t('Finishes:') . ' ' . '['
159 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC',
160 $ev['finish'] , $bd_format ))
161 : day_translate(datetime_convert('UTC', 'UTC',
162 $ev['finish'] , $bd_format )))
163 . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
165 if(strlen($ev['location']))
166 $o .= t('Location:') . bb2diaspora($ev['location'])