Completed
Pull Request — master (#109)
by
unknown
21:52
created

ParagraphConverter::escapeOtherCharactersRegex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 3
cts 3
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
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 27
52
        return $line;
53 27
    }
54
55
    /**
56
     * @param string $line
57
     *
58
     * @return string
59
     */
60
    private function escapeFirstCharacters($line)
61 27
    {
62
        $escapable = [
63 27
            '>',
64
            '- ',
65 3
            '+ ',
66
            '--',
67 27
            '~~~',
68
            '---',
69
            '- - -'
70
        ];
71
72
        foreach ($escapable as $i) {
73
            if (strpos(ltrim($line), $i) === 0) {
74
                // Found a character that must be escaped, adding a backslash before
75
                return '\\' . ltrim($line);
76 27
            }
77
        }
78 27
79
        return $line;
80 3
    }
81
82 27
    /**
83
     * @param string $line
84
     *
85
     * @return string
86
     */
87
    private function escapeOtherCharacters($line)
88
    {
89
        $escapable = [
90
            '<!--'
91 27
        ];
92
93
        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
            }
98 27
        }
99
100
        return $line;
101
    }
102
103
    /**
104
     * @param string $line
105
     *
106
     * @return string
107 27
     */
108
    private function escapeOtherCharactersRegex($line)
109 27
    {
110
        $regExs = [
111 3
            // Match numbers ending on ')' or '.' that are at the beginning of the line.
112
            '/^[0-9]+(?=\)|\.)/'
113 27
        ];
114
115
        foreach ($regExs as $i) {
116
            if (preg_match($i, $line, $match)) {
117
                // Matched an escapable character, adding a backslash on the string before the offending character
118
                $line = substr_replace($line, '\\', strlen($match[0]), 0);
119
            }
120
        }
121
122
        return $line;
123
    }
124
}
125