Completed
Branch TemplateInspector (5726eb)
by Josh
09:25
created

Parser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 65
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 8 2
B linkifyUrl() 0 25 3
A trimUrl() 0 4 1
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
}