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

TextConverter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 42
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B convert() 0 26 6
A getSupportedTags() 0 4 1
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