]> git.mxchange.org Git - friendica.git/blob - vendor/league/html-to-markdown/src/Converter/ParagraphConverter.php
Move HTML to Markdown library to Composer
[friendica.git] / vendor / league / html-to-markdown / src / Converter / ParagraphConverter.php
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     public function convert(ElementInterface $element)
15     {
16         $value = $element->getValue();
17
18         $markdown = '';
19
20         $lines = preg_split('/\r\n|\r|\n/', $value);
21         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             $markdown .= $this->escapeSpecialCharacters($line);
27             $markdown .= "\n";
28         }
29
30         return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
31     }
32
33     /**
34      * @return string[]
35      */
36     public function getSupportedTags()
37     {
38         return array('p');
39     }
40
41     /**
42      * @param string $line
43      *
44      * @return string
45      */
46     private function escapeSpecialCharacters($line)
47     {
48         $line = $this->escapeFirstCharacters($line);
49         $line = $this->escapeOtherCharacters($line);
50         $line = $this->escapeOtherCharactersRegex($line);
51
52         return $line;
53     }
54
55     /**
56      * @param string $line
57      *
58      * @return string
59      */
60     private function escapeFirstCharacters($line)
61     {
62         $escapable = array(
63             '>',
64             '- ',
65             '+ ',
66             '--',
67             '~~~',
68             '---',
69             '- - -'
70         );
71
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);
76             }
77         }
78
79         return $line;
80     }
81
82     /**
83      * @param string $line
84      *
85      * @return string
86      */
87     private function escapeOtherCharacters($line)
88     {
89         $escapable = array(
90             '<!--'
91         );
92
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);
97             }
98         }
99
100         return $line;
101     }
102
103     /**
104      * @param string $line
105      *
106      * @return string
107      */
108     private function escapeOtherCharactersRegex($line)
109     {
110         $regExs = array(
111             // Match numbers ending on ')' or '.' that are at the beginning of the line.
112             '/^[0-9]+(?=\)|\.)/'
113         );
114
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);
119             }
120         }
121
122         return $line;
123     }
124 }