Completed
Branch wip/litedown (e234a3)
by Josh
31:46 queued 18:30
created

InlineCode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 87
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 27 6
A addInlineCodeTags() 0 9 1
B getInlineCodeMarkers() 0 31 3
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 InlineCode extends AbstractPass
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 263
	public function parse()
16
	{
17 263
		$markers = $this->getInlineCodeMarkers();
18 263
		$i       = -1;
19 263
		$cnt     = count($markers);
20 263
		while (++$i < ($cnt - 1))
21
		{
22 24
			$pos = $markers[$i]['next'];
23 24
			$j   = $i;
24 24
			if ($this->text->charAt($markers[$i]['pos']) !== '`')
25 24
			{
26
				// Adjust the left marker if its first backtick was escaped
27 1
				++$markers[$i]['pos'];
28 1
				--$markers[$i]['len'];
29 1
			}
30 24
			while (++$j < $cnt && $markers[$j]['pos'] === $pos)
31
			{
32 23
				if ($markers[$j]['len'] === $markers[$i]['len'])
33 23
				{
34 21
					$this->addInlineCodeTags($markers[$i], $markers[$j]);
35 21
					$i = $j;
36 21
					break;
37
				}
38 10
				$pos = $markers[$j]['next'];
39 10
			}
40 24
		}
41 263
	}
42
43
	/**
44
	* Add the tag pair for an inline code span
45
	*
46
	* @param  array $left  Left marker
47
	* @param  array $right Right marker
48
	* @return void
49
	*/
50 21
	protected function addInlineCodeTags($left, $right)
51
	{
52 21
		$startPos = $left['pos'];
53 21
		$startLen = $left['len'] + $left['trimAfter'];
54 21
		$endPos   = $right['pos'] - $right['trimBefore'];
55 21
		$endLen   = $right['len'] + $right['trimBefore'];
56 21
		$this->parser->addTagPair('C', $startPos, $startLen, $endPos, $endLen);
57 21
		$this->text->overwrite($startPos, $endPos + $endLen - $startPos);
58 21
	}
59
60
	/**
61
	* Capture and return inline code markers
62
	*
63
	* @return array
64
	*/
65 263
	protected function getInlineCodeMarkers()
66
	{
67 263
		$pos = $this->text->indexOf('`');
68 263
		if ($pos === false)
69 263
		{
70 239
			return [];
71
		}
72
73 24
		preg_match_all(
74 24
			'/(`+)(\\s*)[^\\x17`]*/',
75 24
			str_replace("\x1BB", '\\`', $this->text),
76 24
			$matches,
77 24
			PREG_OFFSET_CAPTURE | PREG_SET_ORDER,
78
			$pos
79 24
		);
80 24
		$trimNext = 0;
81 24
		$markers  = [];
82 24
		foreach ($matches as $m)
83
		{
84 24
			$markers[] = [
85 24
				'pos'        => $m[0][1],
86 24
				'len'        => strlen($m[1][0]),
87 24
				'trimBefore' => $trimNext,
88 24
				'trimAfter'  => strlen($m[2][0]),
89 24
				'next'       => $m[0][1] + strlen($m[0][0])
90 24
			];
91 24
			$trimNext = strlen($m[0][0]) - strlen(rtrim($m[0][0]));
92 24
		}
93
94 24
		return $markers;
95
	}
96
}