Completed
Push — master ( f21c84...d19842 )
by Colin
03:11
created

TextConverter::convert()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.8817
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\ElementInterface;
6
7
class TextConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 66
    public function convert(ElementInterface $element)
15
    {
16 66
        $markdown = $element->getValue();
17
18
        // Remove leftover \n at the beginning of the line
19 66
        $markdown = ltrim($markdown, "\n");
20
21
        // Replace sequences of invisible characters with spaces
22 66
        $markdown = preg_replace('~\s+~u', ' ', $markdown);
23
24
        // Escape the following characters: '*', '_', '[', ']' and '\'
25 66
        if ($element->getParent() && $element->getParent()->getTagName() !== 'div') {
26 66
            $markdown = preg_replace('~([*_\\[\\]\\\\])~u', '\\\\$1', $markdown);
27 44
        }
28
29 66
        $markdown = preg_replace('~^#~u', '\\\\#', $markdown);
30
31 66
        if ($markdown === ' ') {
32 9
            $next = $element->getNext();
33 9
            if (!$next || $next->isBlock()) {
34 3
                $markdown = '';
35 2
            }
36 6
        }
37
38 66
        return $markdown;
39
    }
40
41
    /**
42
     * @return string[]
43
     */
44 84
    public function getSupportedTags()
45
    {
46 84
        return array('#text');
47
    }
48
}
49