Completed
Branch wip/litedown (039d9d)
by Josh
29:41
created

LinkReferences::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 0
crap 4
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\Litedown\Parser\Passes;
9
10
class LinkReferences extends AbstractPass
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 263
	public function parse()
16
	{
17 263
		if (strpos($this->text, ']:') === false)
18 263
		{
19 237
			return;
20
		}
21
22 26
		$regexp = '/^\\x1A* {0,3}\\[([^\\x17\\]]+)\\]: *([^\\s\\x17]+ *(?:"[^\\x17]*?"|\'[^\\x17]*?\'|\\([^\\x17)]*\\))?)[^\\x17\\n]*\\n?/m';
23 26
		preg_match_all($regexp, $this->text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
24 26
		foreach ($matches as $m)
25
		{
26 26
			$this->parser->addIgnoreTag($m[0][1], strlen($m[0][0]), -2);
27
28
			// Ignore the reference if it already exists
29 26
			$id = strtolower($m[1][0]);
30 26
			if (!isset($this->text->linkReferences[$id]))
31 26
			{
32 26
				$this->text->linkReferences[$id] = $m[2][0];
33 26
			}
34 26
		}
35
	}
36
}