Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BaseDoc::extractFirstSentence() must consider previous text #235

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

Merged
samdark merged 1 commit into yiisoft:master from arogachev:fix-first-sentence-prev-text
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions models/BaseDoc.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,26 @@ protected static function convertInlineLinks($content)
/**
* Extracts first sentence out of text
* @param string $text
* @param string $prevText
* @return string
*/
public static function extractFirstSentence($text)
public static function extractFirstSentence($text, $prevText = '')
{
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;

if (mb_strlen($text, 'utf-8') >= $pos + 3) {
$abbrev = mb_substr($text, $pos - 1, 4, 'utf-8');
// do not break sentence after abbreviation
if ($abbrev === 'e.g.' ||
$abbrev === 'i.e.' ||
mb_substr_count($sentence, '`', 'utf-8') % 2 === 1 ||
mb_substr_count($text, '`', 'utf-8') % 2 === 1
mb_substr_count($prevText, '`', 'utf-8') % 2 === 1
) {
$sentence .= static::extractFirstSentence(mb_substr($text, $pos + 1, mb_strlen($text, 'utf-8'), 'utf-8'));
$sentence .= static::extractFirstSentence(
mb_substr($text, $pos + 1, mb_strlen($text, 'utf-8'), 'utf-8'),
$prevText
);
}
}
return $sentence;
Expand Down
14 changes: 10 additions & 4 deletions tests/models/BaseDocTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class BaseDocTest extends TestCase
*/
public function testExtractFirstSentenceWithBackticks()
{
$initialText = 'the host info (e.g. `http://www.example.com`) that is used by [[createAbsoluteUrl()]] to ' .
'prepend to created URLs.';
$firstSentence = BaseDoc::extractFirstSentence($initialText);
$this->assertEquals($initialText, $firstSentence);;
$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.';
$this->assertEquals($expectedFirstSentence, $actualFirstSentence);
}
}

AltStyle によって変換されたページ (->オリジナル) /