]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Text/BBCode.php
Function moved to the "Strings" class
[friendica.git] / src / Content / Text / BBCode.php
index 930b94ac465bae41b947cf4933df15d3e6c113d3..0e5ee9d4e2e03600e470e8ad9a5f5fcb91a0f79e 100644 (file)
@@ -57,6 +57,7 @@ class BBCode
        const DIASPORA     = 3;
        const CONNECTORS   = 4;
        const TWITTER_API  = 5;
+       const NPF          = 6;
        const OSTATUS      = 7;
        const TWITTER      = 8;
        const BACKLINK     = 8;
@@ -507,26 +508,7 @@ class BBCode
         */
        private static function convertUrlForActivityPub(string $url): string
        {
-               return sprintf('<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', $url, self::getStyledURL($url));
-       }
-
-       /**
-        * Converts an URL in a nicer format (without the scheme and possibly shortened)
-        *
-        * @param string $url URL that is about to be reformatted
-        * @return string reformatted link
-        */
-       private static function getStyledURL(string $url): string
-       {
-               $parts = parse_url($url);
-               $scheme = $parts['scheme'] . '://';
-               $styled_url = str_replace($scheme, '', $url);
-
-               if (strlen($styled_url) > 30) {
-                       $styled_url = substr($styled_url, 0, 30) . "…";
-               }
-
-               return $styled_url;
+               return sprintf('<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', $url, Strings::getStyledURL($url));
        }
 
        /*
@@ -548,7 +530,7 @@ class BBCode
 
        /*
         * The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
-        * now turns back and the [noparse] tags are trimed
+        * now turns back and the [noparse] tags are trimmed
         * returning [i]italic[/i]
         *
         * @param array $match
@@ -1355,7 +1337,9 @@ class BBCode
 
                                /// @todo Have a closer look at the different html modes
                                // Handle attached links or videos
-                               if (in_array($simple_html, [self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
+                               if ($simple_html == self::NPF) {
+                                       $text = self::removeAttachment($text);
+                               } elseif (in_array($simple_html, [self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
                                        $text = self::replaceAttachment($text);
                                } elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) {
                                        $text = self::replaceAttachment($text, true);
@@ -1373,7 +1357,7 @@ class BBCode
                                        });
                                }
 
-                               // leave open the posibility of [map=something]
+                               // leave open the possibility of [map=something]
                                // this is replaced in Item::prepareBody() which has knowledge of the item location
                                if (strpos($text, '[/map]') !== false) {
                                        $text = preg_replace_callback(
@@ -1400,12 +1384,32 @@ class BBCode
                                }
 
                                // Check for headers
-                               $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '</p><h1>$1</h1><p>', $text);
-                               $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '</p><h2>$1</h2><p>', $text);
-                               $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '</p><h3>$1</h3><p>', $text);
-                               $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '</p><h4>$1</h4><p>', $text);
-                               $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '</p><h5>$1</h5><p>', $text);
-                               $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '</p><h6>$1</h6><p>', $text);
+
+                               if ($simple_html == self::INTERNAL) {
+                                       //Ensure to always start with <h4> if possible
+                                       $heading_count = 0;
+                                       for ($level = 6; $level > 0; $level--) { 
+                                               if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) {
+                                                       $heading_count++;
+                                               }
+                                       }
+                                       if ($heading_count > 0) {
+                                               $heading = min($heading_count + 3, 6);
+                                               for ($level = 6; $level > 0; $level--) { 
+                                                       if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) {
+                                                               $text = preg_replace("(\[h$level\](.*?)\[\/h$level\])ism", "</p><h$heading>$1</h$heading><p>", $text);
+                                                               $heading--;
+                                                       }
+                                               }
+                                       }
+                               } else {
+                                       $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '</p><h1>$1</h1><p>', $text);
+                                       $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '</p><h2>$1</h2><p>', $text);
+                                       $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '</p><h3>$1</h3><p>', $text);
+                                       $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '</p><h4>$1</h4><p>', $text);
+                                       $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '</p><h5>$1</h5><p>', $text);
+                                       $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '</p><h6>$1</h6><p>', $text);
+                               }
 
                                // Check for paragraph
                                $text = preg_replace("(\[p\](.*?)\[\/p\])ism", '<p>$1</p>', $text);
@@ -1568,7 +1572,7 @@ class BBCode
                                        function ($matches) use ($simple_html, $uriid) {
                                                $matches[1] = self::proxyUrl($matches[1], $simple_html, $uriid);
                                                $alt = htmlspecialchars($matches[2], ENT_COMPAT);
-                                               // Fix for Markdown problems wirh Diaspora, see issue #12701
+                                               // Fix for Markdown problems with Diaspora, see issue #12701
                                                if (($simple_html != self::DIASPORA) || strpos($matches[2], '"') === false) {
                                                        return '<img src="' . $matches[1] . '" alt="' . $alt . '" title="' . $alt . '">';
                                                } else {
@@ -1605,7 +1609,18 @@ class BBCode
                                // Simplify "video" element
                                $text = preg_replace('(\[video[^\]]*?\ssrc\s?=\s?([^\s\]]+)[^\]]*?\].*?\[/video\])ism', '[video]$1[/video]', $text);
 
-                               if ($try_oembed) {
+                               if ($simple_html == self::NPF) {
+                                       $text = preg_replace(
+                                               "/\[video\](.*?)\[\/video\]/ism",
+                                               '</p><video src="$1" controls width="100%" height="auto">$1</video><p>',
+                                               $text
+                                       );
+                                       $text = preg_replace(
+                                               "/\[audio\](.*?)\[\/audio\]/ism",
+                                               '</p><audio src="$1" controls>$1">$1</audio><p>',
+                                               $text
+                                       );
+                               } elseif ($try_oembed) {
                                        // html5 video and audio
                                        $text = preg_replace(
                                                "/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism",
@@ -1639,13 +1654,6 @@ class BBCode
                                $text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '<a href="$1">$1</a>', $text);
 
                                // Youtube extensions
-                               if ($try_oembed) {
-                                       $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
-                                       $text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
-                                       $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/shorts\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
-                                       $text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
-                               }
-
                                $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
                                $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
                                $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
@@ -1661,11 +1669,6 @@ class BBCode
                                        );
                                }
 
-                               if ($try_oembed) {
-                                       $text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism", $try_oembed_callback, $text);
-                                       $text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism", $try_oembed_callback, $text);
-                               }
-
                                $text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
                                $text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);