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
|
|
|
$line = $this->escapeOrderedListlikeCharacters($line); |
51
|
27 |
|
|
52
|
|
|
return $line; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $line |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
27 |
|
*/ |
60
|
|
View Code Duplication |
private function escapeBlockquotelikeCharacters($line) |
|
|
|
|
61
|
27 |
|
{ |
62
|
|
|
if (strpos(ltrim($line), '>') === 0) { |
63
|
3 |
|
// Found a > char, escaping it |
64
|
|
|
return '\\' . ltrim($line); |
65
|
27 |
|
} else { |
66
|
|
|
return $line; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $line |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
27 |
|
*/ |
75
|
|
View Code Duplication |
private function escapeHeaderlikeCharacters($line) |
|
|
|
|
76
|
27 |
|
{ |
77
|
|
|
if (strpos(ltrim($line), '--') === 0) { |
78
|
3 |
|
// Found a -- structure, escaping it |
79
|
|
|
return '\\' . ltrim($line); |
80
|
27 |
|
} else { |
81
|
|
|
return $line; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $line |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
private function escapeOrderedListlikeCharacters($line) |
91
|
|
|
{ |
92
|
|
|
// This regex will match numbers ending on ')' or '.' that are at the beginning of the line. |
93
|
|
|
if (preg_match('/^[0-9]+(?=\)|\.)/', $line, $match)) { |
94
|
|
|
// Found an Ordered list like character, escaping it |
95
|
|
|
return substr_replace($line, '\\', strlen($match[0]), 0); |
96
|
|
|
} else { |
97
|
|
|
return $line; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.