Completed
Push — master ( 30ece4...1ec015 )
by Josh
20:33 queued 13:00
created

Parser::trimUrl()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 2
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 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 32
	public function parse($text, array $matches)
18
	{
19 32
		foreach ($matches as $m)
20
		{
21
			// Linkify the trimmed URL
22 32
			$this->linkifyUrl($m[0][1], $this->trimUrl($m[0][0]));
23 32
		}
24 32
	}
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 32
	protected function linkifyUrl($tagPos, $url)
34
	{
35
		// Ensure that the anchor (scheme/www) is still there
36 32
		if (!preg_match('/^[^:]+:|^www\\./i', $url))
37 32
		{
38 2
			return;
39
		}
40
41
		// Create a zero-width end tag right after the URL
42 30
		$endTag = $this->parser->addEndTag($this->config['tagName'], $tagPos + strlen($url), 0);
43
44
		// If the URL starts with "www." we prepend "http://"
45 30
		if ($url[3] === '.')
46 30
		{
47 3
			$url = 'http://' . $url;
48 3
		}
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 30
		$startTag = $this->parser->addStartTag($this->config['tagName'], $tagPos, 0, 1);
53 30
		$startTag->setAttribute($this->config['attrName'], $url);
54
55
		// Pair the tags together
56 30
		$startTag->pairWith($endTag);
57 30
	}
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 32
	protected function trimUrl($url)
73
	{
74 32
		while (1)
75
		{
76 32
			$url = preg_replace('#(?![-=/)])[\\s!-.:-@[-`{-~\\pP]+$#Du', '', $url);
77 32
			if (substr($url, -1) === ')' && substr_count($url, '(') < substr_count($url, ')'))
78 32
			{
79 2
				$url = substr($url, 0, -1);
80 2
				continue;
81
			}
82 32
			break;
83
		}
84
85 32
		return $url;
86
	}
87
}