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

ParagraphConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 42
ccs 12
cts 14
cp 0.8571
rs 10

2 Methods

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