1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2017 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Plugins\Autolink; |
9
|
|
|
|
10
|
|
|
use s9e\TextFormatter\Plugins\ParserBase; |
11
|
|
|
|
12
|
|
|
class Parser extends ParserBase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
public function parse($text, array $matches) |
18
|
|
|
{ |
19
|
|
|
foreach ($matches as $m) |
20
|
|
|
{ |
21
|
|
|
// Linkify the trimmed URL |
22
|
|
|
$this->linkifyUrl($m[0][1], $this->trimUrl($m[0][0])); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Linkify given URL at given position |
28
|
|
|
* |
29
|
|
|
* @param integer $tagPos URL's position in the text |
30
|
|
|
* @param string $url URL |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
protected function linkifyUrl($tagPos, $url) |
34
|
|
|
{ |
35
|
|
|
// Ensure that the anchor (scheme/www) is still there |
36
|
|
|
if (!preg_match('/^[^:]+:|^www\\./i', $url)) |
37
|
|
|
{ |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Create a zero-width end tag right after the URL |
42
|
|
|
$endTag = $this->parser->addEndTag($this->config['tagName'], $tagPos + strlen($url), 0); |
43
|
|
|
|
44
|
|
|
// If the URL starts with "www." we prepend "http://" |
45
|
|
|
if ($url[3] === '.') |
46
|
|
|
{ |
47
|
|
|
$url = 'http://' . $url; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Create a zero-width start tag right before the URL, with a slightly worse priority to |
51
|
|
|
// allow specialized plugins to use the URL instead |
52
|
|
|
$startTag = $this->parser->addStartTag($this->config['tagName'], $tagPos, 0, 1); |
53
|
|
|
$startTag->setAttribute($this->config['attrName'], $url); |
54
|
|
|
|
55
|
|
|
// Pair the tags together |
56
|
|
|
$startTag->pairWith($endTag); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Remove trailing punctuation from given URL |
61
|
|
|
* |
62
|
|
|
* We remove most ASCII non-letters and Unicode punctuation from the end of the string. |
63
|
|
|
* Exceptions: |
64
|
|
|
* - dashes (some YouTube URLs end with a dash due to the video ID) |
65
|
|
|
* - equal signs (because of "foo?bar="), |
66
|
|
|
* - trailing slashes, |
67
|
|
|
* - closing parentheses are balanced separately. |
68
|
|
|
* |
69
|
|
|
* @param string $url Original URL |
70
|
|
|
* @return string Trimmed URL |
71
|
|
|
*/ |
72
|
|
|
protected function trimUrl($url) |
73
|
|
|
{ |
74
|
|
|
return preg_replace('#(?![-=/)])[\\s!-.:-@[-`{-~\\pP]+$#Du', '', $url); |
75
|
|
|
} |
76
|
|
|
} |