Completed
Push — master ( 8dfe3b...f03e1d )
by Colin
10s
created

ParagraphConverter::convert()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 5
cts 5
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 11
nc 6
nop 1
crap 4
1
<?php
2
3
namespace League\HTMLToMarkdown\Converter;
4
5
use League\HTMLToMarkdown\ElementInterface;
6
7
class ParagraphConverter implements ConverterInterface
8
{
9
    /**
10
     * @param ElementInterface $element
11
     *
12
     * @return string
13
     */
14 24
    public function convert(ElementInterface $element)
15
    {
16 24
        $value = $element->getValue();
17
18 24
        $markdown = '';
19
20
        /*
21
         * &gt; ocurrences must be escaped, otherwise instead of rendering p tags as paragraph blocks,
22
         * the > will make them appear as a blockquote.
23
         * To achieve this, the content of the paragraph must be exploded and then each line must be check
24 78
         * if the first character (sans blank space) is a >
25
         */
26 78
27
        $lines = explode("\n", $value);
28
        foreach ($lines as $line) {
29
            if (strpos(ltrim($line), '>') === 0) {
30
                // Found a > char, escaping it
31
                $markdown .= '\\' . ltrim($line);
32
            } else {
33
                $markdown .= $line;
34
            }
35
            $markdown .= "\n";
36
        }
37
38
        return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
39
    }
40
41
    /**
42
     * @return string[]
43
     */
44
    public function getSupportedTags()
45
    {
46
        return array('p');
47
    }
48
}
49