Skip to content

Commit

Permalink
refs #28 Add usernameIncludeSymbol option to Autolink class
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Jan 28, 2019
1 parent dd2c6d6 commit 41710de
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
79 changes: 65 additions & 14 deletions lib/Twitter/Text/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -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'];
Expand All @@ -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
Expand All @@ -694,7 +723,7 @@ public function linkToMentionAndList($entity)
}
$attributes['href'] = $url;

return $this->linkToText($entity, $linkText, $attributes);
return $this->linkToTextWithSymbol($entity, $symbol, $linkText, $attributes);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/AutolinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ public function testAutolinkWithEmoji()

$this->assertSame($expected, $linkedText);
}

public function testUsernameIncludeSymbol()
{
$tweet = 'Testing @mention and @mention/list';
// @codingStandardsIgnoreStart
$expected = 'Testing <a class="tweet-url username" href="https://twitter.com/mention" rel="external nofollow" target="_blank">@mention</a> and <a class="tweet-url list-slug" href="https://twitter.com/mention/list" rel="external nofollow" target="_blank">@mention/list</a>';
// @codingStandardsIgnoreEnd

$this->linker->setUsernameIncludeSymbol(true);
$linkedText = $this->linker->autoLink($tweet);
$this->assertSame($expected, $linkedText);
}
}

0 comments on commit 41710de

Please sign in to comment.