From ad854d5d401b34758cabb5c5ec18d46c79b7565d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 9 Mar 2019 23:21:19 -0500 Subject: [PATCH 1/4] Define absolute paths for various includes - Fix errors running local tests in IDE --- include/items.php | 3 ++- src/BaseObject.php | 2 +- src/Render/FriendicaSmartyEngine.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/items.php b/include/items.php index 88eec1dc4..a28e19a05 100644 --- a/include/items.php +++ b/include/items.php @@ -24,7 +24,8 @@ use Friendica\Util\ParseUrl; use Friendica\Util\Strings; use Friendica\Util\Temporal; -require_once 'mod/share.php'; +require_once __DIR__ . '/../mod/share.php'; + function add_page_info_data(array $data, $no_photos = false) { Hook::callAll('page_info_data', $data); diff --git a/src/BaseObject.php b/src/BaseObject.php index 4a6fa12d2..7e90478a9 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -4,7 +4,7 @@ */ namespace Friendica; -require_once 'boot.php'; +require_once __DIR__ . '/../boot.php'; use Friendica\Network\HTTPException\InternalServerErrorException; diff --git a/src/Render/FriendicaSmartyEngine.php b/src/Render/FriendicaSmartyEngine.php index 69a34ae56..36e46435f 100644 --- a/src/Render/FriendicaSmartyEngine.php +++ b/src/Render/FriendicaSmartyEngine.php @@ -17,7 +17,7 @@ class FriendicaSmartyEngine implements ITemplateEngine public function __construct() { - if (!is_writable('view/smarty3/')) { + if (!is_writable(__DIR__ . '/../../view/smarty3/')) { echo "ERROR: folder view/smarty3/ must be writable by webserver."; exit(); } From 184b51ec56295ff4b37a0ef9a865602cc19b0957 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 9 Mar 2019 23:25:53 -0500 Subject: [PATCH 2/4] Move autolink regex in Util\Strings --- src/Content/Text/BBCode.php | 19 +------------------ src/Util/Strings.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 2c324a433..2d1c681f4 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1268,24 +1268,7 @@ class BBCode extends BaseObject // 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 (!$for_plaintext) { - // Autolink feature (thanks to https://daringfireball.net/2010/07/improved_regex_for_matching_urls) - $autolink_regex = '@(?xi) -(?]+ # Run of non-space, non-()<> - | # or - \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels - | # or - [^\s`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars - )* -)@'; - $text = preg_replace($autolink_regex, '[url]$1[/url]', $text); + $text = preg_replace(Strings::autoLinkRegEx(), '[url]$1[/url]', $text); if ($simple_html == 7) { $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text); $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text); diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 55751d8d8..3edc9ba90 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -346,4 +346,30 @@ class Strings return $return; } + + /** + * Returns the regular expression string to match URLs in a given text + * + * @return string + * @see https://daringfireball.net/2010/07/improved_regex_for_matching_urls + */ + public static function autoLinkRegEx() + { + return '@(?xi) +(?]+ # Run of non-space, non-()<> + | # or + \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels + | # or + [^\s`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars + )* +)@'; + } } From 4d70e3282990a6c8f7216c92b9eb2f8b4f1828cd Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 9 Mar 2019 23:28:50 -0500 Subject: [PATCH 3/4] Add new Content\Text\MarkdownParser - Add autolinker to MarkdownParser->doAutoLinks() - Set hashtag_protection and url_filter_func properties of MarkdownParser --- src/Content/Text/Markdown.php | 16 +++++++++++----- src/Content/Text/MarkdownParser.php | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/Content/Text/MarkdownParser.php diff --git a/src/Content/Text/Markdown.php b/src/Content/Text/Markdown.php index e3e2cd3ac..a715de7e9 100644 --- a/src/Content/Text/Markdown.php +++ b/src/Content/Text/Markdown.php @@ -9,7 +9,6 @@ namespace Friendica\Content\Text; use Friendica\BaseObject; use Friendica\Core\System; use Friendica\Model\Contact; -use Michelf\MarkdownExtra; /** * Friendica-specific usage of Markdown @@ -31,11 +30,18 @@ class Markdown extends BaseObject public static function convert($text, $hardwrap = true) { $stamp1 = microtime(true); - $MarkdownParser = new MarkdownExtra(); - $MarkdownParser->hard_wrap = $hardwrap; - $MarkdownParser->code_class_prefix = 'language-'; + $MarkdownParser = new MarkdownParser(); + $MarkdownParser->code_class_prefix = 'language-'; + $MarkdownParser->hard_wrap = $hardwrap; + $MarkdownParser->hashtag_protection = true; + $MarkdownParser->url_filter_func = function ($url) { + if (strpos($url, '#') === 0) { + $url = ltrim($_SERVER['REQUEST_URI'], '/') . $url; + } + return $url; + }; + $html = $MarkdownParser->transform($text); - $html = preg_replace('/ Date: Sat, 9 Mar 2019 23:29:57 -0500 Subject: [PATCH 4/4] Add test for Text\Markdown::convert - Add folder for Markdown datasets --- .../content/text/markdown/bug-6633.html | 9 ++++ .../content/text/markdown/bug-6633.md | 9 ++++ tests/src/Content/Text/MarkdownTest.php | 52 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 tests/datasets/content/text/markdown/bug-6633.html create mode 100644 tests/datasets/content/text/markdown/bug-6633.md create mode 100644 tests/src/Content/Text/MarkdownTest.php diff --git a/tests/datasets/content/text/markdown/bug-6633.html b/tests/datasets/content/text/markdown/bug-6633.html new file mode 100644 index 000000000..313194e02 --- /dev/null +++ b/tests/datasets/content/text/markdown/bug-6633.html @@ -0,0 +1,9 @@ +

Produção de cebola em sistema orgânico com uso de homeopatia

+ +

Bulbos de cebola em sistema orgânico na fase de colheita
+

+ +

https://mapadaagroecologia.org/locais/epagri-estacao-experimental-de-ituporanga-sc?locale=pt-BR

+ +

#agroecologia #ecologia #orgânico #agroecology #brazil

diff --git a/tests/datasets/content/text/markdown/bug-6633.md b/tests/datasets/content/text/markdown/bug-6633.md new file mode 100644 index 000000000..330d49167 --- /dev/null +++ b/tests/datasets/content/text/markdown/bug-6633.md @@ -0,0 +1,9 @@ +# Produção de cebola em sistema orgânico com uso de homeopatia + +![Bulbos de cebola em sistema orgânico na fase de colheita +](https://mapadaagroecologia.org/system/midias/imagems/000/000/097/original/Cebola_em_sistema_org%C3%A2nico.jpg?1549640469 "Bulbos de cebola em sistema orgânico na fase de colheita +") + +## https://mapadaagroecologia.org/locais/epagri-estacao-experimental-de-ituporanga-sc?locale=pt-BR + +#agroecologia #ecologia #orgânico #agroecology #brazil \ No newline at end of file diff --git a/tests/src/Content/Text/MarkdownTest.php b/tests/src/Content/Text/MarkdownTest.php new file mode 100644 index 000000000..e39b46b2c --- /dev/null +++ b/tests/src/Content/Text/MarkdownTest.php @@ -0,0 +1,52 @@ +setUpVfsDir(); + $this->mockApp($this->root); + } + + public function dataMarkdown() + { + $inputFiles = glob(__DIR__ . '/../../../datasets/content/text/markdown/*.md'); + + $data = []; + + foreach ($inputFiles as $file) { + $data[str_replace('.md', '', $file)] = [ + 'input' => file_get_contents($file), + 'expected' => file_get_contents(str_replace('.md', '.html', $file)) + ]; + } + + return $data; + } + + /** + * Test convert different input Markdown text into HTML + * @dataProvider dataMarkdown + * + * @param string $input The Markdown text to test + * @param string $expected The expected HTML output + * @throws \Exception + */ + public function testConvert($input, $expected) + { + $output = Markdown::convert($input); + + $this->assertEquals($expected, $output); + } +} \ No newline at end of file