From c34ca62dcb7bec8d0a094a169be9e2ef65df2647 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:02:36 +0300 Subject: [PATCH 01/13] Lazy fix `BaseDoc::extractFirstSentence()` source comment: ~~~markdown a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or URI template [RFC6570](https://tools.ietf.org/html/rfc6570). This property is required. ~~~ before fix `BaseDoc::extractFirstSentence()` return: `a URI [RFC3986](https://tools.` after: `a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or URI template [RFC6570](https://tools.ietf.org/html/rfc6570).` is dirty fix, but covering main cases --- models/BaseDoc.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/models/BaseDoc.php b/models/BaseDoc.php index a988447c..0925ae82 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -231,14 +231,16 @@ protected static function convertInlineLinks($content) } /** - * Extracts first sentence out of text + * Extracts first sentence out of text. + * * @param string $text * @param string $prevText * @return string */ public static function extractFirstSentence($text, $prevText = '') { - if (mb_strlen($text, 'utf-8') > 4 && ($pos = mb_strpos($text, '.', 4, 'utf-8')) !== false) { + $text = str_replace(["\r\n", "\n"], ' ', $text); + if (mb_strlen($text, 'utf-8') > 4 && ($pos = mb_strpos($text, '. ', 4, 'utf-8')) !== false) { $sentence = mb_substr($text, 0, $pos + 1, 'utf-8'); $prevText = $prevText . $sentence; From 23df534dc41399b3430e1aae1a6230bd36fe2b0d Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:11:56 +0300 Subject: [PATCH 02/13] Update BaseDoc.php --- models/BaseDoc.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/models/BaseDoc.php b/models/BaseDoc.php index 0925ae82..0d762e26 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -240,19 +240,20 @@ protected static function convertInlineLinks($content) public static function extractFirstSentence($text, $prevText = '') { $text = str_replace(["\r\n", "\n"], ' ', $text); - if (mb_strlen($text, 'utf-8') > 4 && ($pos = mb_strpos($text, '. ', 4, 'utf-8')) !== false) { - $sentence = mb_substr($text, 0, $pos + 1, 'utf-8'); + $length = mb_strlen($text, 'utf-8'); + if ($length > 4 && ($pos = mb_strpos($text, '. ', 4, 'utf-8')) !== false) { + $sentence = mb_substr($text, 0, $pos + 2, 'utf-8'); $prevText = $prevText . $sentence; - if (mb_strlen($text, 'utf-8') >= $pos + 3) { - $abbrev = mb_substr($text, $pos - 1, 4, 'utf-8'); + if ($length >= $pos + 4) { + $abbrev = mb_substr($text, $pos - 2, 4, 'utf-8'); // do not break sentence after abbreviation if ($abbrev === 'e.g.' || $abbrev === 'i.e.' || mb_substr_count($prevText, '`', 'utf-8') % 2 === 1 ) { $sentence .= static::extractFirstSentence( - mb_substr($text, $pos + 1, mb_strlen($text, 'utf-8'), 'utf-8'), + mb_substr($text, $pos + 2, $length, 'utf-8'), $prevText ); } From 1b19c4096cc34202b7edc0a010e1f0974e1e108a Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:21:18 +0300 Subject: [PATCH 03/13] Update BaseDoc.php --- models/BaseDoc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/BaseDoc.php b/models/BaseDoc.php index 0d762e26..7b431df4 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -246,7 +246,7 @@ public static function extractFirstSentence($text, $prevText = '') $prevText = $prevText . $sentence; if ($length >= $pos + 4) { - $abbrev = mb_substr($text, $pos - 2, 4, 'utf-8'); + $abbrev = mb_substr($text, $pos - 3, 4, 'utf-8'); // do not break sentence after abbreviation if ($abbrev === 'e.g.' || $abbrev === 'i.e.' || From c879f9f76c405970f37498ce664e139100b3e660 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:25:25 +0300 Subject: [PATCH 04/13] Update BaseDocTest.php --- tests/models/BaseDocTest.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/models/BaseDocTest.php b/tests/models/BaseDocTest.php index a3031dcf..8ddd9959 100644 --- a/tests/models/BaseDocTest.php +++ b/tests/models/BaseDocTest.php @@ -17,15 +17,22 @@ class BaseDocTest extends TestCase */ public function testExtractFirstSentenceWithBackticks() { - $initialText = 'fallback host info (e.g. `http://www.yiiframework.com`) used when ' . - '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] is invalid. This value will replace ' . - '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] before [[$denyCallback]] is called to make sure that ' . - 'an invalid host will not be used for further processing. You can set it to `null` to leave ' . - '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] untouched. Default value is empty string (this will ' . - 'result creating relative URLs instead of absolute).'; + $initialText = 'fallback host info (e.g. `http://www.yiiframework.com`) used when ' + . '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] is invalid. This value will replace ' + . '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] before [[$denyCallback]] is called to make sure that ' + . 'an invalid host will not be used for further processing. You can set it to `null` to leave ' + . '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] untouched. Default value is empty string (this will ' + . 'result creating relative URLs instead of absolute).'; $actualFirstSentence = BaseDoc::extractFirstSentence($initialText); - $expectedFirstSentence = 'fallback host info (e.g. `http://www.yiiframework.com`) used when ' . - '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] is invalid.'; + $expectedFirstSentence = 'fallback host info (e.g. `http://www.yiiframework.com`) used when ' + . '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] is invalid.'; + $this->assertEquals($expectedFirstSentence, $actualFirstSentence); + + $initialText = "a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or\n" + . 'URI template [RFC6570](https://tools.ietf.org/html/rfc6570). This property is required.'; + $actualFirstSentence = BaseDoc::extractFirstSentence($initialText); + $expectedFirstSentence = 'a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or' + . ' URI template [RFC6570](https://tools.ietf.org/html/rfc6570).'; $this->assertEquals($expectedFirstSentence, $actualFirstSentence); } } From bfbbf9cfbbca9e229a8e6aee027d82fcdfc13242 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:27:17 +0300 Subject: [PATCH 05/13] Update BaseDoc.php --- models/BaseDoc.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/BaseDoc.php b/models/BaseDoc.php index 7b431df4..2599005d 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -242,10 +242,10 @@ public static function extractFirstSentence($text, $prevText = '') $text = str_replace(["\r\n", "\n"], ' ', $text); $length = mb_strlen($text, 'utf-8'); if ($length > 4 && ($pos = mb_strpos($text, '. ', 4, 'utf-8')) !== false) { - $sentence = mb_substr($text, 0, $pos + 2, 'utf-8'); + $sentence = mb_substr($text, 0, $pos + 1, 'utf-8'); $prevText = $prevText . $sentence; - if ($length >= $pos + 4) { + if ($length >= $pos + 3) { $abbrev = mb_substr($text, $pos - 3, 4, 'utf-8'); // do not break sentence after abbreviation if ($abbrev === 'e.g.' || @@ -253,7 +253,7 @@ public static function extractFirstSentence($text, $prevText = '') mb_substr_count($prevText, '`', 'utf-8') % 2 === 1 ) { $sentence .= static::extractFirstSentence( - mb_substr($text, $pos + 2, $length, 'utf-8'), + mb_substr($text, $pos + 1, $length, 'utf-8'), $prevText ); } From 7ed8393d0d30c1ff05fa576fb6e50fd9a8fdd5df Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:41:35 +0300 Subject: [PATCH 06/13] Update BaseDoc.php --- models/BaseDoc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/BaseDoc.php b/models/BaseDoc.php index 2599005d..413d8f03 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -243,9 +243,9 @@ public static function extractFirstSentence($text, $prevText = '') $length = mb_strlen($text, 'utf-8'); if ($length > 4 && ($pos = mb_strpos($text, '. ', 4, 'utf-8')) !== false) { $sentence = mb_substr($text, 0, $pos + 1, 'utf-8'); - $prevText = $prevText . $sentence; + $prevText .= $sentence; - if ($length >= $pos + 3) { + if ($length >= $pos + 2) { $abbrev = mb_substr($text, $pos - 3, 4, 'utf-8'); // do not break sentence after abbreviation if ($abbrev === 'e.g.' || From c6fedd70ca1cc3171ca86ac7067897d461a1ab0d Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 20 Mar 2022 12:51:13 +0300 Subject: [PATCH 07/13] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca023e3c..9d2fee45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 apidoc extension Change Log 3.0.4 under development ----------------------- -- no changes in this release. +- Bug #282: Fix `BaseDoc::extractFirstSentence()` (WinterSilence) 3.0.3 February 19, 2022 From 980a6fd359dfd07d36429f6d1ef7732d1f0694a8 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 28 Mar 2022 14:29:46 +0300 Subject: [PATCH 08/13] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d2fee45..1c4e811d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,8 @@ Yii Framework 2 apidoc extension Change Log 3.0.4 under development ----------------------- - -- Bug #282: Fix `BaseDoc::extractFirstSentence()` (WinterSilence) +определение конца первого предложения в тексте +- Bug #282: Fix determining end of first sentence in `BaseDoc::extractFirstSentence()` (WinterSilence) 3.0.3 February 19, 2022 From 460e5d66a6bc4c1579e251682230ae7f406bff78 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 28 Mar 2022 14:30:03 +0300 Subject: [PATCH 09/13] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c4e811d..77ab699f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Yii Framework 2 apidoc extension Change Log 3.0.4 under development ----------------------- -определение конца первого предложения в тексте + - Bug #282: Fix determining end of first sentence in `BaseDoc::extractFirstSentence()` (WinterSilence) From 166bbadeef7804c44211a1686c9e9023dc2b1eb4 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 28 Mar 2022 14:46:17 +0300 Subject: [PATCH 10/13] Update CHANGELOG.md Co-authored-by: Alexey Rogachev --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77ab699f..1922151f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 apidoc extension Change Log 3.0.4 under development ----------------------- -- Bug #282: Fix determining end of first sentence in `BaseDoc::extractFirstSentence()` (WinterSilence) +- Bug #282: Consider the first sentence ended only if the dot is followed by a space in `BaseDoc::extractFirstSentence()` (WinterSilence) 3.0.3 February 19, 2022 From 1e5f3121b076260ffd56a088b381c6c7d1e8c617 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 28 Mar 2022 15:11:10 +0300 Subject: [PATCH 11/13] Update CHANGELOG.md Co-authored-by: Alexey Rogachev --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1922151f..6f48bc98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 apidoc extension Change Log 3.0.4 under development ----------------------- -- Bug #282: Consider the first sentence ended only if the dot is followed by a space in `BaseDoc::extractFirstSentence()` (WinterSilence) +- Bug #282: Convert newlines to spaces and consider the first sentence ended only if the dot is followed by a space in `BaseDoc::extractFirstSentence()` (WinterSilence) 3.0.3 February 19, 2022 From 6c7c10ff40dfea30f8d18c280a2134280b7a9adf Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 28 Mar 2022 15:20:25 +0300 Subject: [PATCH 12/13] Update BaseDocTest.php --- tests/models/BaseDocTest.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/models/BaseDocTest.php b/tests/models/BaseDocTest.php index 8ddd9959..3f4326ec 100644 --- a/tests/models/BaseDocTest.php +++ b/tests/models/BaseDocTest.php @@ -12,6 +12,16 @@ class BaseDocTest extends TestCase { + public function testExtractFirstSentence() + { + $initialText = "a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or\n" + . 'URI template [RFC6570](https://tools.ietf.org/html/rfc6570). This property is required.'; + $actualFirstSentence = BaseDoc::extractFirstSentence($initialText); + $expectedFirstSentence = 'a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or' + . ' URI template [RFC6570](https://tools.ietf.org/html/rfc6570).'; + $this->assertEquals($expectedFirstSentence, $actualFirstSentence); + } + /** * @link https://github.com/yiisoft/yii2-apidoc/issues/128 */ @@ -27,12 +37,5 @@ public function testExtractFirstSentenceWithBackticks() $expectedFirstSentence = 'fallback host info (e.g. `http://www.yiiframework.com`) used when ' . '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] is invalid.'; $this->assertEquals($expectedFirstSentence, $actualFirstSentence); - - $initialText = "a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or\n" - . 'URI template [RFC6570](https://tools.ietf.org/html/rfc6570). This property is required.'; - $actualFirstSentence = BaseDoc::extractFirstSentence($initialText); - $expectedFirstSentence = 'a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or' - . ' URI template [RFC6570](https://tools.ietf.org/html/rfc6570).'; - $this->assertEquals($expectedFirstSentence, $actualFirstSentence); } } From 95762d9738ca1deeb627520a1d696ee0e2f35c7d Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Tue, 29 Mar 2022 11:46:03 +0600 Subject: [PATCH 13/13] Minor additions to a new test --- tests/models/BaseDocTest.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/models/BaseDocTest.php b/tests/models/BaseDocTest.php index 3f4326ec..87e4ce97 100644 --- a/tests/models/BaseDocTest.php +++ b/tests/models/BaseDocTest.php @@ -12,16 +12,6 @@ class BaseDocTest extends TestCase { - public function testExtractFirstSentence() - { - $initialText = "a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or\n" - . 'URI template [RFC6570](https://tools.ietf.org/html/rfc6570). This property is required.'; - $actualFirstSentence = BaseDoc::extractFirstSentence($initialText); - $expectedFirstSentence = 'a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or' - . ' URI template [RFC6570](https://tools.ietf.org/html/rfc6570).'; - $this->assertEquals($expectedFirstSentence, $actualFirstSentence); - } - /** * @link https://github.com/yiisoft/yii2-apidoc/issues/128 */ @@ -38,4 +28,17 @@ public function testExtractFirstSentenceWithBackticks() . '[[\yii\web\Request::$hostInfo|Request::$hostInfo]] is invalid.'; $this->assertEquals($expectedFirstSentence, $actualFirstSentence); } + + /** + * @link https://github.com/yiisoft/yii2-apidoc/pull/282 + */ + public function testExtractFirstSentenceWithNewlineAndNoSpaceAfterDot() + { + $initialText = "a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or\n" + . 'URI template [RFC6570](https://tools.ietf.org/html/rfc6570). This property is required.'; + $actualFirstSentence = BaseDoc::extractFirstSentence($initialText); + $expectedFirstSentence = 'a URI [RFC3986](https://tools.ietf.org/html/rfc3986) or' + . ' URI template [RFC6570](https://tools.ietf.org/html/rfc6570).'; + $this->assertEquals($expectedFirstSentence, $actualFirstSentence); + } }