Skip to content
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
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
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);
}
}