]> git.mxchange.org Git - friendica.git/blob - include/bb2diaspora.php
New routines for markdown to html and html to markdown.
[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 //require_once("include/markdownify/markdownify.php");
10
11
12 // we don't want to support a bbcode specific markdown interpreter
13 // and the markdown library we have is pretty good, but provides HTML output.
14 // So we'll use that to convert to HTML, then convert the HTML back to bbcode,
15 // and then clean up a few Diaspora specific constructs.
16
17 function diaspora2bb($s) {
18
19         $s = html_entity_decode($s,ENT_COMPAT,'UTF-8');
20
21         // Simply remove cr.
22         $s = str_replace("\r","",$s);
23
24         // <br/> is invalid. Replace it with the valid expression
25         //$s = str_replace(array("<br/>", "</p>", "<p>", '<p dir="ltr">'),array("<br />", "<br />", "<br />", "<br />"),$s);
26
27         // Escaping the hash tags
28         $s = preg_replace('/\#([^\s\#])/','&#35;$1',$s);
29
30         $s = Markdown($s);
31
32         $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s);
33
34         $s = str_replace('&#35;','#',$s);
35
36         $s = html2bbcode($s);
37
38         // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
39         $s = str_replace('&#x2672;',html_entity_decode('&#x2672;',ENT_QUOTES,'UTF-8'),$s);
40
41         // Convert everything that looks like a link to a link
42         $s = preg_replace("/([^\]\=]|^)(https?\:\/\/)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3]$2$3[/url]',$s);
43
44         //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
45         $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]','url',$s);
46         $s = bb_tag_preg_replace("/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism",'[youtube]$1[/youtube]','url',$s);
47         $s = bb_tag_preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]','url',$s);
48         $s = bb_tag_preg_replace("/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism",'[vimeo]$1[/vimeo]','url',$s);
49         // remove duplicate adjacent code tags
50         $s = preg_replace("/(\[code\])+(.*?)(\[\/code\])+/ism","[code]$2[/code]", $s);
51
52         // Don't show link to full picture (until it is fixed)
53         $s = scale_external_images($s, false);
54
55         return $s;
56 }
57
58 function bb2diaspora($Text,$preserve_nl = false, $fordiaspora = true) {
59
60         $OriginalText = $Text;
61
62         // Since Diaspora is creating a summary for links, this function removes them before posting
63         if ($fordiaspora)
64                 $Text = bb_remove_share_information($Text);
65
66         /**
67          * Transform #tags, strip off the [url] and replace spaces with underscore
68          */
69         $URLSearchString = "^\[\]";
70         $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i", create_function('$match',
71                 'return \'#\'. str_replace(\' \', \'_\', $match[2]);'
72         ), $Text);
73
74
75         // Converting images with size parameters to simple images. Markdown doesn't know it.
76         $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
77
78         // Convert it to HTML - don't try oembed
79         if ($fordiaspora) {
80                 $Text = bbcode($Text, $preserve_nl, false, 3);
81
82                 // Add all tags that maybe were removed
83                 if (preg_match_all("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",$OriginalText, $tags)) {
84                         $tagline = "";
85                         foreach($tags[2] as $tag)
86                                 if (!strpos($Text, "#".$tag))
87                                         $tagline .= "#".$tag." ";
88
89                         $Text = $Text."<br />".$tagline;
90                 }
91
92         } else {
93                 $Text = bbcode($Text, $preserve_nl, false, 4);
94
95                 // Libertree doesn't convert a harizontal rule if there isn't a linefeed
96                 //$Text = str_replace(array("<hr />", "<hr>"), array("<br /><hr />", "<br><hr>"), $Text);
97         }
98
99         // Now convert HTML to Markdown
100         $Text = new HTML_To_Markdown($Text);
101
102 /*
103         //$md = new Markdownify(false, false, false);
104         //$Text = $md->parseString($Text);
105
106         // The Markdownify converter converts underscores '_' in URLs to '\_', which
107         // messes up the URL. Manually fix these
108         $count = 1;
109         $pos = bb_find_open_close($Text, '[', ']', $count);
110         while($pos !== false) {
111                 $start = substr($Text, 0, $pos['start']);
112                 $subject = substr($Text, $pos['start'], $pos['end'] - $pos['start'] + 1);
113                 $end = substr($Text, $pos['end'] + 1);
114
115                 $subject = str_replace('\_', '_', $subject);
116                 $Text = $start . $subject . $end;
117
118                 $count++;
119                 $pos = bb_find_open_close($Text, '[', ']', $count);
120         }
121
122         // If the text going into bbcode() has a plain URL in it, i.e.
123         // with no [url] tags around it, it will come out of parseString()
124         // looking like: <http://url.com>, which gets removed by strip_tags().
125         // So take off the angle brackets of any such URL
126         $Text = preg_replace("/<http(.*?)>/is", "http$1", $Text);
127
128         // Remove all unconverted tags
129         $Text = strip_tags($Text);
130 */
131
132         // Remove any leading or trailing whitespace, as this will mess up
133         // the Diaspora signature verification and cause the item to disappear
134         $Text = trim($Text);
135
136         call_hooks('bb2diaspora',$Text);
137
138         return $Text;
139 }
140
141 function unescape_underscores_in_links($m) {
142         $y = str_replace('\\_','_', $m[2]);
143         return('[' . $m[1] . '](' . $y . ')');
144 }
145
146 function format_event_diaspora($ev) {
147
148         $a = get_app();
149
150         if(! ((is_array($ev)) && count($ev)))
151                 return '';
152
153         $bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
154
155         $o = 'Friendica event notification:' . "\n";
156
157         $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) .  '**' . "\n";
158
159         $o .= t('Starts:') . ' ' . '['
160                 . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
161                         $ev['start'] , $bd_format ))
162                         :  day_translate(datetime_convert('UTC', 'UTC', 
163                         $ev['start'] , $bd_format)))
164                 .  '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['start'])) . ")\n";
165
166         if(! $ev['nofinish'])
167                 $o .= t('Finishes:') . ' ' . '[' 
168                         . (($ev['adjust']) ? day_translate(datetime_convert('UTC', 'UTC', 
169                                 $ev['finish'] , $bd_format ))
170                                 :  day_translate(datetime_convert('UTC', 'UTC', 
171                                 $ev['finish'] , $bd_format )))
172                         . '](' . $a->get_baseurl() . '/localtime/?f=&time=' . urlencode(datetime_convert('UTC','UTC',$ev['finish'])) . ")\n";
173
174         if(strlen($ev['location']))
175                 $o .= t('Location:') . bb2diaspora($ev['location']) 
176                         . "\n";
177
178         $o .= "\n";
179         return $o;
180 }