3 namespace League\HTMLToMarkdown\Converter;
5 use League\HTMLToMarkdown\ElementInterface;
7 class ParagraphConverter implements ConverterInterface
10 * @param ElementInterface $element
14 public function convert(ElementInterface $element)
16 $value = $element->getValue();
20 $lines = preg_split('/\r\n|\r|\n/', $value);
21 foreach ($lines as $line) {
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.
26 $markdown .= $this->escapeSpecialCharacters($line);
30 return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
36 public function getSupportedTags()
46 private function escapeSpecialCharacters($line)
48 $line = $this->escapeFirstCharacters($line);
49 $line = $this->escapeOtherCharacters($line);
50 $line = $this->escapeOtherCharactersRegex($line);
60 private function escapeFirstCharacters($line)
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);
87 private function escapeOtherCharacters($line)
93 foreach ($escapable as $i) {
94 if (strpos($line, $i) !== false) {
95 // Found an escapable character, escaping it
96 $line = substr_replace($line, '\\', strpos($line, $i), 0);
104 * @param string $line
108 private function escapeOtherCharactersRegex($line)
111 // Match numbers ending on ')' or '.' that are at the beginning of the line.
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);