Skip to content

Commit ddbcbba

Browse files
authored
Merge pull request #29 from nojimage/develop
Add usernameIncludeSymbol/symbolTag/textWithSymbol options to Autolink class
2 parents dd2c6d6 + fd1fb6c commit ddbcbba

File tree

2 files changed

+207
-14
lines changed

2 files changed

+207
-14
lines changed

lib/Twitter/Text/Autolink.php

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ class Autolink
129129
*/
130130
protected $invisibleTagAttrs = "style='position:absolute;left:-9999px;'";
131131

132+
/**
133+
* If the at mark '@' should be included in the link (false by default)
134+
*
135+
* @var bool
136+
* @since 3.0.1
137+
*/
138+
protected $usernameIncludeSymbol = false;
139+
140+
/**
141+
* HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
142+
*
143+
* @var string
144+
* @since 3.0.1
145+
*/
146+
protected $symbolTag = '';
147+
148+
/**
149+
* HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
150+
*
151+
* @var string
152+
* @since 3.0.1
153+
*/
154+
protected $textWithSymbolTag = '';
155+
132156
/**
133157
*
134158
* @var Extractor
@@ -387,6 +411,75 @@ public function setTarget($v)
387411
return $this;
388412
}
389413

414+
/**
415+
* @return bool
416+
* @since 3.0.1
417+
*/
418+
public function isUsernameIncludeSymbol()
419+
{
420+
return $this->usernameIncludeSymbol;
421+
}
422+
423+
/**
424+
* Set if the at mark '@' should be included in the link (false by default)
425+
*
426+
* @param bool $usernameIncludeSymbol if username includes symbol
427+
* @return Autolink
428+
* @since 3.0.1
429+
*/
430+
public function setUsernameIncludeSymbol($usernameIncludeSymbol)
431+
{
432+
$this->usernameIncludeSymbol = $usernameIncludeSymbol;
433+
434+
return $this;
435+
}
436+
437+
/**
438+
* @return string
439+
* @since 3.0.1
440+
*/
441+
public function getSymbolTag()
442+
{
443+
return $this->symbolTag;
444+
}
445+
446+
/**
447+
* Set HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
448+
*
449+
* @param string $symbolTag HTML tag without bracket. e.g., 'b' or 's'
450+
* @return Autolink
451+
* @since 3.0.1
452+
*/
453+
public function setSymbolTag($symbolTag)
454+
{
455+
$this->symbolTag = $symbolTag;
456+
457+
return $this;
458+
}
459+
460+
/**
461+
* @return string
462+
* @since 3.0.1
463+
*/
464+
public function getTextWithSymbolTag()
465+
{
466+
return $this->textWithSymbolTag;
467+
}
468+
469+
/**
470+
* Set HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
471+
*
472+
* @param string $textWithSymbolTag HTML tag without bracket. e.g., 'b' or 's'
473+
* @return Autolink
474+
* @since 3.0.1
475+
*/
476+
public function setTextWithSymbolTag($textWithSymbolTag)
477+
{
478+
$this->textWithSymbolTag = $textWithSymbolTag;
479+
480+
return $this;
481+
}
482+
390483
/**
391484
* Autolink with entities
392485
*
@@ -404,18 +497,14 @@ public function autoLinkEntities($tweet = null, $entities = null)
404497
$text = '';
405498
$beginIndex = 0;
406499
foreach ($entities as $entity) {
407-
if (isset($entity['screen_name'])) {
408-
$text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex + 1);
409-
} else {
410-
$text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
411-
}
500+
$text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
412501

413502
if (isset($entity['url'])) {
414503
$text .= $this->linkToUrl($entity);
415504
} elseif (isset($entity['hashtag'])) {
416505
$text .= $this->linkToHashtag($entity, $tweet);
417506
} elseif (isset($entity['screen_name'])) {
418-
$text .= $this->linkToMentionAndList($entity);
507+
$text .= $this->linkToMentionAndList($entity, $tweet);
419508
} elseif (isset($entity['cashtag'])) {
420509
$text .= $this->linkToCashtag($entity, $tweet);
421510
}
@@ -651,7 +740,7 @@ public function linkToHashtag($entity, $tweet = null)
651740
$attributes = array();
652741
$class = array();
653742
$hash = StringUtils::substr($tweet, $entity['indices'][0], 1);
654-
$linkText = $hash . $entity['hashtag'];
743+
$linkText = $entity['hashtag'];
655744

656745
$attributes['href'] = $this->url_base_hash . $entity['hashtag'];
657746
$attributes['title'] = '#' . $entity['hashtag'];
@@ -665,18 +754,20 @@ public function linkToHashtag($entity, $tweet = null)
665754
$attributes['class'] = implode(' ', $class);
666755
}
667756

668-
return $this->linkToText($entity, $linkText, $attributes);
757+
return $this->linkToTextWithSymbol($entity, $hash, $linkText, $attributes);
669758
}
670759

671760
/**
672761
*
673762
* @param array $entity
763+
* @param string $tweet
674764
* @return string
675765
* @since 1.1.0
676766
*/
677-
public function linkToMentionAndList($entity)
767+
public function linkToMentionAndList($entity, $tweet)
678768
{
679769
$attributes = array();
770+
$symbol = StringUtils::substr($tweet, $entity['indices'][0], 1);
680771

681772
if (!empty($entity['list_slug'])) {
682773
# Replace the list and username
@@ -694,7 +785,7 @@ public function linkToMentionAndList($entity)
694785
}
695786
$attributes['href'] = $url;
696787

697-
return $this->linkToText($entity, $linkText, $attributes);
788+
return $this->linkToTextWithSymbol($entity, $symbol, $linkText, $attributes);
698789
}
699790

