]> git.mxchange.org Git - friendica.git/blob - include/bb2diaspora.php
restore jotplugins
[friendica.git] / include / bb2diaspora.php
1 <?php
2
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");
9
10
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.
15
16 function diaspora2bb($s) {
17
18         $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
19
20         // Remove CR to avoid problems with following code
21         $s = str_replace("\r","",$s);
22
23         $s = str_replace("\n"," \n",$s);
24
25         // The parser cannot handle paragraphs correctly
26         $s = str_replace(array("</p>", "<p>", '<p dir="ltr">'),array("<br>", "<br>", "<br>"),$s);
27
28         // Escaping the hash tags
29         $s = preg_replace('/\#([^\s\#])/','&#35;$1',$s);
30
31         $s = Markdown($s);
32
33         $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
34
35         $s = str_replace('&#35;','#',$s);
36
37         $s = html2bbcode($s);
38
39         // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
40         $s = str_replace('&#x2672;',html_entity_decode('&#x2672;',ENT_QUOTES,'UTF-8'),$s);
41
42         // Convert everything that looks like a link to a link
43         $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
44
45         //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
46         $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
47         $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
48         $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
49         $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
50         // remove duplicate adjacent code tags
51         $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
52
53         // Don't show link to full picture (until it is fixed)
54         $s = scale_external_images($s, false);
55
56         return $s;
57 }
58
59 function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
60
61         $a = get_app();
62
63         $OriginalText = $Text;
64
65         // Since Diaspora is creating a summary for links, this function removes them before posting
66         if ($fordiaspora)
67                 $Text = bb_remove_share_information($Text);
68
69         /**
70          * Transform #tags, strip off the [url] and replace spaces with underscore
71          */
72         $URLSearchString = "^\[\]";
73         $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
74                 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
75         ), $Text);
76
77
78         // Converting images with size parameters to simple images. Markdown doesn't know it.
79         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
80
81         // Convert it to HTML - don't try oembed
82         if ($fordiaspora) {
83                 $Text = bbcode($Text, $preserve_nl, false, 3);
84
85                 // Add all tags that maybe were removed
86                 if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",$OriginalText, $tags)) {
87                         $tagline = "";
88                         foreach($tags[2] as $tag)
89                                 if (!strpos($Text, "#".$tag))
90                                         $tagline .= "#".$tag." ";
91
92                         $Text = $Text."<br />".$tagline;
93                 }
94
95         } else
96                 $Text = bbcode($Text, $preserve_nl, false, 4);
97
98         // If a link is followed by a quote then there should be a newline before it
99         // Maybe we should make this newline at every time before a quote.
100         $Text = str_replace(array("</a><blockquote>"), array("</a><br><blockquote>"), $Text);
101
102         $stamp1 = microtime(true);
103
104         // Now convert HTML to Markdown
105         $Text = new HTML_To_Markdown($Text);
106
107         $a->save_timestamp($stamp1, "parser");
108
109         // Libertree has a problem with escaped hashtags.
110         $Text = str_replace(array('\#'), array('#'), $Text);
111
112         // Remove any leading or trailing whitespace, as this will mess up
113         // the Diaspora signature verification and cause the item to disappear
114         $Text = trim($Text);
115
116         call_hooks('bb2diaspora',$Text);
117
118         return $Text;
119 }
120
121 function unescape_underscores_in_links($m) {
122         $y = str_replace('\\_','_', $m[2]);
123         return('[' . $m[1] . '](' . $y . ')');
124 }
125
126 function format_event_diaspora($ev) {
127
128         $a = get_app();
129
130         if(! ((is_array($ev)) && count($ev)))
131                 return '';
132
133         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
134
135         $o = 'Friendica event notification:' . "\n";
136
137         $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) .  '**' . "\n";
138
139         $o .= t('Starts:') . ' ' . '['
140                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
141                         $ev['start'] , $bd_format ))
142                         :  day_translate(datetime_convert('UTC', 'UTC', 
143                         $ev['start'] , $bd_format)))
144                 .  '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
145
146         if(! $ev['nofinish'])
147                 $o .= t('Finishes:') . ' ' . '[' 
148                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
149                                 $ev['finish'] , $bd_format ))
150                                 :  day_translate(datetime_convert('UTC', 'UTC', 
151                                 $ev['finish'] , $bd_format )))
152                         . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
153
154         if(strlen($ev['location']))
155                 $o .= t('Location:') . bb2diaspora($ev['location']) 
156                         . "\n";
157
158         $o .= "\n";
159         return $o;
160 }