HeadlineTrait::identifyHeadline()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2015 Nobuo Kihara
4
 * @license https://github.com/softark/creole/blob/master/LICENSE
5
 * @link https://github.com/softark/creole#readme
6
 */
7
8
namespace softark\creole\block;
9
10
/**
11
 * Adds the headline blocks
12
 */
13
trait HeadlineTrait
14
{
15
    /**
16
     * identify a line as a headline
17
     * A headline always starts with a '=', with leading white spaces permitted.
18
     */
19 23
    protected function identifyHeadline($line)
20
    {
21 23
        $line = ltrim($line);
22
        return (
23
            // heading with =
24 23
            isset($line[0]) && $line[0] === '='
25
        );
26
    }
27
28
    /**
29
     * Consume lines for a headline
30
     */
31 3
    protected function consumeHeadline($lines, $current)
32
    {
33 3
        $line = trim($lines[$current]);
34 3
        $level = 1;
35 3
        while (isset($line[$level]) && $line[$level] === '=' && $level < 6) {
36 3
            $level++;
37
        }
38
        $block = [
39 3
            'headline',
40
            // parse headline content. The leading and trailing '='s are removed.
41 3
            'content' => $this->parseInline(trim($line, " \t=")),
42 3
            'level' => $level,
43
        ];
44 3
        return [$block, $current];
45
    }
46
47
    /**
48
     * Renders a headline
49
     */
50 3
    protected function renderHeadline($block)
51
    {
52 3
        $tag = 'h' . $block['level'];
53 3
        return "<$tag>" . $this->renderAbsy($block['content']) . "</$tag>\n";
54
    }
55
56
    abstract protected function parseInline($text);
57
58
    abstract protected function renderAbsy($absy);
59
}
60