From 2002fc8a2a1a1349fe5d06b2b5d6c33c51779840 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 12:18:30 +0000 Subject: [PATCH 1/7] Better design for links in Mastodon posts --- include/bbcode.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/bbcode.php b/include/bbcode.php index d891f23dae..8969b35e11 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -170,6 +170,36 @@ function cleancss($input) { return $cleaned; } +function bb_style_url($match) { + $url = $match[1]; + + $parts = parse_url($url); + if (!isset($parts['scheme'])) { + return $url; + } + + $styled_url = $url; + + $scheme = $parts['scheme'].'://'; + $styled_url = str_replace($scheme, '', $styled_url); + + $html = ''. + ''; + + if (strlen($styled_url) > 30) { + $html .= '%s'. + ''; + + $ellipsis = substr($styled_url, 0, 30); + $rest = substr($styled_url, 30); + return sprintf($html, $url, $scheme, $ellipsis, $rest); + } else { + $html .= '%s'; + return sprintf($html, $url, $scheme, $styled_url); + } +} + + function stripcode_br_cb($s) { return '[code]' . str_replace('
', '', $s[1]) . '[/code]'; } @@ -956,6 +986,10 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa $Text = preg_replace("/([#])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text); + if ($simplehtml == 7) { + $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); + } + $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $Text); $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $Text); //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text); From 3f975f6751d92b3833fc2b240cf98c2d1aa8c906 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 12:35:44 +0000 Subject: [PATCH 2/7] More URL can be styled --- include/bbcode.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/bbcode.php b/include/bbcode.php index 8969b35e11..bea9a07f5b 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -173,9 +173,13 @@ function cleancss($input) { function bb_style_url($match) { $url = $match[1]; + if (isset($match[2]) AND ($match[1] != $match[2])) { + return $match[0]; + } + $parts = parse_url($url); if (!isset($parts['scheme'])) { - return $url; + return $match[0]; } $styled_url = $url; @@ -988,6 +992,7 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa if ($simplehtml == 7) { $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); + $Text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); } $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $Text); From 5abef34284ae0d63093544cc61b2bc58d10ffe48 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 14:02:32 +0000 Subject: [PATCH 3/7] Should work now ... --- include/bbcode.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/bbcode.php b/include/bbcode.php index bea9a07f5b..faf7eb6042 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -976,7 +976,8 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text // if ($simplehtml != 7) { if (!$forplaintext) { - $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text); + //$Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text); + $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $Text); } else { $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism", " $1 ", $Text); $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text); From aa4eafc30450dcbb0d80cd66d6c01f248a726d38 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 14:31:37 +0000 Subject: [PATCH 4/7] Even better? --- include/bbcode.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index faf7eb6042..4b5384a615 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -976,8 +976,14 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text // if ($simplehtml != 7) { if (!$forplaintext) { - //$Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text); - $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $Text); + if ($simplehtml != 7) { + $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text); + } else { + $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $Text); + + $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); + $Text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); + } } else { $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism", " $1 ", $Text); $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text); @@ -991,11 +997,6 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = fa $Text = preg_replace("/([#])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $Text); - if ($simplehtml == 7) { - $Text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); - $Text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'bb_style_url', $Text); - } - $Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/ism", '$1', $Text); $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$2', $Text); //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '$2', $Text); From 722e03cc58ed636fa1a3ace1b12ccd4d92e4ed08 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 17:29:46 +0000 Subject: [PATCH 5/7] Added documentation --- include/bbcode.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/bbcode.php b/include/bbcode.php index 4b5384a615..9518fe7dd1 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -170,6 +170,11 @@ function cleancss($input) { return $cleaned; } +/** + * @brief Converts [url] BBCodes in a format that looks fine on Mastodon. + * @param array $match Array with the matching values + * @return string replaced value + */ function bb_style_url($match) { $url = $match[1]; @@ -203,7 +208,6 @@ function bb_style_url($match) { } } - function stripcode_br_cb($s) { return '[code]' . str_replace('
', '', $s[1]) . '[/code]'; } From f602ec30ccde5700726bc51be1096b0a2c55b1ec Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 18:11:37 +0000 Subject: [PATCH 6/7] Shared links create now a better HTML --- include/bbcode.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index 9518fe7dd1..867c9fe2bf 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -55,18 +55,7 @@ function bb_attachment($Text, $simplehtml = false, $tryoembed = true) { } if ($simplehtml == 7) { - $title2 = $data["title"]; - - $test1 = trim(html_entity_decode($data["text"],ENT_QUOTES,'UTF-8')); - $test2 = trim(html_entity_decode($data["title"],ENT_QUOTES,'UTF-8')); - - // If the link description is similar to the text above then don't add the link description - if (($data["title"] != "") AND ((strpos($test1,$test2) !== false) OR - (similar_text($test1,$test2) / strlen($data["title"])) > 0.9)) { - $title2 = $data["url"]; - } - $text = sprintf('%s
', - $data["url"], $data["title"], $title2); + $text = style_url_for_mastodon($data["url"]); } elseif (($simplehtml != 4) AND ($simplehtml != 0)) { $text = sprintf('%s
', $data["url"], $data["title"]); } else { @@ -171,9 +160,9 @@ function cleancss($input) { } /** - * @brief Converts [url] BBCodes in a format that looks fine on Mastodon. + * @brief Converts [url] BBCodes in a format that looks fine on Mastodon. (callback function) * @param array $match Array with the matching values - * @return string replaced value + * @return string reformatted link including HTML codes */ function bb_style_url($match) { $url = $match[1]; @@ -187,8 +176,18 @@ function bb_style_url($match) { return $match[0]; } + return style_url_for_mastodon($url); +} + +/** + * @brief Converts [url] BBCodes in a format that looks fine on Mastodon. + * @param string $url URL that is about to be reformatted + * @return string reformatted link including HTML codes + */ +function style_url_for_mastodon($url) { $styled_url = $url; + $parts = parse_url($url); $scheme = $parts['scheme'].'://'; $styled_url = str_replace($scheme, '', $styled_url); From 3db3d2f1f7a1fef7ef39bccb087c52f9292c88ab Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 May 2017 19:11:47 +0000 Subject: [PATCH 7/7] This should make posts fine on GNU Social as well --- include/bbcode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bbcode.php b/include/bbcode.php index 867c9fe2bf..9a43cdb305 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -180,7 +180,7 @@ function bb_style_url($match) { } /** - * @brief Converts [url] BBCodes in a format that looks fine on Mastodon. + * @brief Converts [url] BBCodes in a format that looks fine on Mastodon and GNU Social. * @param string $url URL that is about to be reformatted * @return string reformatted link including HTML codes */ @@ -191,7 +191,7 @@ function style_url_for_mastodon($url) { $scheme = $parts['scheme'].'://'; $styled_url = str_replace($scheme, '', $styled_url); - $html = ''. + $html = ''. ''; if (strlen($styled_url) > 30) {