Passed
Push — master ( bf4593...1c3000 )
by Rougin
04:34
created

ParagraphConverter::escapeOtherCharacters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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