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