3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content\Text;
26 use Friendica\Model\Photo;
27 use Friendica\Model\Post;
30 * Tumblr Neue Post Format
31 * @see https://www.tumblr.com/docs/npf
35 static public function fromBBCode(string $bbcode, int $uri_id): array
37 $bbcode = self::prepareBody($bbcode);
39 $html = BBCode::convert($bbcode, false, BBCode::CONNECTORS);
44 $doc = new DOMDocument();
45 $doc->formatOutput = true;
46 if (!@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
50 $element = $doc->getElementsByTagName('body')->item(0);
51 echo $element->ownerDocument->saveHTML($element) . "\n";
57 self::routeChildren($element, $uri_id, true, [], $npf, $text, $formatting);
59 return self::addLinkBlockForUriId($uri_id, 0, $npf);
62 static private function prepareBody(string $body): string
64 $shared = BBCode::fetchShareAttributes($body);
65 if (!empty($shared)) {
66 $body = $shared['shared'];
69 $body = BBCode::removeAttachment($body);
71 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
73 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
74 foreach ($pictures as $picture) {
75 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
76 $body = str_replace($picture[0], "\n\n[img=" . str_replace('-1.', '-0.', $picture[2]) . "]" . $picture[3] . "[/img]\n\n", $body);
81 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", "\n\n[img=$1]$2[/img]\n\n", $body);
83 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
84 foreach ($pictures as $picture) {
85 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
86 $body = str_replace($picture[0], "\n\n[img]" . str_replace('-1.', '-0.', $picture[2]) . "[/img]\n\n", $body);
91 $body = preg_replace("/\[img\](.*?)\[\/img\]/ism", "\n\n[img]$1[/img]\n\n", $body);
92 $body = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", "\n\n[audio]$1[/audio]\n\n", $body);
93 $body = preg_replace("/\[video\](.*?)\[\/video\]/ism", "\n\n[video]$1[/video]\n\n", $body);
97 $body = str_replace(["\n\n\n"], ["\n\n"], $body);
98 } while ($oldbody != $body);
103 static private function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array &$npf, string &$text, array &$formatting)
105 if ($parse_structure && $text) {
106 self::addBlock($text, $formatting, $npf, $callstack);
109 $callstack[] = $element->nodeName;
110 $level = self::getLevelByCallstack($callstack);
112 foreach ($element->childNodes as $child) {
113 switch ($child->nodeName) {
116 self::addFormatting($child, $uri_id, 'bold', $callstack, $npf, $text, $formatting);
121 self::addFormatting($child, $uri_id, 'italic', $callstack, $npf, $text, $formatting);
125 self::addFormatting($child, $uri_id, 'strikethrough', $callstack, $npf, $text, $formatting);
130 self::addFormatting($child, $uri_id, '', $callstack, $npf, $text, $formatting);
141 $text .= $child->textContent;
146 // Ignore tables and spoilers
151 self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting);
153 $npf = self::addLinkBlock($child, $uri_id, $level, $npf);
158 $npf = self::addImageBlock($child, $uri_id, $level, $npf);
176 self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting);
186 if ($parse_structure && $text) {
187 self::addBlock($text, $formatting, $npf, $callstack);
191 static private function getLevelByCallstack($callstack): int
194 foreach ($callstack as $entry) {
195 if (in_array($entry, ['ol', 'ul', 'blockquote'])) {
199 return max(0, $level - 1);
202 static private function getSubTypeByCallstack($callstack): string
205 foreach ($callstack as $entry) {
208 $subtype = 'ordered-list-item';
212 $subtype = 'unordered-list-item';
216 $subtype = 'heading1';
220 $subtype = 'heading1';
224 $subtype = 'heading1';
228 $subtype = 'heading2';
232 $subtype = 'heading2';
236 $subtype = 'heading2';
242 $subtype = 'indented';
249 static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array &$npf, string &$text, array &$formatting)
251 $start = mb_strlen($text);
252 self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
257 'end' => mb_strlen($text),
263 static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array &$npf, string &$text, array &$formatting)
265 $start = mb_strlen($text);
266 self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
269 foreach ($element->attributes as $key => $attribute) {
270 $attributes[$key] = trim($attribute->value);
272 if (!empty($attributes['href'])) {
275 'end' => mb_strlen($text),
277 'url' => $attributes['href']
282 static private function addBlock(string &$text, array &$formatting, array &$npf, array $callstack)
285 'callstack' => $callstack,
290 if (!empty($formatting)) {
291 $block['formatting'] = $formatting;
294 $level = self::getLevelByCallstack($callstack);
296 $block['indent_level'] = $level;
299 $subtype = self::getSubTypeByCallstack($callstack);
301 $block['subtype'] = $subtype;
309 static private function addPoster(array $media, array $block): array
312 if (!empty($media['preview'])) {
313 $poster['url'] = $media['preview'];
315 if (!empty($media['preview-width'])) {
316 $poster['width'] = $media['preview-width'];
318 if (!empty($media['preview-height'])) {
319 $poster['height'] = $media['preview-height'];
321 if (!empty($poster)) {
322 $block['poster'] = $poster;
327 static private function addLinkBlockForUriId(int $uri_id, int $level, array $npf): array
329 foreach (Post\Media::getByURIId($uri_id, [Post\Media::HTML]) as $link) {
330 $host = parse_url($link['url'], PHP_URL_HOST);
331 if (in_array($host, ['www.youtube.com', 'youtu.be'])) {
334 'provider' => 'youtube',
335 'url' => $link['url'],
337 } elseif (in_array($host, ['vimeo.com'])) {
340 'provider' => 'vimeo',
341 'url' => $link['url'],
343 } elseif (in_array($host, ['open.spotify.com'])) {
346 'provider' => 'spotify',
347 'url' => $link['url'],
352 'url' => $link['url'],
354 if (!empty($link['name'])) {
355 $block['title'] = $link['name'];
357 if (!empty($link['description'])) {
358 $block['description'] = $link['description'];
360 if (!empty($link['author-name'])) {
361 $block['author'] = $link['author-name'];
363 if (!empty($link['publisher-name'])) {
364 $block['site_name'] = $link['publisher-name'];
369 $block['indent_level'] = $level;
372 $npf[] = self::addPoster($link, $block);
377 static private function addImageBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
380 foreach ($element->attributes as $key => $attribute) {
381 $attributes[$key] = trim($attribute->value);
383 if (empty($attributes['src'])) {
392 if (!empty($attributes['alt'])) {
393 $block['alt_text'] = $attributes['alt'];
396 if (!empty($attributes['title']) && (($attributes['alt'] ?? '') != $attributes['title'])) {
397 $block['caption'] = $attributes['title'];
400 $rid = Photo::ridFromURI($attributes['src']);
402 $photos = Photo::selectToArray([], ['resource-id' => $rid]);
403 foreach ($photos as $photo) {
404 $block['media'][] = [
405 'type' => $photo['type'],
406 'url' => str_replace('-0.', '-' . $photo['scale'] . '.', $attributes['src']),
407 'width' => $photo['width'],
408 'height' => $photo['height'],
411 if (empty($attributes['alt']) && !empty($photos[0]['desc'])) {
412 $block['alt_text'] = $photos[0]['desc'];
414 } elseif ($media = Post\Media::getByURL($uri_id, $attributes['src'], [Post\Media::IMAGE])) {
415 $block['media'][] = [
416 'type' => $media['mimetype'],
417 'url' => $media['url'],
418 'width' => $media['width'],
419 'height' => $media['height'],
421 if (empty($attributes['alt']) && !empty($media['description'])) {
422 $block['alt_text'] = $media['description'];
425 $block['media'][] = ['url' => $attributes['src']];
429 $block['indent_level'] = $level;
437 static private function addLinkBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
440 foreach ($element->attributes as $key => $attribute) {
441 $attributes[$key] = trim($attribute->value);
443 if (empty($attributes['href'])) {
447 $media = Post\Media::getByURL($uri_id, $attributes['href'], [Post\Media::AUDIO, Post\Media::VIDEO]);
448 if (!empty($media)) {
449 switch ($media['type']) {
450 case Post\Media::AUDIO:
454 'type' => $media['mimetype'],
455 'url' => $media['url'],
459 if (!empty($media['name'])) {
460 $block['title'] = $media['name'];
461 } elseif (!empty($media['description'])) {
462 $block['title'] = $media['description'];
465 $block = self::addPoster($media, $block);
468 case Post\Media::VIDEO:
472 'type' => $media['mimetype'],
473 'url' => $media['url'],
477 $block = self::addPoster($media, $block);
483 'text' => $element->textContent,
486 'end' => strlen($element->textContent),
488 'url' => $attributes['href']
494 $block['indent_level'] = $level;