Completed
Pull Request — master (#108)
by
unknown
02:49
created

escapeBlockquotelikeCharacters()   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->escapeHeaderlikeCharacters($line);
49 27
        $line = $this->escapeBlockquotelikeCharacters($line);
50 27
        $line = $this->escapeOrderedListlikeCharacters($line);
51 27
        $line = $this->escapeListlikeCharacters($line);
52 27
        $line = $this->escapeTaglikeCharacters($line);
53
54 27
        return $line;
55
    }
56
57
    /**
58
     * @param string $line
59
     *
60
     * @return string
61
     */
62 27 View Code Duplication
    private function escapeBlockquotelikeCharacters($line)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64 27
        if (strpos(ltrim($line), '>') === 0) {
65
            // Found a > char, escaping it
66 3
            return '\\' . ltrim($line);
67
        } else {
68 27
            return $line;
69
        }
70
    }
71
72
    /**
73
     * @param string $line
74
     *
75
     * @return string
76
     */
77 27 View Code Duplication
    private function escapeHeaderlikeCharacters($line)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79 27
        if (strpos(ltrim($line), '--') === 0) {
80
            // Found a -- structure, escaping it
81 3
            return '\\' . ltrim($line);
82
        } else {
83 27
            return $line;
84
        }
85
    }
86
87
    /**
88
     * @param string $line
89
     *
90
     * @return string
91
     */
92 27
    private function escapeOrderedListlikeCharacters($line)
93
    {
94
        // This regex will match numbers ending on ')' or '.' that are at the beginning of the line.
95 27
        if (preg_match('/^[0-9]+(?=\)|\.)/', $line, $match)) {
96
            // Found an Ordered list like character, escaping it
97 3
            return substr_replace($line, '\\', strlen($match[0]), 0);
98
        } else {
99 27
            return $line;
100
        }
101
    }
102
103
    /**
104
     * @param string $line
105
     *
106
     * @return string
107
     */
108 27
    private function escapeListlikeCharacters($line)
109
    {
110 27
        if (strpos(ltrim($line), '- ') === 0 || strpos(ltrim($line), '+ ') === 0) {
111
            // Found an list like character, escaping it
112 3
            return '\\' . ltrim($line);
113
        } else {
114 27
            return $line;
115
        }
116
    }
117
118
    /**
119
     * @param string $line
120
     *
121
     * @return string
122
     */
123 27
    private function escapeTaglikeCharacters($line)
124
    {
125 27
        if (strpos($line, '<!--') !== false) {
126
            // Found an tag like character, escaping it
127 3
            return substr_replace($line, '\\', strpos($line, '<!--'), 0);
128
        } else {
129 27
            return $line;
130
        }
131
    }
132
}
133