Completed
Push — master ( 09adde...747182 )
by Colin
07:41
created

HtmlConverter::convert()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 9.0856
cc 3
eloc 11
nc 3
nop 1
crap 3
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 75
    public function __construct(array $options = array())
30
    {
31
        $defaults = array(
32 75
            'header_style'    => 'setext', // Set to 'atx' to output H1 and H2 headers as # Header1 and ## Header2
33 75
            'suppress_errors' => true, // Set to false to show warnings when loading malformed HTML
34 75
            '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 75
            'bold_style'      => '**', // Set to '__' if you prefer the underlined style
36 75
            'italic_style'    => '_', // Set to '*' if you prefer the asterisk style
37 75
            'remove_nodes'    => '', // space-separated list of dom nodes that should be removed. example: 'meta style script'
38 75
        );
39
40 75
        $this->environment = Environment::createDefaultEnvironment($defaults);
41
42 75
        $this->environment->getConfig()->merge($options);
43 75
    }
44
45
    /**
46
     * @return Environment
47
     */
48
    public function getEnvironment()
49
    {
50
        return $this->environment;
51
    }
52
53
    /**
54
     * @return Configuration
55
     */
56 72
    public function getConfig()
57
    {
58 72
        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 3
    public function __invoke($html)
71
    {
72 3
        return $this->convert($html);
73
    }
74
75
    /**
76
     * Convert
77
     *
78
     * Loads HTML and passes to getMarkdown()
79
     *
80
     * @param $html
81
     *
82
     * @return string The Markdown version of the html
83
     */
84 75
    public function convert($html)
85 3
    {
86 75
        if (trim($html) === '') {
87 3
            return '';
88 3
        }
89
90 75
        $document = $this->createDOMDocument($html);
91
92
        // Work on the entire DOM tree (including head and body)
93 75
        if (!($root = $document->getElementsByTagName('html')->item(0))) {
94 3
            throw new \InvalidArgumentException('Invalid HTML was provided');
95 3
        }
96
97 75
        $rootElement = new Element($root);
98 75
        $this->convertChildren($rootElement);
99
100
        // Store the now-modified DOMDocument as a string
101 72
        $markdown = $document->saveHTML();
102
103 72
        $markdown = $this->sanitize($markdown);
104
105 72
        return $markdown;
106
    }
107
108
    /**
109
     * @param string $html
110
     *
111
     * @return \DOMDocument
112
     */
113 72
    private function createDOMDocument($html)
114
    {
115 72
        $document = new \DOMDocument();
116
117 72
        if ($this->getConfig()->getOption('suppress_errors')) {
118
            // Suppress conversion errors (from http://bit.ly/pCCRSX)
119 72
            libxml_use_internal_errors(true);
120 72
        }
121
122
        // Hack to load utf-8 HTML (from http://bit.ly/pVDyCt)
123 72
        $document->loadHTML('<?xml encoding="UTF-8">' . $html);
124 72
        $document->encoding = 'UTF-8';
125
126 72
        if ($this->getConfig()->getOption('suppress_errors')) {
127 72
            libxml_clear_errors();
128 72
        }
129
130 72
        return $document;
131
    }
132
133
    /**
134
     * Convert Children
135
     *
136
     * Recursive function to drill into the DOM and convert each node into Markdown from the inside out.
137
     *
138
     * Finds children of each node and convert those to #text nodes containing their Markdown equivalent,
139
     * starting with the innermost element and working up to the outermost element.
140
     *
141
     * @param ElementInterface $element
142
     */
143 72
    private function convertChildren(ElementInterface $element)
144
    {
145
        // Don't convert HTML code inside <code> and <pre> blocks to Markdown - that should stay as HTML
146 72
        if ($element->isDescendantOf(array('pre', 'code'))) {
147 12
            return;
148
        }
149
150
        // If the node has children, convert those to Markdown first
151 72
        if ($element->hasChildren()) {
152 72
            foreach ($element->getChildren() as $child) {
153 72
                $this->convertChildren($child);
154 72
            }
155 72
        }
156
157
        // Now that child nodes have been converted, convert the original node
158 72
        $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 72
        $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 72
    }
165
166
    /**
167
     * Convert to Markdown
168
     *
169
     * Converts an individual node into a #text node containing a string of its Markdown equivalent.
170
     *
171
     * Example: An <h3> node with text content of 'Title' becomes a text node with content of '### Title'
172
     *
173
     * @param ElementInterface $element
174
     *
175
     * @return string The converted HTML as Markdown
176
     */
177 72
    protected function convertToMarkdown(ElementInterface $element)
178
    {
179 72
        $tag = $element->getTagName();
180
181
        // Strip nodes named in remove_nodes
182 72
        $tags_to_remove = explode(' ', $this->getConfig()->getOption('remove_nodes'));
183 72
        if (in_array($tag, $tags_to_remove)) {
184 3
            return false;
185
        }
186
187 72
        $converter = $this->environment->getConverterByTag($tag);
188
189 72
        return $converter->convert($element);
190
    }
191
192
    /**
193
     * @param string $markdown
194
     *
195
     * @return string
196
     */
197 72
    protected function sanitize($markdown)
198
    {
199 72
        $markdown = html_entity_decode($markdown, ENT_QUOTES, 'UTF-8');
200 72
        $markdown = preg_replace('/<!DOCTYPE [^>]+>/', '', $markdown); // Strip doctype declaration
201 72
        $unwanted = array('<html>', '</html>', '<body>', '</body>', '<head>', '</head>', '<?xml encoding="UTF-8">', '&#xD;');
202 72
        $markdown = str_replace($unwanted, '', $markdown); // Strip unwanted tags
203 72
        $markdown = trim($markdown, "\n\r\0\x0B");
204
205 72
        return $markdown;
206
    }
207
}
208