Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert newlines to spaces and consider the first sentence ended only if the dot is followed by a space in BaseDoc::extractFirstSentence() #282

Merged
merged 13 commits into from
Mar 29, 2022
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 apidoc extension Change Log
3.0.4 under development
-----------------------

- no changes in this release.
- Bug #282: Consider the first sentence ended only if the dot is followed by a space in `BaseDoc::extractFirstSentence()` (WinterSilence)
WinterSilence marked this conversation as resolved.
Show resolved Hide resolved


3.0.3 February 19, 2022
Expand Down
15 changes: 9 additions & 6 deletions models/BaseDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
23 changes: 15 additions & 8 deletions tests/models/BaseDocTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
WinterSilence marked this conversation as resolved.
Show resolved Hide resolved
. '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);
}
}