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 $heading_subtype = [];
37 static public function fromBBCode(string $bbcode, int $uri_id): array
39 $bbcode = self::prepareBody($bbcode);
41 $html = BBCode::convert($bbcode, false, BBCode::CONNECTORS);
46 $doc = new DOMDocument();
47 $doc->formatOutput = true;
48 if (!@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
52 self::setHeadingSubStyles($doc);
54 $element = $doc->getElementsByTagName('body')->item(0);
55 // echo $element->ownerDocument->saveHTML($element) . "\n";
57 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, true, []);
59 return self::addLinkBlockForUriId($uri_id, 0, $npf);
62 static function setHeadingSubStyles($doc)
64 self::$heading_subtype = [];
65 foreach (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as $element) {
66 if ($doc->getElementsByTagName($element)->count() > 0) {
67 if (empty(self::$heading_subtype)) {
68 self::$heading_subtype[$element] = 'heading1';
70 self::$heading_subtype[$element] = 'heading2';
76 static private function prepareBody(string $body): string
78 $shared = BBCode::fetchShareAttributes($body);
79 if (!empty($shared)) {
80 $body = $shared['shared'];
83 $body = BBCode::removeAttachment($body);
85 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
87 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
88 foreach ($pictures as $picture) {
89 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
90 $body = str_replace($picture[0], "\n\n[img=" . str_replace('-1.', '-0.', $picture[2]) . "]" . $picture[3] . "[/img]\n\n", $body);
95 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", "\n\n[img=$1]$2[/img]\n\n", $body);
97 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
98 foreach ($pictures as $picture) {
99 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
100 $body = str_replace($picture[0], "\n\n[img]" . str_replace('-1.', '-0.', $picture[2]) . "[/img]\n\n", $body);
105 $body = preg_replace("/\[img\](.*?)\[\/img\]/ism", "\n\n[img]$1[/img]\n\n", $body);
106 $body = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", "\n\n[audio]$1[/audio]\n\n", $body);
107 $body = preg_replace("/\[video\](.*?)\[\/video\]/ism", "\n\n[video]$1[/video]\n\n", $body);
111 $body = str_replace(["\n\n\n"], ["\n\n"], $body);
112 } while ($oldbody != $body);
117 static private function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array $npf = [], string $text = '', array $formatting = []): array
119 if ($parse_structure && $text) {
120 list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
123 $callstack[] = $element->nodeName;
124 $level = self::getLevelByCallstack($callstack);
126 foreach ($element->childNodes as $child) {
127 switch ($child->nodeName) {
130 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'bold', $callstack, $npf, $text, $formatting);
135 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'italic', $callstack, $npf, $text, $formatting);
139 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'strikethrough', $callstack, $npf, $text, $formatting);
144 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, '', $callstack, $npf, $text, $formatting);
155 $text .= $child->textContent;
160 // Ignore tables and spoilers
165 list($npf, $text, $formatting) = self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting);
167 $npf = self::addLinkBlock($child, $uri_id, $level, $npf);
172 $npf = self::addImageBlock($child, $uri_id, $level, $npf);
176 list($npf, $text, $formatting) = self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting);
181 if ($parse_structure && $text) {
182 list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
184 return [$npf, $text, $formatting];
187 static private function getLevelByCallstack($callstack): int
190 foreach ($callstack as $entry) {
191 if (in_array($entry, ['ol', 'ul', 'blockquote'])) {
195 return max(0, $level - 1);
198 static private function getSubTypeByCallstack($callstack, string $text): string
201 foreach ($callstack as $entry) {
204 $subtype = 'ordered-list-item';
208 $subtype = 'unordered-list-item';
212 $subtype = self::$heading_subtype[$entry];
216 $subtype = self::$heading_subtype[$entry];
220 $subtype = self::$heading_subtype[$entry];
224 $subtype = self::$heading_subtype[$entry];
228 $subtype = self::$heading_subtype[$entry];
232 $subtype = self::$heading_subtype[$entry];
236 $subtype = strlen($text) < 100 ? 'quote' : 'indented';
240 $subtype = 'indented';
251 static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array $npf, string $text, array $formatting): array
253 $start = mb_strlen($text);
254 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
259 'end' => mb_strlen($text),
263 return [$npf, $text, $formatting];
266 static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array $npf, string $text, array $formatting): array
268 $start = mb_strlen($text);
269 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
272 foreach ($element->attributes as $key => $attribute) {
273 $attributes[$key] = trim($attribute->value);
275 if (!empty($attributes['href'])) {
278 'end' => mb_strlen($text),
280 'url' => $attributes['href']
283 return [$npf, $text, $formatting];
286 static private function addBlock(string $text, array $formatting, array $npf, array $callstack): array
294 // Deactivated since Tumblr has got issues with it
295 //if (!empty($formatting)) {
296 // $block['formatting'] = $formatting;
299 $level = self::getLevelByCallstack($callstack);
301 $block['indent_level'] = $level;
304 $subtype = self::getSubTypeByCallstack($callstack, $text);
306 $block['subtype'] = $subtype;
308 unset($block['subtype']);
314 return [$npf, $text, $formatting];
317 static private function addPoster(array $media, array $block): array
320 if (!empty($media['preview'])) {
321 $poster['url'] = $media['preview'];
323 if (!empty($media['preview-width'])) {
324 $poster['width'] = $media['preview-width'];
326 if (!empty($media['preview-height'])) {
327 $poster['height'] = $media['preview-height'];
329 if (!empty($poster)) {
330 $block['poster'] = [$poster];
335 static private function addLinkBlockForUriId(int $uri_id, int $level, array $npf): array
337 foreach (Post\Media::getByURIId($uri_id, [Post\Media::HTML]) as $link) {
338 $host = parse_url($link['url'], PHP_URL_HOST);
339 if (in_array($host, ['www.youtube.com', 'youtu.be'])) {
342 'provider' => 'youtube',
343 'url' => $link['url'],
345 } elseif (in_array($host, ['vimeo.com'])) {
348 'provider' => 'vimeo',
349 'url' => $link['url'],
351 } elseif (in_array($host, ['open.spotify.com'])) {
354 'provider' => 'spotify',
355 'url' => $link['url'],
360 'url' => $link['url'],
362 if (!empty($link['name'])) {
363 $block['title'] = $link['name'];
365 if (!empty($link['description'])) {
366 $block['description'] = $link['description'];
368 if (!empty($link['author-name'])) {
369 $block['author'] = $link['author-name'];
371 if (!empty($link['publisher-name'])) {
372 $block['site_name'] = $link['publisher-name'];
377 $block['indent_level'] = $level;
380 $npf[] = self::addPoster($link, $block);
385 static private function addImageBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
388 foreach ($element->attributes as $key => $attribute) {
389 $attributes[$key] = trim($attribute->value);
391 if (empty($attributes['src'])) {
400 if (!empty($attributes['alt'])) {
401 $block['alt_text'] = $attributes['alt'];
404 if (!empty($attributes['title']) && (($attributes['alt'] ?? '') != $attributes['title'])) {
405 $block['caption'] = $attributes['title'];
408 $rid = Photo::ridFromURI($attributes['src']);
410 $photos = Photo::selectToArray([], ['resource-id' => $rid]);
411 foreach ($photos as $photo) {
412 $block['media'][] = [
413 'type' => $photo['type'],
414 'url' => str_replace('-0.', '-' . $photo['scale'] . '.', $attributes['src']),
415 'width' => $photo['width'],
416 'height' => $photo['height'],
419 if (empty($attributes['alt']) && !empty($photos[0]['desc'])) {
420 $block['alt_text'] = $photos[0]['desc'];
422 } elseif ($media = Post\Media::getByURL($uri_id, $attributes['src'], [Post\Media::IMAGE])) {
423 $block['media'][] = [
424 'type' => $media['mimetype'],
425 'url' => $media['url'],
426 'width' => $media['width'],
427 'height' => $media['height'],
429 if (empty($attributes['alt']) && !empty($media['description'])) {
430 $block['alt_text'] = $media['description'];
433 $block['media'][] = ['url' => $attributes['src']];
437 $block['indent_level'] = $level;
445 static private function addLinkBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
448 foreach ($element->attributes as $key => $attribute) {
449 $attributes[$key] = trim($attribute->value);
451 if (empty($attributes['href'])) {
455 $media = Post\Media::getByURL($uri_id, $attributes['href'], [Post\Media::AUDIO, Post\Media::VIDEO]);
456 if (!empty($media)) {
457 switch ($media['type']) {
458 case Post\Media::AUDIO:
462 'type' => $media['mimetype'],
463 'url' => $media['url'],
467 if (!empty($media['name'])) {
468 $block['title'] = $media['name'];
469 } elseif (!empty($media['description'])) {
470 $block['title'] = $media['description'];
473 $block = self::addPoster($media, $block);
476 case Post\Media::VIDEO:
480 'type' => $media['mimetype'],
481 'url' => $media['url'],
485 $block = self::addPoster($media, $block);
491 'text' => $element->textContent,
494 'end' => strlen($element->textContent),
496 'url' => $attributes['href']
502 $block['indent_level'] = $level;