1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\Linguist\Transformers; |
4
|
|
|
|
5
|
|
|
use Ballen\Linguist\Transformers\TransformerInitTrait; |
6
|
|
|
use Ballen\Linguist\Transformers\TransformerInterface; |
7
|
|
|
use Ballen\Linguist\Transformers\Transformer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Linguist |
11
|
|
|
* |
12
|
|
|
* Linguist is a PHP library for parsing strings, it can extract and manipulate |
13
|
|
|
* prefixed words in content ideal for working with @mentions, #topics and |
14
|
|
|
* even custom action tags! |
15
|
|
|
* |
16
|
|
|
* @author Bobby Allen <[email protected]> |
17
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
18
|
|
|
* @link https://github.com/allebb/linguist |
19
|
|
|
* @link http://www.bobbyallen.me |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class HtmlTransformer extends Transformer implements TransformerInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
1 |
|
use TransformerInitTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Transforms the message string. |
29
|
|
|
* @param string $string |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
4 |
|
private function transform($string) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
|
35
|
4 |
|
$string = preg_replace(Transformer::URL_REGEX, "<a href=\"$0\">$0</a> ", $string); |
36
|
|
|
|
37
|
4 |
|
foreach (array_keys($this->tags) as $tagtype) { |
38
|
4 |
|
$tagconf = $this->tags[$tagtype]; |
39
|
4 |
|
$string = preg_replace_callback('/\s+' . $this->tags[$tagtype]['prefix'] . '(\w+)/', function($matches) use ($tagconf) { |
40
|
4 |
|
return ' ' . trim($this->buildHtmlLink($matches[1], trim($matches[0]), $tagconf)); |
41
|
4 |
|
}, $string); |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
$this->formatted = $string; |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Builds the HTML link replacement for the HTML transformation. |
49
|
|
|
* @param string $link The URL |
50
|
|
|
* @param string $text The name of the link |
51
|
|
|
* @param array $tag |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
4 |
|
private function buildHtmlLink($link, $text, $tag) |
55
|
|
|
{ |
56
|
4 |
|
$url = sprintf($tag['url'], $link); |
57
|
4 |
|
$html_link = "<a href = \"{$url}\""; |
58
|
|
|
|
59
|
|
|
// Add the class tag if one is available. |
60
|
4 |
|
if (!is_null($tag['class'])) { |
61
|
2 |
|
$html_link .= " class=\"{$tag['class']}\""; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Add the target="_blank" attribute if the user requires it! |
65
|
4 |
|
if ($tag['target']) { |
66
|
4 |
|
$html_link .= " target=\"_blank\""; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
$html_link .= ">{$text}</a>"; |
70
|
4 |
|
return $html_link; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.