3 use Friendica\Content\Text\BBCode;
4 use Friendica\Content\Text\Markdown;
5 use Friendica\Core\Addon;
6 use Friendica\Core\L10n;
7 use Friendica\Core\System;
8 use Friendica\Model\Contact;
9 use Friendica\Network\Probe;
10 use Friendica\Util\DateTimeFormat;
11 use League\HTMLToMarkdown\HtmlConverter;
13 require_once 'include/event.php';
14 require_once 'include/html2bbcode.php';
16 function diaspora2bb($s) {
17 return Markdown::toBBCode($s);
21 * @brief Callback function to replace a Friendica style mention in a mention for Diaspora
23 * @param array $match Matching values for the callback
24 * @return string Replaced mention
26 function diaspora_mentions($match) {
28 $contact = Contact::getDetailsByURL($match[3]);
30 if (!x($contact, 'addr')) {
31 $contact = Probe::uri($match[3]);
34 if (!x($contact, 'addr')) {
38 $mention = '@{' . $match[2] . '; ' . $contact['addr'] . '}';
43 * @brief Converts a BBCode text into Markdown
45 * This function converts a BBCode item body to be sent to Markdown-enabled
46 * systems like Diaspora and Libertree
49 * @param bool $fordiaspora Diaspora requires more changes than Libertree
52 function bb2diaspora($Text, $fordiaspora = true) {
55 $OriginalText = $Text;
57 // Since Diaspora is creating a summary for links, this function removes them before posting
59 $Text = BBCode::removeShareInformation($Text);
63 * Transform #tags, strip off the [url] and replace spaces with underscore
65 $URLSearchString = "^\[\]";
66 $Text = preg_replace_callback("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/i",
68 return '#' . str_replace(' ', '_', $matches[2]);
72 // Converting images with size parameters to simple images. Markdown doesn't know it.
73 $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $Text);
75 // Extracting multi-line code blocks before the whitespace processing/code highlighter in BBCode::convert()
78 $Text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#is",
79 function ($matches) use (&$codeblocks) {
80 $return = $matches[0];
81 if (strpos($matches[2], "\n") !== false) {
82 $return = '#codeblock-' . count($codeblocks) . '#';
84 $prefix = '````' . $matches[1] . PHP_EOL;
85 $codeblocks[] = $prefix . trim($matches[2]) . PHP_EOL . '````';
91 // Convert it to HTML - don't try oembed
93 $Text = BBCode::convert($Text, 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 $tag = html_entity_decode($tag, ENT_QUOTES, 'UTF-8');
100 if (!strpos(html_entity_decode($Text, ENT_QUOTES, 'UTF-8'), '#' . $tag)) {
101 $tagline .= '#' . $tag . ' ';
104 $Text = $Text." ".$tagline;
107 $Text = BBCode::convert($Text, false, 4);
110 // mask some special HTML chars from conversation to markdown
111 $Text = str_replace(['<', '>', '&'], ['&_lt_;', '&_gt_;', '&_amp_;'], $Text);
113 // If a link is followed by a quote then there should be a newline before it
114 // Maybe we should make this newline at every time before a quote.
115 $Text = str_replace(["</a><blockquote>"], ["</a><br><blockquote>"], $Text);
117 $stamp1 = microtime(true);
119 // Now convert HTML to Markdown
120 $converter = new HtmlConverter();
121 $Text = $converter->convert($Text);
123 // unmask the special chars back to HTML
124 $Text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['<', '>', '&'], $Text);
126 $a->save_timestamp($stamp1, "parser");
128 // Libertree has a problem with escaped hashtags.
129 $Text = str_replace(['\#'], ['#'], $Text);
131 // Remove any leading or trailing whitespace, as this will mess up
132 // the Diaspora signature verification and cause the item to disappear
136 $URLSearchString = "^\[\]";
137 $Text = preg_replace_callback("/([@]\[(.*?)\])\(([$URLSearchString]*?)\)/ism", 'diaspora_mentions', $Text);
140 // Restore code blocks
141 $Text = preg_replace_callback('/#codeblock-([0-9]+)#/iU',
142 function ($matches) use ($codeblocks) {
144 if (isset($codeblocks[intval($matches[1])])) {
145 $return = $codeblocks[$matches[1]];
151 Addon::callHooks('bb2diaspora',$Text);
156 function unescape_underscores_in_links($m) {
157 $y = str_replace('\\_', '_', $m[2]);
158 return('[' . $m[1] . '](' . $y . ')');
161 function format_event_diaspora($ev) {
162 if (! ((is_array($ev)) && count($ev))) {
166 $bd_format = L10n::t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
168 $o = 'Friendica event notification:' . "\n";
170 $o .= '**' . (($ev['summary']) ? bb2diaspora($ev['summary']) : bb2diaspora($ev['desc'])) . '**' . "\n";
172 // @todo What. Is. Going. On. With. This. Useless. Ternary. Operator? - mrpetovan
173 $o .= L10n::t('Starts:') . ' ' . '[' . day_translate(
174 $ev['adjust'] ? DateTimeFormat::utc($ev['start'], $bd_format) : DateTimeFormat::utc($ev['start'], $bd_format)
176 . '](' . System::baseUrl() . '/localtime/?f=&time=' . urlencode(DateTimeFormat::utc($ev['start'])) . ")\n";
178 if (! $ev['nofinish']) {
179 $o .= L10n::t('Finishes:') . ' ' . '[' . day_translate(
180 $ev['adjust'] ? DateTimeFormat::utc($ev['finish'], $bd_format) : DateTimeFormat::utc($ev['finish'], $bd_format)
182 . '](' . System::baseUrl() . '/localtime/?f=&time=' . urlencode(DateTimeFormat::utc($ev['finish'])) . ")\n";
185 if (strlen($ev['location'])) {
186 $o .= L10n::t('Location:') . bb2diaspora($ev['location'])