aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/league/html-to-markdown/src/Converter/ParagraphConverter.php
blob: 7207b81a6bf0633996013954441f851493473df3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

namespace League\HTMLToMarkdown\Converter;

use League\HTMLToMarkdown\ElementInterface;

class ParagraphConverter implements ConverterInterface
{
    /**
     * @param ElementInterface $element
     *
     * @return string
     */
    public function convert(ElementInterface $element)
    {
        $value = $element->getValue();

        $markdown = '';

        $lines = preg_split('/\r\n|\r|\n/', $value);
        foreach ($lines as $line) {
            /*
             * Some special characters need to be escaped based on the position that they appear
             * The following function will deal with those special cases.
             */
            $markdown .= $this->escapeSpecialCharacters($line);
            $markdown .= "\n";
        }

        return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : '';
    }

    /**
     * @return string[]
     */
    public function getSupportedTags()
    {
        return array('p');
    }

    /**
     * @param string $line
     *
     * @return string
     */
    private function escapeSpecialCharacters($line)
    {
        $line = $this->escapeFirstCharacters($line);
        $line = $this->escapeOtherCharacters($line);
        $line = $this->escapeOtherCharactersRegex($line);

        return $line;
    }

    /**
     * @param string $line
     *
     * @return string
     */
    private function escapeFirstCharacters($line)
    {
        $escapable = array(
            '>',
            '- ',
            '+ ',
            '--',
            '~~~',
            '---',
            '- - -'
        );

        foreach ($escapable as $i) {
            if (strpos(ltrim($line), $i) === 0) {
                // Found a character that must be escaped, adding a backslash before
                return '\\' . ltrim($line);
            }
        }

        return $line;
    }

    /**
     * @param string $line
     *
     * @return string
     */
    private function escapeOtherCharacters($line)
    {
        $escapable = array(
            '<!--'
        );

        foreach ($escapable as $i) {
            if (strpos($line, $i) !== false) {
                // Found an escapable character, escaping it
                $line = substr_replace($line, '\\', strpos($line, $i), 0);
            }
        }

        return $line;
    }

    /**
     * @param string $line
     *
     * @return string
     */
    private function escapeOtherCharactersRegex($line)
    {
        $regExs = array(
            // Match numbers ending on ')' or '.' that are at the beginning of the line.
            // They will be escaped if immediately followed by a space or newline.
            '/^[0-9]+(?=(\)|\.)( |$))/'
        );

        foreach ($regExs as $i) {
            if (preg_match($i, $line, $match)) {
                // Matched an escapable character, adding a backslash on the string before the offending character
                $line = substr_replace($line, '\\', strlen($match[0]), 0);
            }
        }

        return $line;
    }
}