Completed
Pull Request — master (#100)
by
unknown
21:51 queued 19:53
created

TextConverter::convert()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 20
ccs 7
cts 12
cp 0.5833
rs 9.2
c 2
b 0
f 2
cc 4
eloc 10
nc 3
nop 1
crap 5.1576
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 48
    public function convert(ElementInterface $element)
15
    {
16 48
        $value = $element->getValue();
17
18 48
        $markdown = preg_replace('~\s+~u', ' ', $value);
19
20
        //escape the following characters: '*', '_' and '\'
21 48
        $markdown = preg_replace('~([*_\\\\])~u', '\\\\$1', $markdown);
22
23 48
        $markdown = preg_replace('~^#~u', '\\\\#', $markdown);
24
25 48
        if ($markdown === ' ') {
26
            $next = $element->getNext();
27
            if (!$next || $next->isBlock()) {
28
                $markdown = '';
29
            }
30
        }
31
32 48
        return $markdown;
33
    }
34
35
    /**
36
     * @return string[]
37
     */
38 78
    public function getSupportedTags()
39
    {
40 78
        return array('#text');
41
    }
42
}
43