Completed
Push — master ( a1ddbf...2742fe )
by Colin
05:07
created

ParagraphConverter::escapeOtherCharacters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

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