1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2020 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
|
38 |
|
public function parse($text, array $matches) |
18
|
|
|
{ |
19
|
38 |
|
foreach ($matches as $m) |
20
|
|
|
{ |
21
|
|
|
// Linkify the trimmed URL |
22
|
38 |
|
$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
|
38 |
|
protected function linkifyUrl($tagPos, $url) |
34
|
|
|
{ |
35
|
|
|
// Create a zero-width end tag right after the URL |
36
|
38 |
|
$endPos = $tagPos + strlen($url); |
37
|
38 |
|
$endTag = $this->parser->addEndTag($this->config['tagName'], $endPos, 0); |
38
|
|
|
|
39
|
|
|
// If the URL starts with "www." we prepend "http://" |
40
|
38 |
|
if ($url[3] === '.') |
41
|
|
|
{ |
42
|
4 |
|
$url = 'http://' . $url; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Create a zero-width start tag right before the URL, with a slightly worse priority to |
46
|
|
|
// allow specialized plugins to use the URL instead |
47
|
38 |
|
$startTag = $this->parser->addStartTag($this->config['tagName'], $tagPos, 0, 1); |
48
|
38 |
|
$startTag->setAttribute($this->config['attrName'], $url); |
49
|
|
|
|
50
|
|
|
// Pair the tags together |
51
|
38 |
|
$startTag->pairWith($endTag); |
52
|
|
|
|
53
|
|
|
// Protect the tag's content from partial replacements with a low priority tag |
54
|
38 |
|
$contentTag = $this->parser->addVerbatim($tagPos, $endPos - $tagPos, 1000); |
55
|
38 |
|
$startTag->cascadeInvalidationTo($contentTag); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Remove trailing punctuation from given URL |
60
|
|
|
* |
61
|
|
|
* We remove most ASCII non-letters and Unicode punctuation from the end of the string. |
62
|
|
|
* Exceptions: |
63
|
|
|
* - dashes and underscores, (base64 IDs could end with one) |
64
|
|
|
* - equal signs, (because of "foo?bar=") |
65
|
|
|
* - trailing slashes, |
66
|
|
|
* - closing parentheses. (they are balanced separately) |
67
|
|
|
* |
68
|
|
|
* @param string $url Original URL |
69
|
|
|
* @return string Trimmed URL |
70
|
|
|
*/ |
71
|
38 |
|
protected function trimUrl($url) |
72
|
|
|
{ |
73
|
38 |
|
return preg_replace('#(?:(?![-=)/_])[\\s!-.:-@[-`{-~\\pP])+$#Du', '', $url); |
74
|
|
|
} |
75
|
|
|
} |