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);
190 list($npf, $text, $formatting) = self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting);
200 if ($parse_structure && $text) {
201 list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
203 return [$npf, $text, $formatting];
206 static private function getLevelByCallstack($callstack): int
209 foreach ($callstack as $entry) {
210 if (in_array($entry, ['ol', 'ul', 'blockquote'])) {
214 return max(0, $level - 1);
217 static private function getSubTypeByCallstack($callstack, string $text): string
220 foreach ($callstack as $entry) {
223 $subtype = 'ordered-list-item';
227 $subtype = 'unordered-list-item';
231 $subtype = self::$heading_subtype[$entry];
235 $subtype = self::$heading_subtype[$entry];
239 $subtype = self::$heading_subtype[$entry];
243 $subtype = self::$heading_subtype[$entry];
247 $subtype = self::$heading_subtype[$entry];
251 $subtype = self::$heading_subtype[$entry];
255 $subtype = strlen($text) < 100 ? 'quote' : 'indented';
259 $subtype = 'indented';
270 static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array $npf, string $text, array $formatting): array
272 $start = mb_strlen($text);
273 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
278 'end' => mb_strlen($text),
282 return [$npf, $text, $formatting];
285 static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array $npf, string $text, array $formatting): array
287 $start = mb_strlen($text);
288 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
291 foreach ($element->attributes as $key => $attribute) {
292 $attributes[$key] = trim($attribute->value);
294 if (!empty($attributes['href'])) {
297 'end' => mb_strlen($text),
299 'url' => $attributes['href']
302 return [$npf, $text, $formatting];
305 static private function addBlock(string $text, array $formatting, array $npf, array $callstack): array
313 if (!empty($formatting)) {
314 $block['formatting'] = $formatting;
317 $level = self::getLevelByCallstack($callstack);
319 $block['indent_level'] = $level;
322 $subtype = self::getSubTypeByCallstack($callstack, $text);
324 $block['subtype'] = $subtype;
326 unset($block['subtype']);
332 return [$npf, $text, $formatting];
335 static private function addPoster(array $media, array $block): array
338 if (!empty($media['preview'])) {
339 $poster['url'] = $media['preview'];
341 if (!empty($media['preview-width'])) {
342 $poster['width'] = $media['preview-width'];
344 if (!empty($media['preview-height'])) {
345 $poster['height'] = $media['preview-height'];
347 if (!empty($poster)) {
348 $block['poster'] = $poster;
353 static private function addLinkBlockForUriId(int $uri_id, int $level, array $npf): array
355 foreach (Post\Media::getByURIId($uri_id, [Post\Media::HTML]) as $link) {
356 $host = parse_url($link['url'], PHP_URL_HOST);
357 if (in_array($host, ['www.youtube.com', 'youtu.be'])) {
360 'provider' => 'youtube',
361 'url' => $link['url'],
363 } elseif (in_array($host, ['vimeo.com'])) {
366 'provider' => 'vimeo',
367 'url' => $link['url'],
369 } elseif (in_array($host, ['open.spotify.com'])) {
372 'provider' => 'spotify',
373 'url' => $link['url'],
378 'url' => $link['url'],
380 if (!empty($link['name'])) {
381 $block['title'] = $link['name'];
383 if (!empty($link['description'])) {
384 $block['description'] = $link['description'];
386 if (!empty($link['author-name'])) {
387 $block['author'] = $link['author-name'];
389 if (!empty($link['publisher-name'])) {
390 $block['site_name'] = $link['publisher-name'];
395 $block['indent_level'] = $level;
398 $npf[] = self::addPoster($link, $block);
403 static private function addImageBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
406 foreach ($element->attributes as $key => $attribute) {
407 $attributes[$key] = trim($attribute->value);
409 if (empty($attributes['src'])) {
418 if (!empty($attributes['alt'])) {
419 $block['alt_text'] = $attributes['alt'];
422 if (!empty($attributes['title']) && (($attributes['alt'] ?? '') != $attributes['title'])) {
423 $block['caption'] = $attributes['title'];
426 $rid = Photo::ridFromURI($attributes['src']);
428 $photos = Photo::selectToArray([], ['resource-id' => $rid]);
429 foreach ($photos as $photo) {
430 $block['media'][] = [
431 'type' => $photo['type'],
432 'url' => str_replace('-0.', '-' . $photo['scale'] . '.', $attributes['src']),
433 'width' => $photo['width'],
434 'height' => $photo['height'],
437 if (empty($attributes['alt']) && !empty($photos[0]['desc'])) {
438 $block['alt_text'] = $photos[0]['desc'];
440 } elseif ($media = Post\Media::getByURL($uri_id, $attributes['src'], [Post\Media::IMAGE])) {
441 $block['media'][] = [
442 'type' => $media['mimetype'],
443 'url' => $media['url'],
444 'width' => $media['width'],
445 'height' => $media['height'],
447 if (empty($attributes['alt']) && !empty($media['description'])) {
448 $block['alt_text'] = $media['description'];
451 $block['media'][] = ['url' => $attributes['src']];
455 $block['indent_level'] = $level;
463 static private function addLinkBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
466 foreach ($element->attributes as $key => $attribute) {
467 $attributes[$key] = trim($attribute->value);
469 if (empty($attributes['href'])) {
473 $media = Post\Media::getByURL($uri_id, $attributes['href'], [Post\Media::AUDIO, Post\Media::VIDEO]);
474 if (!empty($media)) {
475 switch ($media['type']) {
476 case Post\Media::AUDIO:
480 'type' => $media['mimetype'],
481 'url' => $media['url'],
485 if (!empty($media['name'])) {
486 $block['title'] = $media['name'];
487 } elseif (!empty($media['description'])) {
488 $block['title'] = $media['description'];
491 $block = self::addPoster($media, $block);
494 case Post\Media::VIDEO:
498 'type' => $media['mimetype'],
499 'url' => $media['url'],
503 $block = self::addPoster($media, $block);
509 'text' => $element->textContent,
512 'end' => strlen($element->textContent),
514 'url' => $attributes['href']
520 $block['indent_level'] = $level;