Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to add HTML links to twitter handles #17

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added .phpintel/052b22ff0e04838a4cab006c50e58961
Binary file not shown.
Binary file added .phpintel/0c23ba91e4716b6f699f904ac8e649cc
Binary file not shown.
Binary file added .phpintel/index
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/Misd/.DS_Store
Binary file not shown.
55 changes: 47 additions & 8 deletions src/Misd/Linkify/Linkify.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Converts URLs and/or email addresses into HTML links.
*
* @author Chris Wilkinson <[email protected]>
*/
class Linkify implements LinkifyInterface
{
Expand All @@ -38,25 +40,29 @@ public function __construct(array $options = array())
*/
public function process($text, array $options = array())
{
return $this->linkify($text, true, true, $options);
return $this->linkify($text, true, false, true, $options);
}

/**
* {@inheritdoc}
*/
public function processUrls($text, array $options = array())
{
return $this->linkify($text, true, false, $options);
return $this->linkify($text, true, false, false, $options);
}

/**
* {@inheritdoc}
*/
public function processEmails($text, array $options = array())
{
return $this->linkify($text, false, true, $options);
}
return $this->linkify($text, false, true, false, $options);
}

public function processTwitterHandles($text, array $options = array())
{
return $this->linkify($text, false, false, true, $options);
}
/**
* Add links to text.
*
Expand All @@ -67,9 +73,9 @@ public function processEmails($text, array $options = array())
*
* @return string Linkified text.
*/
protected function linkify($text, $urls = true, $emails = true, array $options = array())
protected function linkify($text, $urls = true, $emails = true, $twitter = true, array $options = array())
{
if (false === $urls && false === $emails) {
if (false === $urls && false === $emails && false === $twitter) {
// nothing to do...
return $text;
}
Expand Down Expand Up @@ -105,6 +111,9 @@ protected function linkify($text, $urls = true, $emails = true, array $options =
if (true === $emails) {
$chunks[$i] = $this->linkifyEmails($chunks[$i], $options);
}
if (true === $twitter) {
$chunks[$i] = $this->linkifyTwitter($chunks[$i], $options);
}
}
} else { // odd numbers are tags
// Only process this tag if there are no unclosed $ignoreTags
Expand Down Expand Up @@ -174,7 +183,7 @@ protected function linkifyUrls($text, $options = array('attr' => ''))
}
}

return '<a href="' . $match[0] . '"' . $options['attr'] . '>' . $caption . '</a>';
return '<a class="linkify" href="' . $match[0] . '"' . $options['attr'] . '"rel="nofollow" target="_blank"' . '>' . $caption . '</a>';
};

return preg_replace_callback($pattern, $callback, $text);
Expand Down Expand Up @@ -208,9 +217,39 @@ protected function linkifyEmails($text, $options = array('attr' => ''))
}
}

return '<a href="mailto:' . $match[0] . '"' . $options['attr'] . '>' . $match[0] . '</a>';
return '<a class="linkify" href="mailto:' . $match[0] . '"' . $options['attr'] . '>' . $match[0] . '</a>';
};

return preg_replace_callback($pattern, $callback, $text);
}

/**
* Add HTML links to Twitter Handles in plain text.
*
* @param string $text Text to linkify.
* @param array $options Options, 'attr' key being the attributes to add to the links, with a preceding space.
*
* @return string Linkified text.
*/

protected function linkifyTwitter($text, $options = array('attr' => ''))
{
$pattern = '/\B@[^\b]([^.\s]+)/';
//$pattern = '/@([\w]+)([[^\s])/i';
///$pattern = '/@([\w]+)([^\s]+)/i';

$callback = function ($match) use ($options) {
if (isset($options['callback'])) {
$cb = $options['callback']($match[0], $match[0], true);
if (!is_null($cb)) {
return $cb;
}
}

return '<a class="linkify" href="https://plugmatch.com/' . substr($match[0],1) . '">'. $match[0] .'</a>';
};

return preg_replace_callback($pattern, $callback, $text);
}

}
12 changes: 12 additions & 0 deletions src/Misd/Linkify/LinkifyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Converts URLs and/or email addresses into HTML links.
*
* @author Chris Wilkinson <[email protected]>
*/
interface LinkifyInterface
{
Expand Down Expand Up @@ -45,4 +47,14 @@ public function processUrls($text, array $options = array());
* @return string Processed text.
*/
public function processEmails($text, array $options = array());

/**
* Add HTML links to @Twitter Handles
*
* @param string $text Text to process.
* @param array $options Options.
*
* @return string Processed text.
*/
public function processTwitterHandles($text, array $options = array());
}