Completed
Pull Request — master (#102)
by
unknown
03:16
created

ParagraphConverter::convert()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 10
cts 12
cp 0.8333
rs 8.5806
cc 4
eloc 11
nc 6
nop 1
crap 4.074
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 21
    public function convert(ElementInterface $element)
15
    {
16 21
        $value = $element->getValue();
17
18 21
        $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
         * if the first character (sans blank space) is a >
25
         */
26
27 21
        $lines = explode("\n", $value);
28 21
        foreach ($lines as $line) {
29 21
            if (strpos(ltrim($line), '>') === 0) {
30
                // Found a > char, escaping it
31
                $markdown .= '\\' . ltrim($line);
32
            } else {
33 21
                $markdown .= $line;
34
            }
35 21
            $markdown .= "\n";
36 21
        }
37
38 21
        return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
39
    }
40
41
    /**
42
     * @return string[]
43
     */
44 78
    public function getSupportedTags()
45
    {
46 78
        return array('p');
47
    }
48
}
49