700791
/**
@@ -710,15 +801,15 @@ public function linkToCashtag($entity, $tweet = null)
710801
$tweet = $this->tweet;
711802
}
712803
$attributes = array();
713-
$doller = StringUtils::substr($tweet, $entity['indices'][0], 1);
714-
$linkText = $doller . $entity['cashtag'];
804+
$dollar = StringUtils::substr($tweet, $entity['indices'][0], 1);
805+
$linkText = $entity['cashtag'];
715806
$attributes['href'] = $this->url_base_cash . $entity['cashtag'];
716-
$attributes['title'] = $linkText;
807+
$attributes['title'] = '$' . $linkText;
717808
if (!empty($this->class_cash)) {
718809
$attributes['class'] = $this->class_cash;
719810
}
720811

721-
return $this->linkToText($entity, $linkText, $attributes);
812+
return $this->linkToTextWithSymbol($entity, $dollar, $linkText, $attributes);
722813
}
723814

724815
/**
@@ -752,6 +843,35 @@ public function linkToText(array $entity, $text, $attributes = array())
752843
return $link;
753844
}
754845

846+
/**
847+
*
848+
* @param array $entity
849+
* @param string $symbol
850+
* @param string $linkText
851+
* @param array $attributes
852+
* @return string
853+
* @since 3.0.1
854+
*/
855+
protected function linkToTextWithSymbol(array $entity, $symbol, $linkText, array $attributes)
856+
{
857+
$includeSymbol = $this->usernameIncludeSymbol || !preg_match('/[@@]/u', $symbol);
858+
859+
if (!empty($this->symbolTag)) {
860+
$symbol = sprintf('<%1$s>%2$s</%1$s>', $this->symbolTag, $symbol);
861+
}
862+
if (!empty($this->textWithSymbolTag)) {
863+
$linkText = sprintf('<%1$s>%2$s</%1$s>', $this->textWithSymbolTag, $linkText);
864+
}
865+
866+
if (!$includeSymbol) {
867+
return $symbol . $this->linkToText($entity, $linkText, $attributes);
868+
}
869+
870+
$linkText = $symbol . $linkText;
871+
872+
return $this->linkToText($entity, $linkText, $attributes);
873+
}
874+
755875
/**
756876
* html escape
757877
*

tests/TestCase/AutolinkTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,45 @@ public function testCreate()
4141
$this->assertInstanceOf('Twitter\\Text\\AutoLink', $linker);
4242
}
4343

44+
/**
45+
* test for accessor / mutator
46+
*/
47+
public function testAccessorMutator()
48+
{
49+
$this->assertSame('', $this->linker->getURLClass());
50+
$this->assertSame('-url', $this->linker->setURLClass('-url')->getURLClass());
51+
52+
$this->assertSame('tweet-url username', $this->linker->getUsernameClass());
53+
$this->assertSame('-username', $this->linker->setUsernameClass('-username')->getUsernameClass());
54+
55+
$this->assertSame('tweet-url list-slug', $this->linker->getListClass());
56+
$this->assertSame('-list', $this->linker->setListClass('-list')->getListClass());
57+
58+
$this->assertSame('tweet-url hashtag', $this->linker->getHashtagClass());
59+
$this->assertSame('-hashtag', $this->linker->setHashtagClass('-hashtag')->getHashtagClass());
60+
61+
$this->assertSame('tweet-url cashtag', $this->linker->getCashtagClass());
62+
$this->assertSame('-cashtag', $this->linker->setCashtagClass('-cashtag')->getCashtagClass());
63+
64+
$this->assertSame('_blank', $this->linker->getTarget());
65+
$this->assertSame('', $this->linker->setTarget(false)->getTarget());
66+
67+
$this->assertSame(true, $this->linker->getExternal());
68+
$this->assertSame(false, $this->linker->setExternal(false)->getExternal());
69+
70+
$this->assertSame(true, $this->linker->getNoFollow());
71+
$this->assertSame(false, $this->linker->setNoFollow(false)->getNoFollow());
72+
73+
$this->assertSame(false, $this->linker->isUsernameIncludeSymbol());
74+
$this->assertSame(true, $this->linker->setUsernameIncludeSymbol(true)->isUsernameIncludeSymbol());
75+
76+
$this->assertSame('', $this->linker->getSymbolTag());
77+
$this->assertSame('i', $this->linker->setSymbolTag('i')->getSymbolTag());
78+
79+
$this->assertSame('', $this->linker->getTextWithSymbolTag());
80+
$this->assertSame('b', $this->linker->setTextWithSymbolTag('b')->getTextWithSymbolTag());
81+
}
82+
4483
public function testAutolinkWithEmoji()
4584
{
4685
$text = '@ummjackson 🤡 https://i.imgur.com/I32CQ81.jpg';
@@ -52,4 +91,38 @@ public function testAutolinkWithEmoji()
5291

5392
$this->assertSame($expected, $linkedText);
5493
}
94+
95+
public function testUsernameIncludeSymbol()
96+
{
97+
$tweet = 'Testing @mention and @mention/list';
98+
// @codingStandardsIgnoreStart
99+
$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>';
100+
// @codingStandardsIgnoreEnd
101+
102+
$this->linker->setUsernameIncludeSymbol(true);
103+
$linkedText = $this->linker->autoLink($tweet);
104+
$this->assertSame($expected, $linkedText);
105+
}
106+
107+
public function testSymbolTag()
108+
{
109+
$this->linker
110+
->setExternal(false)
111+
->setTarget(false)
112+
->setNoFollow(false)
113+
->setSymbolTag('s')
114+
->setTextWithSymbolTag('b');
115+
116+
$tweet = '#hash';
117+
$expected = '<a href="https://twitter.com/search?q=%23hash" title="#hash" class="tweet-url hashtag"><s>#</s><b>hash</b></a>';
118+
$this->assertSame($expected, $this->linker->autoLink($tweet));
119+
120+
$tweet = '@mention';
121+
$expected = '<s>@</s><a class="tweet-url username" href="https://twitter.com/mention"><b>mention</b></a>';
122+
$this->assertSame($expected, $this->linker->autoLink($tweet));
123+
124+
$this->linker->setUsernameIncludeSymbol(true);
125+
$expected = '<a class="tweet-url username" href="https://twitter.com/mention"><s>@</s><b>mention</b></a>';
126+
$this->assertSame($expected, $this->linker->autoLink($tweet));
127+
}
55128
}

0 commit comments

Comments
 (0)