]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/NPF.php
Merge pull request #13368 from MrPetovan/bug/13367-post-plink
[friendica.git] / src / Content / Text / NPF.php
index a919562c744830028de035d2f1fe2805cb0d2a62..f5a6e2dc188baf09a24f95accfdb71895a918b1a 100644 (file)
@@ -32,18 +32,26 @@ use Friendica\Model\Post;
  */
 class NPF
 {
-       static $heading_subtype = [];
-
-       static public function fromBBCode(string $bbcode, int $uri_id): array
+       private static $heading_subtype = [];
+
+       /**
+        * Convert BBCode into NPF (Tumblr Neue Post Format)
+        *
+        * @param string $bbcode
+        * @param integer $uri_id
+        * @return array NPF
+        */
+       public static function fromBBCode(string $bbcode, int $uri_id): array
        {
                $bbcode = self::prepareBody($bbcode);
 
-               $html = BBCode::convert($bbcode, false, BBCode::CONNECTORS);
+               $html = BBCode::convertForUriId($uri_id, $bbcode, BBCode::NPF);
                if (empty($html)) {
                        return [];
                }
 
                $doc = new DOMDocument();
+
                $doc->formatOutput = true;
                if (!@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
                        return [];
@@ -58,7 +66,13 @@ class NPF
                return self::addLinkBlockForUriId($uri_id, 0, $npf);
        }
 
-       static function setHeadingSubStyles($doc)
+       /**
+        * Fetch the heading types
+        *
+        * @param DOMDocument $doc
+        * @return void
+        */
+       private static function setHeadingSubStyles(DOMDocument $doc)
        {
                self::$heading_subtype = [];
                foreach (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as $element) {
@@ -72,55 +86,69 @@ class NPF
                }
        }
 
-       static private function prepareBody(string $body): string
+       /**
+        * Prepare the BBCode for the NPF conversion
+        *
+        * @param string $bbcode
+        * @return string
+        */
+       private static function prepareBody(string $bbcode): string
        {
-               $shared = BBCode::fetchShareAttributes($body);
+               $shared = BBCode::fetchShareAttributes($bbcode);
                if (!empty($shared)) {
-                       $body = $shared['shared'];
+                       $bbcode = $shared['shared'];
                }
 
-               $body = BBCode::removeAttachment($body);
-
-               $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
+               $bbcode = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $bbcode);
 
-               if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
+               if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $bbcode, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
                                if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
-                                       $body = str_replace($picture[0], "\n\n[img=" . str_replace('-1.', '-0.', $picture[2]) . "]" . $picture[3] . "[/img]\n\n", $body);
+                                       $bbcode = str_replace($picture[0], "\n\n[img=" . str_replace('-1.', '-0.', $picture[2]) . "]" . $picture[3] . "[/img]\n\n", $bbcode);
                                }
                        }
                }
 
-               $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", "\n\n[img=$1]$2[/img]\n\n", $body);
+               $bbcode = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", "\n\n[img=$1]$2[/img]\n\n", $bbcode);
 
-               if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
+               if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $bbcode, $pictures, PREG_SET_ORDER)) {
                        foreach ($pictures as $picture) {
                                if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
-                                       $body = str_replace($picture[0], "\n\n[img]" . str_replace('-1.', '-0.', $picture[2]) . "[/img]\n\n", $body);
+                                       $bbcode = str_replace($picture[0], "\n\n[img]" . str_replace('-1.', '-0.', $picture[2]) . "[/img]\n\n", $bbcode);
                                }
                        }
                }
 
-               $body = preg_replace("/\[img\](.*?)\[\/img\]/ism", "\n\n[img]$1[/img]\n\n", $body);
-               $body = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", "\n\n[audio]$1[/audio]\n\n", $body);
-               $body = preg_replace("/\[video\](.*?)\[\/video\]/ism", "\n\n[video]$1[/video]\n\n", $body);
+               $bbcode = preg_replace("/\[img\](.*?)\[\/img\]/ism", "\n\n[img]$1[/img]\n\n", $bbcode);
 
                do {
-                       $oldbody = $body;
-                       $body = str_replace(["\n\n\n"], ["\n\n"], $body);
-               } while ($oldbody != $body);
+                       $oldbbcode = $bbcode;
+                       $bbcode    = str_replace(["\n\n\n"], ["\n\n"], $bbcode);
+               } while ($oldbbcode != $bbcode);
 
-               return trim($body);
+               return trim($bbcode);
        }
 
