HeadlineTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 58
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
parseInline() 0 1 ?
renderAbsy() 0 1 ?
A identifyHeadline() 0 12 6
B consumeHeadline() 0 24 6
A renderHeadline() 0 5 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2014 Carsten Brandt
4
 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5
 * @link https://github.com/cebe/markdown#readme
6
 */
7
8
namespace cebe\markdown\block;
9
10
/**
11
 * Adds the headline blocks
12
 */
13
trait HeadlineTrait
14
{
15
	/**
16
	 * identify a line as a headline
17
	 */
18 205
	protected function identifyHeadline($line, $lines, $current)
19
	{
20
		return (
21
			// heading with #
22 205
			$line[0] === '#' && !preg_match('/^#\d+/', $line)
23
			||
24
			// underlined headline
25 205
			!empty($lines[$current + 1]) &&
26 205
			(($l = $lines[$current + 1][0]) === '=' || $l === '-') &&
27 205
			preg_match('/^(\-+|=+)\s*$/', $lines[$current + 1])
28
		);
29
	}
30
31
	/**
32
	 * Consume lines for a headline
33
	 */
34 36
	protected function consumeHeadline($lines, $current)
35
	{
36 36
		if ($lines[$current][0] === '#') {
37
			// ATX headline
38 21
			$level = 1;
39 21
			while (isset($lines[$current][$level]) && $lines[$current][$level] === '#' && $level < 6) {
40 20
				$level++;
41
			}
42
			$block = [
43 21
				'headline',
44 21
				'content' => $this->parseInline(trim($lines[$current], "# \t")),
45 21
				'level' => $level,
46
			];
47 21
			return [$block, $current];
48
		} else {
49
			// underlined headline
50
			$block = [
51 25
				'headline',
52 25
				'content' => $this->parseInline($lines[$current]),
53 25
				'level' => $lines[$current + 1][0] === '=' ? 1 : 2,
54
			];
55 25
			return [$block, $current + 1];
56
		}
57
	}
58
59
	/**
60
	 * Renders a headline
61
	 */
62 24
	protected function renderHeadline($block)
63
	{
64 24
		$tag = 'h' . $block['level'];
65 24
		return "<$tag>" . $this->renderAbsy($block['content']) . "</$tag>\n";
66
	}
67
68
	abstract protected function parseInline($text);
69
	abstract protected function renderAbsy($absy);
70
}
71