Completed
Pull Request — master (#139)
by Philip
04:58
created

HtmlConverter   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.15%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 24
lcom 1
cbo 5
dl 0
loc 216
ccs 75
cts 78
cp 0.9615
rs 10
c 2
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
A getEnvironment() 0 4 1
A getConfig() 0 4 1
A __invoke() 0 4 1
A convert() 0 21 3
A createDOMDocument() 0 19 3
B convertChildren() 0 23 5
A convertToMarkdown() 0 14 2
B sanitize() 0 28 5
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 Environment|array $options Environment object or configuration options
28
     */
29 81
    public function __construct($options = array())
30
    {
31 81
        if ($options instanceof Environment) {
32 3
            $this->environment = $options;
33 81
        } elseif (is_array($options)) {
34
            $defaults = array(
35 78
                'header_style' => 'setext', // Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2
36 78
                'suppress_errors' => true, // Set to false to show warnings when loading malformed HTML
37 78
                '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.
38 78
                'bold_style' => '**', // Set to '__' if you prefer the underlined style
39 78
                'italic_style' => '_', // Set to '*' if you prefer the asterisk style
40 78
                'remove_nodes' => '', // space-separated list of dom nodes that should be removed. example: 'meta style script'
41 78
                'hard_break' => false, // Set to true to turn <br> into `\n` instead of `  \n`
42 78
                'list_item_style' => '-', // Set the default character for each <li> in a <ul>. Can be '-', '*', or '+'
43 78
            );
44
45 78
            $this->environment = Environment::createDefaultEnvironment($defaults);
46
47 78
            $this->environment->getConfig()->merge($options);
48 78
        }
49 81
    }
50
51
    /**
52
     * @return Environment
53
     */
54
    public function getEnvironment()
55
    {
56
        return $this->environment;
57
    }
58
59
    /**
60
     * @return Configuration
61
     */
62 78
    public function getConfig()
63
    {
64 78
        return $this->environment->getConfig();
65
    }
66
67
    /**
68
     * Convert
69
     *
70
     * @see HtmlConverter::convert
71
     *
72
     * @param string $html
73
     *
74
     * @return string The Markdown version of the html
75
     */
76 3
    public function __invoke($html)
77
    {
78 3
        return $this->convert($html);
79
    }
80
81
    /**
82
     * Convert
83
     *
84
     * Loads HTML and passes to getMarkdown()
85
     *
86
     * @param string $html
87
     *
88
     * @throws \InvalidArgumentException
89
     *
90
     * @return string The Markdown version of the html
91
     */
92 81
    public function convert($html)
93
    {
94 81
        if (trim($html) === '') {
95 3
            return '';
96
        }
97
98 78
        $document = $this->createDOMDocument($html);
99
100
        // Work on the entire DOM tree (including head and body)
101 78
        if (!($root = $document->getElementsByTagName('html')->item(0))) {
102
            throw new \InvalidArgumentException('Invalid HTML was provided');
103
        }
104
105 78
        $rootElement = new Element($root);
106 78
        $this->convertChildren($rootElement);
107
108
        // Store the now-modified DOMDocument as a string
109 78
        $markdown = $document->saveHTML();
110
111 78
        return $this->sanitize($markdown);
112
    }
113
114
    /**
115
     * @param string $html
116
     *
117
     * @return \DOMDocument
118
     */
119 78
    private function createDOMDocument($html)
120
    {
121 78
        $document = new \DOMDocument();
122
123 78
        if ($this->getConfig()->getOption('suppress_errors')) {
124
            // Suppress conversion errors (from http://bit.ly/pCCRSX)
125 75
            libxml_use_internal_errors(true);
126 75
        }
127
128
        // Hack to load utf-8 HTML (from http://bit.ly/pVDyCt)
129 78
        $document->loadHTML('<?xml encoding="UTF-8">' . $html);
130 78
        $document->encoding = 'UTF-8';
131
132 78
        if ($this->getConfig()->getOption('suppress_errors')) {
133 75
            libxml_clear_errors();
134 75
        }
135
136 78
        return $document;
137
    }
138
139
    /**
140
     * Convert Children
141
     *
142
     * Recursive function to drill into the DOM and convert each node into Markdown from the inside out.
143
     *
144
     * Finds children of each node and convert those to #text nodes containing their Markdown equivalent,
145
     * starting with the innermost element and working up to the outermost element.
146
     *
147
     * @param ElementInterface $element
148
     */
149 78
    private function convertChildren(ElementInterface $element)
150
    {
151
        // Don't convert HTML code inside <code> and <pre> blocks to Markdown - that should stay as HTML
152
        // except if the current node is a code tag, which needs to be converted by the CodeConverter.
153 78
        if ($element->isDescendantOf(array('pre', 'code')) && $element->getTagName() !== 'code') {
154 15
            return;
155
        }
156
157
        // If the node has children, convert those to Markdown first
158 78
        if ($element->hasChildren()) {
159 78
            foreach ($element->getChildren() as $child) {
160 78
                $this->convertChildren($child);
161 78
            }
162 78
        }
163
164
        // Now that child nodes have been converted, convert the original node
165 78
        $markdown = $this->convertToMarkdown($element);
166
167
        // Create a DOM text node containing the Markdown equivalent of the original node
168
169
        // Replace the old $node e.g. '<h3>Title</h3>' with the new $markdown_node e.g. '### Title'
170 78
        $element->setFinalMarkdown($markdown);
0 ignored issues
show
Security Bug introduced by
It seems like $markdown defined by $this->convertToMarkdown($element) on line 165 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...
171 78
    }
172
173
    /**
174
     * Convert to Markdown
175
     *
176
     * Converts an individual node into a #text node containing a string of its Markdown equivalent.
177
     *
178
     * Example: An <h3> node with text content of 'Title' becomes a text node with content of '### Title'
179
     *
180
     * @param ElementInterface $element
181
     *
182
     * @return string The converted HTML as Markdown
183
     */
184 78
    protected function convertToMarkdown(ElementInterface $element)
185
    {
186 78
        $tag = $element->getTagName();
187
188
        // Strip nodes named in remove_nodes
189 78
        $tags_to_remove = explode(' ', $this->getConfig()->getOption('remove_nodes'));
190 78
        if (in_array($tag, $tags_to_remove)) {
191 3
            return false;
192
        }
193
194 78
        $converter = $this->environment->getConverterByTag($tag);
195
196 78
        return $converter->convert($element);
197
    }
198
199
    /**
200
     * @param string $markdown
201
     *
202
     * @return string
203
     */
204 78
    protected function sanitize($markdown)
205
    {
206 78
        $markdown = html_entity_decode($markdown, ENT_QUOTES, 'UTF-8');
207 78
        $markdown = preg_replace('/<!DOCTYPE [^>]+>/', '', $markdown); // Strip doctype declaration
208 78
        $markdown = trim($markdown); // Remove blank spaces at the beggining of the html
209
210
        /*
211
         * Removing unwanted tags. Tags should be added to the array in the order they are expected.
212
         * XML, html and body opening tags should be in that order. Same case with closing tags
213
         */
214 78
        $unwanted = array('<?xml encoding="UTF-8">', '<html>', '</html>', '<body>', '</body>', '<head>', '</head>', '&#xD;');
215
216 78
        foreach ($unwanted as $tag) {
217 78
            if (strpos($tag, '/') === false) {
218
                // Opening tags
219 78
                if (strpos($markdown, $tag) === 0) {
220 78
                    $markdown = substr($markdown, strlen($tag));
221 78
                }
222 78
            } else {
223
                // Closing tags
224 78
                if (strpos($markdown, $tag) === strlen($markdown) - strlen($tag)) {
225 69
                    $markdown = substr($markdown, 0, -strlen($tag));
226 69
                }
227
            }
228 78
        }
229
230 78
        return trim($markdown, "\n\r\0\x0B");
231
    }
232
}
233