From 229fb0439e3971ad93fb76b85ab41248606e2d6a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 11 Mar 2018 10:54:02 +0000 Subject: [PATCH] Links to Diaspora had sometimes been cut --- src/Content/Text/BBCode.php | 46 ++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 091957c19..dcf1f4481 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -754,7 +754,7 @@ class BBCode if (($data["url"] != "") && ($data["title"] != "")) { $text .= "\n[url=" . $data["url"] . "]" . $data["title"] . "[/url]"; } elseif (($data["url"] != "")) { - $text .= "\n" . $data["url"]; + $text .= "\n[url]" . $data["url"] . "[/url]"; } return $text . "\n" . $data["after"]; @@ -831,6 +831,47 @@ class BBCode } } + /** + * @brief Shortens [url] BBCodes in a format that looks less ugly than the full address. (callback function) + * @param array $match Array with the matching values + * @return string reformatted link including HTML codes + */ + private static function shortenVisibleUrlCallback($match) + { + $url = $match[1]; + + if (isset($match[2]) && ($match[1] != $match[2])) { + return $match[0]; + } + + $parts = parse_url($url); + if (!isset($parts['scheme'])) { + return $match[0]; + } + + return self::shortenVisibleUrl($url); + } + + /** + * @brief Shortens [url] BBCodes in a format that looks less ugly than the full address. + * @param string $url URL that is about to be reformatted + * @return string reformatted link including HTML codes + */ + private static function shortenVisibleUrl($url) + { + $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) . "…"; + } + + $html = '%s'; + + return sprintf($html, $url, $styled_url); + } + /* * [noparse][i]italic[/i][/noparse] turns into * [noparse][ i ]italic[ /i ][/noparse], @@ -1459,6 +1500,9 @@ class BBCode if ($simple_html == 7) { $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForMastodonCallback', $text); $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForMastodonCallback', $text); + } else { + $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::shortenVisibleUrlCallback', $text); + $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::shortenVisibleUrlCallback', $text); } } else { $text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism", " $1 ", $text);