From 41710de6a901037d46b384c574a7a53ea7574df5 Mon Sep 17 00:00:00 2001 From: nojimage Date: Mon, 28 Jan 2019 13:06:24 +0900 Subject: [PATCH] refs #28 Add usernameIncludeSymbol option to Autolink class --- lib/Twitter/Text/Autolink.php | 79 +++++++++++++++++++++++++++------ tests/TestCase/AutolinkTest.php | 12 +++++ 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/lib/Twitter/Text/Autolink.php b/lib/Twitter/Text/Autolink.php index a6f29a6..e18ee1c 100644 --- a/lib/Twitter/Text/Autolink.php +++ b/lib/Twitter/Text/Autolink.php @@ -129,6 +129,14 @@ class Autolink */ protected $invisibleTagAttrs = "style='position:absolute;left:-9999px;'"; + /** + * If the at mark '@' should be included in the link (false by default) + * + * @var bool + * @since 3.0.1 + */ + protected $usernameIncludeSymbol = false; + /** * * @var Extractor @@ -387,6 +395,29 @@ public function setTarget($v) return $this; } + /** + * @return bool + * @since 3.0.1 + */ + public function isUsernameIncludeSymbol() + { + return $this->usernameIncludeSymbol; + } + + /** + * Set if the at mark '@' should be included in the link (false by default) + * + * @param bool $usernameIncludeSymbol if username includes symbol + * @return Autolink + * @since 3.0.1 + */ + public function setUsernameIncludeSymbol($usernameIncludeSymbol) + { + $this->usernameIncludeSymbol = $usernameIncludeSymbol; + + return $this; + } + /** * Autolink with entities * @@ -404,18 +435,14 @@ public function autoLinkEntities($tweet = null, $entities = null) $text = ''; $beginIndex = 0; foreach ($entities as $entity) { - if (isset($entity['screen_name'])) { - $text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex + 1); - } else { - $text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex); - } + $text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex); if (isset($entity['url'])) { $text .= $this->linkToUrl($entity); } elseif (isset($entity['hashtag'])) { $text .= $this->linkToHashtag($entity, $tweet); } elseif (isset($entity['screen_name'])) { - $text .= $this->linkToMentionAndList($entity); + $text .= $this->linkToMentionAndList($entity, $tweet); } elseif (isset($entity['cashtag'])) { $text .= $this->linkToCashtag($entity, $tweet); } @@ -651,7 +678,7 @@ public function linkToHashtag($entity, $tweet = null) $attributes = array(); $class = array(); $hash = StringUtils::substr($tweet, $entity['indices'][0], 1); - $linkText = $hash . $entity['hashtag']; + $linkText = $entity['hashtag']; $attributes['href'] = $this->url_base_hash . $entity['hashtag']; $attributes['title'] = '#' . $entity['hashtag']; @@ -665,18 +692,20 @@ public function linkToHashtag($entity, $tweet = null) $attributes['class'] = implode(' ', $class); } - return $this->linkToText($entity, $linkText, $attributes); + return $this->linkToTextWithSymbol($entity, $hash, $linkText, $attributes); } /** * * @param array $entity + * @param string $tweet * @return string * @since 1.1.0 */ - public function linkToMentionAndList($entity) + public function linkToMentionAndList($entity, $tweet) { $attributes = array(); + $symbol = StringUtils::substr($tweet, $entity['indices'][0], 1); if (!empty($entity['list_slug'])) { # Replace the list and username @@ -694,7 +723,7 @@ public function linkToMentionAndList($entity) } $attributes['href'] = $url; - return $this->linkToText($entity, $linkText, $attributes); + return $this->linkToTextWithSymbol($entity, $symbol, $linkText, $attributes); } /** @@ -710,15 +739,15 @@ public function linkToCashtag($entity, $tweet = null) $tweet = $this->tweet; } $attributes = array(); - $doller = StringUtils::substr($tweet, $entity['indices'][0], 1); - $linkText = $doller . $entity['cashtag']; + $dollar = StringUtils::substr($tweet, $entity['indices'][0], 1); + $linkText = $entity['cashtag']; $attributes['href'] = $this->url_base_cash . $entity['cashtag']; - $attributes['title'] = $linkText; + $attributes['title'] = '$' . $linkText; if (!empty($this->class_cash)) { $attributes['class'] = $this->class_cash; } - return $this->linkToText($entity, $linkText, $attributes); + return $this->linkToTextWithSymbol($entity, $dollar, $linkText, $attributes); } /** @@ -752,6 +781,28 @@ public function linkToText(array $entity, $text, $attributes = array()) return $link; } + /** + * + * @param array $entity + * @param string $symbol + * @param string $linkText + * @param array $attributes + * @return string + * @since 3.0.1 + */ + protected function linkToTextWithSymbol(array $entity, $symbol, $linkText, array $attributes) + { + $includeSymbol = $this->usernameIncludeSymbol || !preg_match('/[@@]/u', $symbol); + + if (!$includeSymbol) { + return $symbol . $this->linkToText($entity, $linkText, $attributes); + } + + $linkText = $symbol . $linkText; + + return $this->linkToText($entity, $linkText, $attributes); + } + /** * html escape * diff --git a/tests/TestCase/AutolinkTest.php b/tests/TestCase/AutolinkTest.php index 6672997..49c1cae 100644 --- a/tests/TestCase/AutolinkTest.php +++ b/tests/TestCase/AutolinkTest.php @@ -52,4 +52,16 @@ public function testAutolinkWithEmoji() $this->assertSame($expected, $linkedText); } + + public function testUsernameIncludeSymbol() + { + $tweet = 'Testing @mention and @mention/list'; + // @codingStandardsIgnoreStart + $expected = 'Testing @mention and @mention/list'; + // @codingStandardsIgnoreEnd + + $this->linker->setUsernameIncludeSymbol(true); + $linkedText = $this->linker->autoLink($tweet); + $this->assertSame($expected, $linkedText); + } }