Skip to content

Commit

Permalink
Merge pull request #29 from nojimage/develop
Browse files Browse the repository at this point in the history
Add usernameIncludeSymbol/symbolTag/textWithSymbol options to Autolink class
  • Loading branch information
nojimage authored Jan 28, 2019
2 parents dd2c6d6 + fd1fb6c commit ddbcbba
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 14 deletions.
148 changes: 134 additions & 14 deletions lib/Twitter/Text/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ 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;

/**
* HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
*
* @var string
* @since 3.0.1
*/
protected $symbolTag = '';

/**
* HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
*
* @var string
* @since 3.0.1
*/
protected $textWithSymbolTag = '';

/**
*
* @var Extractor
Expand Down Expand Up @@ -387,6 +411,75 @@ 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;
}

/**
* @return string
* @since 3.0.1
*/
public function getSymbolTag()
{
return $this->symbolTag;
}

/**
* Set HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
*
* @param string $symbolTag HTML tag without bracket. e.g., 'b' or 's'
* @return Autolink
* @since 3.0.1
*/
public function setSymbolTag($symbolTag)
{
$this->symbolTag = $symbolTag;

return $this;
}

/**
* @return string
* @since 3.0.1
*/
public function getTextWithSymbolTag()
{
return $this->textWithSymbolTag;
}

/**
* Set HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
*
* @param string $textWithSymbolTag HTML tag without bracket. e.g., 'b' or 's'
* @return Autolink
* @since 3.0.1
*/
public function setTextWithSymbolTag($textWithSymbolTag)
{
$this->textWithSymbolTag = $textWithSymbolTag;

return $this;
}

/**
* Autolink with entities
*
Expand All @@ -404,18 +497,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 +740,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 +754,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 +785,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 +801,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 +843,35 @@ 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 (!empty($this->symbolTag)) {
$symbol = sprintf('<%1$s>%2$s</%1$s>', $this->symbolTag, $symbol);
}
if (!empty($this->textWithSymbolTag)) {
$linkText = sprintf('<%1$s>%2$s</%1$s>', $this->textWithSymbolTag, $linkText);
}

if (!$includeSymbol) {
return $symbol . $this->linkToText($entity, $linkText, $attributes);
}

$linkText = $symbol . $linkText;

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

/**
* html escape
*
Expand Down
73 changes: 73 additions & 0 deletions tests/TestCase/AutolinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,45 @@ public function testCreate()
$this->assertInstanceOf('Twitter\\Text\\AutoLink', $linker);
}

/**
* test for accessor / mutator
*/
public function testAccessorMutator()
{
$this->assertSame('', $this->linker->getURLClass());
$this->assertSame('-url', $this->linker->setURLClass('-url')->getURLClass());

$this->assertSame('tweet-url username', $this->linker->getUsernameClass());
$this->assertSame('-username', $this->linker->setUsernameClass('-username')->getUsernameClass());

$this->assertSame('tweet-url list-slug', $this->linker->getListClass());
$this->assertSame('-list', $this->linker->setListClass('-list')->getListClass());

$this->assertSame('tweet-url hashtag', $this->linker->getHashtagClass());
$this->assertSame('-hashtag', $this->linker->setHashtagClass('-hashtag')->getHashtagClass());

$this->assertSame('tweet-url cashtag', $this->linker->getCashtagClass());
$this->assertSame('-cashtag', $this->linker->setCashtagClass('-cashtag')->getCashtagClass());

$this->assertSame('_blank', $this->linker->getTarget());
$this->assertSame('', $this->linker->setTarget(false)->getTarget());

$this->assertSame(true, $this->linker->getExternal());
$this->assertSame(false, $this->linker->setExternal(false)->getExternal());

$this->assertSame(true, $this->linker->getNoFollow());
$this->assertSame(false, $this->linker->setNoFollow(false)->getNoFollow());

$this->assertSame(false, $this->linker->isUsernameIncludeSymbol());
$this->assertSame(true, $this->linker->setUsernameIncludeSymbol(true)->isUsernameIncludeSymbol());

$this->assertSame('', $this->linker->getSymbolTag());
$this->assertSame('i', $this->linker->setSymbolTag('i')->getSymbolTag());

$this->assertSame('', $this->linker->getTextWithSymbolTag());
$this->assertSame('b', $this->linker->setTextWithSymbolTag('b')->getTextWithSymbolTag());
}

public function testAutolinkWithEmoji()
{
$text = '@ummjackson 🤡 https://i.imgur.com/I32CQ81.jpg';
Expand All @@ -52,4 +91,38 @@ 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);
}

public function testSymbolTag()
{
$this->linker
->setExternal(false)
->setTarget(false)
->setNoFollow(false)
->setSymbolTag('s')
->setTextWithSymbolTag('b');

$tweet = '#hash';
$expected = '<a href="https://twitter.com/search?q=%23hash" title="#hash" class="tweet-url hashtag"><s>#</s><b>hash</b></a>';
$this->assertSame($expected, $this->linker->autoLink($tweet));

$tweet = '@mention';
$expected = '<s>@</s><a class="tweet-url username" href="https://twitter.com/mention"><b>mention</b></a>';
$this->assertSame($expected, $this->linker->autoLink($tweet));

$this->linker->setUsernameIncludeSymbol(true);
$expected = '<a class="tweet-url username" href="https://twitter.com/mention"><s>@</s><b>mention</b></a>';
$this->assertSame($expected, $this->linker->autoLink($tweet));
}
}

0 comments on commit ddbcbba

Please sign in to comment.