-       static private function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array $npf = [], string $text = '', array $formatting = []): array
+       /**
+        * Walk recursively through the HTML
+        *
+        * @param DOMElement $element
+        * @param integer $uri_id
+        * @param boolean $parse_structure
+        * @param array $callstack
+        * @param array $npf
+        * @param string $text
+        * @param array $formatting
+        * @return array
+        */
+       private static function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array $npf = [], string $text = '', array $formatting = []): array
        {
                if ($parse_structure && $text) {
                        list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
                }
 
                $callstack[] = $element->nodeName;
-               $level = self::getLevelByCallstack($callstack);
+               $level       = self::getLevelByCallstack($callstack);
 
                foreach ($element->childNodes as $child) {
                        switch ($child->nodeName) {
@@ -128,12 +156,12 @@ class NPF
                                case 'strong':
                                        list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'bold', $callstack, $npf, $text, $formatting);
                                        break;
-       
+
                                case 'i':
                                case 'em':
                                        list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'italic', $callstack, $npf, $text, $formatting);
                                        break;
-       
+
                                case 's':
                                        list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'strikethrough', $callstack, $npf, $text, $formatting);
                                        break;
@@ -149,7 +177,7 @@ class NPF
                                                $text .= "\n";
                                        }
                                        break;
-               
+
                                case '#text':
                                        $text .= $child->textContent;
                                        break;
@@ -160,18 +188,19 @@ class NPF
                                        break;
 
                                case 'a':
-                                       if ($text) {
-                                               list($npf, $text, $formatting) = self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting);
-                                       } else {
-                                               $npf = self::addLinkBlock($child, $uri_id, $level, $npf);
-                                       }
+                                       list($npf, $text, $formatting) = self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting);
                                        break;
 
                                case 'img':
                                        $npf = self::addImageBlock($child, $uri_id, $level, $npf);
                                        break;
 
-                                       default:
+                               case 'audio':
+                               case 'video':
+                                       $npf = self::addMediaBlock($child, $uri_id, $level, $npf);
+                                       break;
+
+                               default:
                                        list($npf, $text, $formatting) = self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting);
                                        break;
                        }
@@ -183,7 +212,13 @@ class NPF
                return [$npf, $text, $formatting];
        }
 
-       static private function getLevelByCallstack($callstack): int
+       /**
+        * Return the correct indent level
+        *
+        * @param array $callstack
+        * @return integer
+        */
+       private static function getLevelByCallstack(array $callstack): int
        {
                $level = 0;
                foreach ($callstack as $entry) {
@@ -194,7 +229,14 @@ class NPF
                return max(0, $level - 1);
        }
 
-       static private function getSubTypeByCallstack($callstack, string $text): string
+       /**
+        * Detect the subtype via the HTML element callstack
+        *
+        * @param array $callstack
+        * @param string $text
+        * @return string
+        */
+       private static function getSubTypeByCallstack(array $callstack, string $text): string
        {
                $subtype = '';
                foreach ($callstack as $entry) {
@@ -210,28 +252,28 @@ class NPF
                                case 'h1':
                                        $subtype = self::$heading_subtype[$entry];
                                        break;
-       
+
                                case 'h2':
                                        $subtype = self::$heading_subtype[$entry];
                                        break;
-       
+
                                case 'h3':
                                        $subtype = self::$heading_subtype[$entry];
                                        break;
-       
+
                                case 'h4':
                                        $subtype = self::$heading_subtype[$entry];
                                        break;
-       
+
                                case 'h5':
                                        $subtype = self::$heading_subtype[$entry];
                                        break;
-       
+
                                case 'h6':
                                        $subtype = self::$heading_subtype[$entry];
                                        break;
-       
-                               case 'blockquote':                                      
+
+                               case 'blockquote':
                                        $subtype = mb_strlen($text) < 100 ? 'quote' : 'indented';
                                        break;
 
@@ -247,9 +289,22 @@ class NPF
                return $subtype;
        }
 
-       static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array $npf, string $text, array $formatting): array
+       /**
+        * Add formatting for a text block
+        *
+        * @param DOMElement $element
+        * @param integer $uri_id
+        * @param string $type
+        * @param array $callstack
+        * @param array $npf
+        * @param string $text
+        * @param array $formatting
+        * @return array
+        */
+       private static function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array $npf, string $text, array $formatting): array
        {
                $start = mb_strlen($text);
+
                list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
 
                if (!empty($type)) {
@@ -262,9 +317,21 @@ class NPF
                return [$npf, $text, $formatting];
        }
 
-       static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array $npf, string $text, array $formatting): array
+       /**
+        * Add an inline link for a text block
+        *
+        * @param DOMElement $element
+        * @param integer $uri_id
+        * @param array $callstack
+        * @param array $npf
+        * @param string $text
+        * @param array $formatting
+        * @return array
+        */
+       private static function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array $npf, string $text, array $formatting): array
        {
                $start = mb_strlen($text);
+
                list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
 
                $attributes = [];
@@ -282,12 +349,21 @@ class NPF
                return [$npf, $text, $formatting];
        }
 
