Completed
Push — master ( 8f8e3f...23d7bb )
by Colin
36s
created

racters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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 27
    public function convert(ElementInterface $element)
15
    {
16 27
        $value = $element->getValue();
17
18 27
        $markdown = '';
19
20 27
        $lines = preg_split('/\r\n|\r|\n/', $value);
21 27
        foreach ($lines as $line) {
22
            /*
23
             * Some special characters need to be escaped based on the position that they appear
24
             * The following function will deal with those special cases.
25
             */
26 27
            $markdown .= $this->escapeSpecialCharacters($line);
27 27
            $markdown .= "\n";
28 27
        }
29
30 27
        return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
31
    }
32
33
    /**
34
     * @return string[]
35
     */
36 78
    public function getSupportedTags()
37
    {
38 78
        return array('p');
39
    }
40
41
    /**
42
     * @param string $line
43
     *
44
     * @return string
45
     */
46 27
    private function escapeSpecialCharacters($line)
47
    {
48 27
        $line = $this->escapeFirstCharacters($line);
49 27
        $line = $this->escapeOtherCharacters($line);
50 27
        $line = $this->escapeOtherCharactersRegex($line);
51
52 27
        return $line;
53
    }
54
55
    /**
56
     * @param string $line
57
     *
58
     * @return string
59
     */
60 27
    private function escapeFirstCharacters($line)
61
    {
62
        $escapable = array(
63 27
            '>',
64 27
            '- ',
65 27
            '+ ',
66 27
            '--',
67 27
            '~~~',
68 27
            '---',
69
            '- - -'
70 27
        );
71
72 27
        foreach ($escapable as $i) {
73 27
            if (strpos(ltrim($line), $i) === 0) {
74
                // Found a character that must be escaped, adding a backslash before
75 3
                return '\\' . ltrim($line);
76
            }
77 27
        }
78
79 27
        return $line;
80
    }
81
82
    /**
83
     * @param string $line
84
     *
85
     * @return string
86
     */
87 27
    private function escapeOtherCharacters($line)
88
    {
89
        $escapable = array(
90
            '<!--'
91 27
        );
92
93 27
        foreach ($escapable as $i) {
94 27
            if (strpos($line, $i) !== false) {
95
                // Found an escapable character, escaping it
96 3
                $line = substr_replace($line, '\\', strpos($line, $i), 0);
97 3
            }
98 27
        }
99
100 27
        return $line;
101
    }
102
103
    /**
104
     * @param string $line
105
     *
106
     * @return string
107
     */
108 27
    private function escapeOtherCharactersRegex($line)
109
    {
110
        $regExs = array(
111
            // Match numbers ending on ')' or '.' that are at the beginning of the line.
112
            '/^[0-9]+(?=\)|\.)/'
113 27
        );
114
115 27
        foreach ($regExs as $i) {
116 27
            if (preg_match($i, $line, $match)) {
117
                // Matched an escapable character, adding a backslash on the string before the offending character
118 3
                $line = substr_replace($line, '\\', strlen($match[0]), 0);
119 3
            }
120 27
        }
121
122 27
        return $line;
123
    }
124
}
125