| Total Complexity | 14 |
| Total Lines | 118 |
| Duplicated Lines | 0 % |
| Coverage | 97.56% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 | 15 | public function convert(ElementInterface $element) |
|
| 23 | { |
||
| 24 | 15 | $value = $element->getValue(); |
|
| 25 | |||
| 26 | 15 | $markdown = ''; |
|
| 27 | |||
| 28 | 15 | $lines = preg_split('/\r\n|\r|\n/', $value); |
|
| 29 | |||
| 30 | 15 | foreach ($lines as $line) |
|
| 31 | { |
||
| 32 | /* |
||
| 33 | * Some special characters need to be escaped based on the position that they appear |
||
| 34 | * The following function will deal with those special cases. |
||
| 35 | */ |
||
| 36 | |||
| 37 | 15 | $markdown .= $this->escapeSpecialCharacters($line); |
|
| 38 | 15 | $markdown .= "\n"; |
|
| 39 | 5 | } |
|
| 40 | |||
| 41 | 15 | return trim($markdown) !== '' ? "\n" . rtrim(ltrim($markdown)) . "\n" : ''; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the supported HTML tags. |
||
| 46 | * |
||
| 47 | * @return string[] |
||
| 48 | */ |
||
| 49 | 15 | public function getSupportedTags() |
|
| 50 | { |
||
| 51 | 15 | return array('p'); |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $line |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 15 | private function escapeSpecialCharacters($line) |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $line |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 15 | private function escapeFirstCharacters($line) |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $line |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 15 | private function escapeOtherCharacters($line) |
|
| 95 | { |
||
| 96 | 15 | $escapable = array('<!--'); |
|
| 97 | |||
| 98 | 15 | foreach ($escapable as $i) |
|
| 99 | { |
||
| 100 | 15 | if (strpos($line, $i) !== false) |
|
| 101 | 5 | { |
|
| 102 | // Found an escapable character, escaping it |
||
| 103 | 5 | $line = substr_replace($line, '\\', strpos($line, $i), 0); |
|
| 104 | } |
||
| 105 | 5 | } |
|
| 106 | |||
| 107 | 15 | return $line; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $line |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 15 | private function escapeOtherCharactersRegex($line) |
|
| 132 | } |
||
| 133 | } |
||
| 134 |