-       static private function addBlock(string $text, array $formatting, array $npf, array $callstack): array
+       /**
+        * Add a text block
+        *
+        * @param string $text
+        * @param array $formatting
+        * @param array $npf
+        * @param array $callstack
+        * @return array
+        */
+       private static function addBlock(string $text, array $formatting, array $npf, array $callstack): array
        {
                $block = [
-                       'type'      => 'text',
-                       'subtype'   => '',
-                       'text'      => $text,
+                       'type'    => 'text',
+                       'subtype' => '',
+                       'text'    => $text,
                ];
 
                if (!empty($formatting)) {
@@ -307,12 +383,17 @@ class NPF
                }
 
                $npf[] = $block;
-               $text = '';
-               $formatting = [];
-               return [$npf, $text, $formatting];
+               return [$npf, '', []];
        }
 
-       static private function addPoster(array $media, array $block): array
+       /**
+        * Add a block for a preview picture
+        *
+        * @param array $media
+        * @param array $block
+        * @return array
+        */
+       private static function addPoster(array $media, array $block): array
        {
                $poster = [];
                if (!empty($media['preview'])) {
@@ -330,7 +411,15 @@ class NPF
                return $block;
        }
 
-       static private function addLinkBlockForUriId(int $uri_id, int $level, array $npf): array
+       /**
+        * Add a link block from the HTML attachment of a given post uri-id
+        *
+        * @param integer $uri_id
+        * @param integer $level
+        * @param array $npf
+        * @return array
+        */
+       private static function addLinkBlockForUriId(int $uri_id, int $level, array $npf): array
        {
                foreach (Post\Media::getByURIId($uri_id, [Post\Media::HTML]) as $link) {
                        $host = parse_url($link['url'], PHP_URL_HOST);
@@ -380,7 +469,16 @@ class NPF
                return $npf;
        }
 
-       static private function addImageBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
+       /**
+        * Add an image block
+        *
+        * @param DOMElement $element
+        * @param integer $uri_id
+        * @param integer $level
+        * @param array $npf
+        * @return array
+        */
+       private static function addImageBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
        {
                $attributes = [];
                foreach ($element->attributes as $key => $attribute) {
@@ -440,22 +538,31 @@ class NPF
                return $npf;
        }
 
-       static private function addLinkBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
+       /**
+        * Add an audio or video block
+        *
+        * @param DOMElement $element
+        * @param integer $uri_id
+        * @param integer $level
+        * @param array $npf
+        * @return array
+        */
+       private static function addMediaBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
        {
                $attributes = [];
                foreach ($element->attributes as $key => $attribute) {
                        $attributes[$key] = trim($attribute->value);
                }
-               if (empty($attributes['href'])) {
+               if (empty($attributes['src'])) {
                        return $npf;
                }
 
-               $media = Post\Media::getByURL($uri_id, $attributes['href'], [Post\Media::AUDIO, Post\Media::VIDEO]);
+               $media = Post\Media::getByURL($uri_id, $attributes['src'], [Post\Media::AUDIO, Post\Media::VIDEO]);
                if (!empty($media)) {
                        switch ($media['type']) {
                                case Post\Media::AUDIO:
                                        $block = [
-                                               'type' => 'audio',
+                                               'type'  => 'audio',
                                                'media' => [
                                                        'type' => $media['mimetype'],
                                                        'url'  => $media['url'],
@@ -473,7 +580,7 @@ class NPF
 
                                case Post\Media::VIDEO:
                                        $block = [
-                                               'type' => 'video',
+                                               'type'  => 'video',
                                                'media' => [
                                                        'type' => $media['mimetype'],
                                                        'url'  => $media['url'],
@@ -485,14 +592,14 @@ class NPF
                        }
                } else {
                        $block = [
-                               'type' => 'text',
-                               'text' => $element->textContent,
+                               'type'       => 'text',
+                               'text'       => $element->textContent,
                                'formatting' => [
                                        [
                                                'start' => 0,
                                                'end'   => mb_strlen($element->textContent),
                                                'type'  => 'link',
-                                               'url'   => $attributes['href']
+                                               'url'   => $attributes['src']
                                        ]
                                ]
                        ];