Completed
Pull Request — master (#85)
by ignace nyamagana
24:41
created

HtmlConverter::convertToMarkdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\HTMLToMarkdown;
4
5
/**
6
 * Class HtmlConverter
7
 *
8
 * A helper class to convert HTML to Markdown.
9
 *
10
 * @author Colin O'Dell <[email protected]>
11
 * @author Nick Cernis <[email protected]>
12
 *
13
 * @link https://github.com/thephpleague/html-to-markdown/ Latest version on GitHub.
14
 *
15
 * @license http://www.opensource.org/licenses/mit-license.php MIT
16
 */
17
class HtmlConverter
18
{
19
    /**
20
     * @var Environment
21
     */
22
    protected $environment;
23
24
    /**
25
     * Constructor
26
     *
27
     * @param array $options Configuration options
28
     */
29 69
    public function __construct(array $options = array())
30
    {
31
        $defaults = array(
32 69
            'header_style'    => 'setext', // Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2
33 69
            'suppress_errors' => true, // Set to false to show warnings when loading malformed HTML
34 69
            'strip_tags'      => false, // Set to true to strip tags that don't have markdown equivalents. N.B. Strips tags, not their content. Useful to clean MS Word HTML output.
35 69
            'bold_style'      => '**', // Set to '__' if you prefer the underlined style
36 69
            'italic_style'    => '_', // Set to '*' if you prefer the asterisk style
37 69
            'remove_nodes'    => '', // space-separated list of dom nodes that should be removed. example: 'meta style script'
38 69
        );
39
40 69
        $this->environment = Environment::createDefaultEnvironment($defaults);
41
42 69
        $this->environment->getConfig()->merge($options);
43 69
    }
44
45
    /**
46
     * @return Environment
47
     */
48
    public function getEnvironment()
49
    {
50
        return $this->environment;
51
    }
52
53
    /**
54
     * @return Configuration
55
     */
56 66
    public function getConfig()
57
    {
58 66
        return $this->environment->getConfig();
59
    }
60
61
    /**
62
     * Convert
63
     *
64
     * @see HtmlConverter::convert
65
     *
66
     * @param string $html
67
     *
68
     * @return string The Markdown version of the html
69
     */
70 69
    public function __invoke($html)
71
    {
72 69
        return $this->convert($html);
73 3
    }
74
75
    /**
76 66
     * Convert
77
     *
78
     * Loads HTML and passes to getMarkdown()
79 66
     *
80
     * @param $html
81
     *
82
     * @return string The Markdown version of the html
83 66
     */
84 66
    public function convert($html)
85
    {
86
        if (trim($html) === '') {
87 69
            return '';
88
        }
89 69
90
        $document = $this->createDOMDocument($html);
91 69
92 3
        // Work on the entire DOM tree (including head and body)
93
        if (!($root = $document->getElementsByTagName('html')->item(0))) {
94
            throw new \InvalidArgumentException('Invalid HTML was provided');
95
        }
96
97
        $rootElement = new Element($root);
98
        $this->convertChildren($rootElement);
99 66
100
        // Store the now-modified DOMDocument as a string
101 66
        $markdown = $document->saveHTML();
102
103 66
        $markdown = $this->sanitize($markdown);
104
105 66
        return $markdown;
106 66
    }
107
108
    /**
109 66
     * @param string $html
110 66
     *
111
     * @return \DOMDocument
112 66
     */
113 66
    private function createDOMDocument($html)
114 66
    {
115
        $document = new \DOMDocument();
116 66
117
        if ($this->getConfig()->getOption('suppress_errors')) {
118
            // Suppress conversion errors (from http://bit.ly/pCCRSX)
119
            libxml_use_internal_errors(true);
120
        }
121
122
        // Hack to load utf-8 HTML (from http://bit.ly/pVDyCt)
123
        $document->loadHTML('<?xml encoding="UTF-8">' . $html);
124
        $document->encoding = 'UTF-8';
125
126
        if ($this->getConfig()->getOption('suppress_errors')) {
127
            libxml_clear_errors();
128
        }
129 66
130
        return $document;
131
    }
132 66
133 9
    /**
134
     * Convert Children
135
     *
136
     * Recursive function to drill into the DOM and convert each node into Markdown from the inside out.
137 66
     *
138 66
     * Finds children of each node and convert those to #text nodes containing their Markdown equivalent,
139 66
     * starting with the innermost element and working up to the outermost element.
140 66
     *
141 66
     * @param ElementInterface $element
142
     */
143
    private function convertChildren(ElementInterface $element)
144 66
    {
145
        // Don't convert HTML code inside <code> and <pre> blocks to Markdown - that should stay as HTML
146
        if ($element->isDescendantOf(array('pre', 'code'))) {
147
            return;
148
        }
149 66
150 66
        // If the node has children, convert those to Markdown first
151
        if ($element->hasChildren()) {
152
            foreach ($element->getChildren() as $child) {
153
                $this->convertChildren($child);
154
            }
155
        }
156
157
        // Now that child nodes have been converted, convert the original node
158
        $markdown = $this->convertToMarkdown($element);
159
160
        // Create a DOM text node containing the Markdown equivalent of the original node
161
162
        // Replace the old $node e.g. '<h3>Title</h3>' with the new $markdown_node e.g. '### Title'
163 66
        $element->setFinalMarkdown($markdown);
0 ignored issues
show
Security Bug introduced by
It seems like $markdown defined by $this->convertToMarkdown($element) on line 158 can also be of type false; however, League\HTMLToMarkdown\El...ace::setFinalMarkdown() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
164
    }
165 66
166
    /**
167
     * Convert to Markdown
168 66
     *
169 66
     * Converts an individual node into a #text node containing a string of its Markdown equivalent.
170 3
     *
171
     * Example: An <h3> node with text content of 'Title' becomes a text node with content of '### Title'
172
     *
173 66
     * @param ElementInterface $element
174
     *
175 66
     * @return string The converted HTML as Markdown
176
     */
177
    protected function convertToMarkdown(ElementInterface $element)
178
    {
179
        $tag = $element->getTagName();
180
181
        // Strip nodes named in remove_nodes
182
        $tags_to_remove = explode(' ', $this->getConfig()->getOption('remove_nodes'));
183 66
        if (in_array($tag, $tags_to_remove)) {
184
            return false;
185 66
        }
186 66
187 66
        $converter = $this->environment->getConverterByTag($tag);
188 66
189 66
        return $converter->convert($element);
190 66
    }
191
192 66
    /**
193
     * @param string $markdown
194
     *
195
     * @return string
196
     */
197
    protected function sanitize($markdown)
198
    {
199
        $markdown = html_entity_decode($markdown, ENT_QUOTES, 'UTF-8');
200
        $markdown = html_entity_decode($markdown, ENT_QUOTES, 'UTF-8'); // Double decode to cover cases like &amp;nbsp; http://www.php.net/manual/en/function.htmlentities.php#99984
201
        $markdown = preg_replace('/<!DOCTYPE [^>]+>/', '', $markdown); // Strip doctype declaration
202
        $unwanted = array('<html>', '</html>', '<body>', '</body>', '<head>', '</head>', '<?xml encoding="UTF-8">', '&#xD;');
203
        $markdown = str_replace($unwanted, '', $markdown); // Strip unwanted tags
204
        $markdown = trim($markdown, "\n\r\0\x0B");
205
206
        return $markdown;
207
    }
208
}
209