From 79132311f409f90f12ab95c13b9235f50c24f761 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 17 Nov 2016 12:39:43 -0800 Subject: [PATCH] Generate slightly shorter summaries in the typeahead browse dialog Summary: Ref T11034. Try to produce a roughly-one-sentence summary instead of a roughly-one-paragraph summary for the browse dialog. Test Plan: - Added unit tests, ran unit tests. - Wrote a longer summary for a project, browsed to it, saw a shorter summary. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11034 Differential Revision: https://secure.phabricator.com/D16892 --- src/__phutil_library_map__.php | 2 + .../PhabricatorProjectDatasource.php | 2 +- .../markup/PhabricatorMarkupEngine.php | 22 ++++++++++ .../PhabricatorMarkupEngineTestCase.php | 43 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/infrastructure/markup/__tests__/PhabricatorMarkupEngineTestCase.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index ab88fad093..89ef6d6b35 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2896,6 +2896,7 @@ phutil_register_library_map(array( 'PhabricatorManiphestTaskTestDataGenerator' => 'applications/maniphest/lipsum/PhabricatorManiphestTaskTestDataGenerator.php', 'PhabricatorMarkupCache' => 'applications/cache/storage/PhabricatorMarkupCache.php', 'PhabricatorMarkupEngine' => 'infrastructure/markup/PhabricatorMarkupEngine.php', + 'PhabricatorMarkupEngineTestCase' => 'infrastructure/markup/__tests__/PhabricatorMarkupEngineTestCase.php', 'PhabricatorMarkupInterface' => 'infrastructure/markup/PhabricatorMarkupInterface.php', 'PhabricatorMarkupOneOff' => 'infrastructure/markup/PhabricatorMarkupOneOff.php', 'PhabricatorMarkupPreviewController' => 'infrastructure/markup/PhabricatorMarkupPreviewController.php', @@ -7871,6 +7872,7 @@ phutil_register_library_map(array( 'PhabricatorManiphestTaskTestDataGenerator' => 'PhabricatorTestDataGenerator', 'PhabricatorMarkupCache' => 'PhabricatorCacheDAO', 'PhabricatorMarkupEngine' => 'Phobject', + 'PhabricatorMarkupEngineTestCase' => 'PhabricatorTestCase', 'PhabricatorMarkupOneOff' => array( 'Phobject', 'PhabricatorMarkupInterface', diff --git a/src/applications/project/typeahead/PhabricatorProjectDatasource.php b/src/applications/project/typeahead/PhabricatorProjectDatasource.php index 59ca5e2163..90294f29fb 100644 --- a/src/applications/project/typeahead/PhabricatorProjectDatasource.php +++ b/src/applications/project/typeahead/PhabricatorProjectDatasource.php @@ -130,7 +130,7 @@ final class PhabricatorProjectDatasource $description = idx($descriptions, $phid); if (strlen($description)) { - $summary = PhabricatorMarkupEngine::summarize($description); + $summary = PhabricatorMarkupEngine::summarizeSentence($description); $proj_result->addAttribute($summary); } } diff --git a/src/infrastructure/markup/PhabricatorMarkupEngine.php b/src/infrastructure/markup/PhabricatorMarkupEngine.php index 565df6cb44..6f13525c76 100644 --- a/src/infrastructure/markup/PhabricatorMarkupEngine.php +++ b/src/infrastructure/markup/PhabricatorMarkupEngine.php @@ -607,6 +607,28 @@ final class PhabricatorMarkupEngine extends Phobject { return array_values($files); } + public static function summarizeSentence($corpus) { + $corpus = trim($corpus); + $blocks = preg_split('/\n+/', $corpus, 2); + $block = head($blocks); + + $sentences = preg_split( + '/\b([.?!]+)\B/u', + $block, + 2, + PREG_SPLIT_DELIM_CAPTURE); + + if (count($sentences) > 1) { + $result = $sentences[0].$sentences[1]; + } else { + $result = head($sentences); + } + + return id(new PhutilUTF8StringTruncator()) + ->setMaximumGlyphs(128) + ->truncateString($result); + } + /** * Produce a corpus summary, in a way that shortens the underlying text * without truncating it somewhere awkward. diff --git a/src/infrastructure/markup/__tests__/PhabricatorMarkupEngineTestCase.php b/src/infrastructure/markup/__tests__/PhabricatorMarkupEngineTestCase.php new file mode 100644 index 0000000000..fa22876452 --- /dev/null +++ b/src/infrastructure/markup/__tests__/PhabricatorMarkupEngineTestCase.php @@ -0,0 +1,43 @@ +assertSentenceSummary( + 'The quick brown fox. Jumped over the lazy dog.', + 'The quick brown fox.'); + + $this->assertSentenceSummary( + 'Go to www.help.com for details. Good day.', + 'Go to www.help.com for details.'); + + $this->assertSentenceSummary( + 'Coxy lummox gives squid who asks for job pen.', + 'Coxy lummox gives squid who asks for job pen.'); + + $this->assertSentenceSummary( + 'DEPRECATED', + 'DEPRECATED'); + + $this->assertSentenceSummary( + 'Never use this! It is deadly poison.', + 'Never use this!'); + + $this->assertSentenceSummary( + "a short poem\nmeow meow meow\nmeow meow meow\n\n- cat", + 'a short poem'); + + $this->assertSentenceSummary( + 'WOW!! GREAT PROJECT!', + 'WOW!!'); + } + + private function assertSentenceSummary($corpus, $summary) { + $this->assertEqual( + $summary, + PhabricatorMarkupEngine::summarizeSentence($corpus), + pht('Summary of: %s', $corpus)); + } + +} -- 2.51.2