From 9525e079710c88eb247c9065b844240d96bde58d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 29 Jan 2019 20:25:51 -0500 Subject: [PATCH 1/3] Fix greedy regular expression and move code block escaping after tag extraction in Model\Item --- src/Model/Item.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index bb7053187e..b460d5f79f 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2386,18 +2386,6 @@ class Item extends BaseObject public static function setHashtags(&$item) { - - // What happens in [code], stays in [code]! - // escape the # and the [ - // hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3 - $item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", - function ($match) { - // we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks - $find = ['#', '[']; - $replace = [chr(27).'sharp', chr(27).'leftsquarebracket']; - return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); - }, $item["body"]); - $tags = BBCode::getTags($item["body"]); // No hashtags? @@ -2405,6 +2393,17 @@ class Item extends BaseObject return false; } + // What happens in [code], stays in [code]! + // escape the # and the [ + // hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3 + $item["body"] = preg_replace_callback("/\[code(.*?)\](.*?)\[\/code\]/ism", + function ($match) { + // we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks + $find = ['#', '[']; + $replace = [chr(27).'sharp', chr(27).'leftsquarebracket']; + return ("[code" . $match[1] . "]" . str_replace($find, $replace, $match[2]) . "[/code]"); + }, $item["body"]); + // This sorting is important when there are hashtags that are part of other hashtags // Otherwise there could be problems with hashtags like #test and #test2 rsort($tags); @@ -2463,12 +2462,12 @@ class Item extends BaseObject // Remember! What happens in [code], stays in [code] // roleback the # and [ - $item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", + $item["body"] = preg_replace_callback("/\[code(.*?)\](.*?)\[\/code\]/ism", function ($match) { // we truly unESCape all sharp and leftsquarebracket $find = [chr(27).'sharp', chr(27).'leftsquarebracket']; $replace = ['#', '[']; - return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); + return ("[code" . $match[1] . "]" . str_replace($find, $replace, $match[2]) . "[/code]"); }, $item["body"]); } From 30e97b4cc1f9dc707a916f5aece2188608a1d32b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 29 Jan 2019 20:39:05 -0500 Subject: [PATCH 2/3] Ignore code blocks when extracting tags --- src/Content/Text/BBCode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index e0190c0c1d..794410a690 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1743,7 +1743,7 @@ class BBCode extends BaseObject // Clean up the HTML by loading and saving the HTML with the DOM. // Bad structured html can break a whole page. - // For performance reasons do it only with ativated item cache or at export. + // For performance reasons do it only with activated item cache or at export. if (!$try_oembed || (get_itemcachepath() != "")) { $doc = new DOMDocument(); $doc->preserveWhiteSpace = false; @@ -1959,7 +1959,7 @@ class BBCode extends BaseObject $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string); // ignore anything in a code block - $string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string); + $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string); // Force line feeds at bbtags $string = str_replace(['[', ']'], ["\n[", "]\n"], $string); From bd031d0b87280d147a3ed39016f449bda458e49a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 29 Jan 2019 20:39:38 -0500 Subject: [PATCH 3/3] Add result window for item tags in mod/babel --- mod/babel.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mod/babel.php b/mod/babel.php index 64c9557767..6e47b81084 100644 --- a/mod/babel.php +++ b/mod/babel.php @@ -72,6 +72,21 @@ function babel_content() 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'), 'content' => visible_whitespace($bbcode4) ]; + + $item = [ + 'body' => $bbcode, + 'tag' => '', + ]; + + \Friendica\Model\Item::setHashtags($item); + $results[] = [ + 'title' => L10n::t('Item Body'), + 'content' => visible_whitespace($item['body']) + ]; + $results[] = [ + 'title' => L10n::t('Item Tags'), + 'content' => $item['tag'] + ]; break; case 'markdown': $markdown = trim($_REQUEST['text']);