diff --git a/CHANGELOG.md b/CHANGELOG.md index ca023e3c..6f48bc98 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: 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 diff --git a/models/BaseDoc.php b/models/BaseDoc.php index a988447c..413d8f03 100644 --- a/models/BaseDoc.php +++ b/models/BaseDoc.php @@ -231,26 +231,29 @@ 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); + $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 (mb_strlen($text, 'utf-8') >= $pos + 3) { - $abbrev = mb_substr($text, $pos - 1, 4, 'utf-8'); + if ($length >= $pos + 2) { + $abbrev = mb_substr($text, $pos - 3, 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 + 1, $length, 'utf-8'), $prevText ); } diff --git a/tests/models/BaseDocTest.php b/tests/models/BaseDocTest.php index a3031dcf..87e4ce97 100644 --- a/tests/models/BaseDocTest.php +++ b/tests/models/BaseDocTest.php @@ -17,15 +17,28 @@ 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); + } + + /** + * @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); } }