1 | <?php |
||
17 | class HtmlConverter implements HtmlConverterInterface |
||
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 | 93 | public function __construct($options = array()) |
|
52 | |||
53 | /** |
||
54 | * @return Environment |
||
55 | */ |
||
56 | public function getEnvironment() |
||
60 | |||
61 | /** |
||
62 | * @return Configuration |
||
63 | */ |
||
64 | 90 | public function getConfig() |
|
68 | |||
69 | /** |
||
70 | * Convert |
||
71 | * |
||
72 | * @see HtmlConverter::convert |
||
73 | * |
||
74 | * @param string $html |
||
75 | * |
||
76 | * @return string The Markdown version of the html |
||
77 | */ |
||
78 | 3 | public function __invoke($html) |
|
82 | |||
83 | /** |
||
84 | * Convert |
||
85 | * |
||
86 | * Loads HTML and passes to getMarkdown() |
||
87 | * |
||
88 | * @param string $html |
||
89 | * |
||
90 | * @throws \InvalidArgumentException |
||
91 | * |
||
92 | * @return string The Markdown version of the html |
||
93 | */ |
||
94 | 93 | public function convert($html) |
|
115 | |||
116 | /** |
||
117 | * @param string $html |
||
118 | * |
||
119 | * @return \DOMDocument |
||
120 | */ |
||
121 | 90 | private function createDOMDocument($html) |
|
122 | { |
||
123 | 90 | $document = new \DOMDocument(); |
|
124 | |||
125 | 90 | if ($this->getConfig()->getOption('suppress_errors')) { |
|
126 | // Suppress conversion errors (from http://bit.ly/pCCRSX) |
||
127 | 87 | libxml_use_internal_errors(true); |
|
128 | } |
||
129 | |||
130 | // Hack to load utf-8 HTML (from http://bit.ly/pVDyCt) |
||
131 | 90 | $document->loadHTML('<?xml encoding="UTF-8">' . $html); |
|
132 | 90 | $document->encoding = 'UTF-8'; |
|
133 | |||
134 | 90 | if ($this->getConfig()->getOption('suppress_errors')) { |
|
135 | 87 | libxml_clear_errors(); |
|
136 | } |
||
137 | |||
138 | 90 | return $document; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Convert Children |
||
143 | * |
||
144 | * Recursive function to drill into the DOM and convert each node into Markdown from the inside out. |
||
145 | * |
||
146 | * Finds children of each node and convert those to #text nodes containing their Markdown equivalent, |
||
147 | * starting with the innermost element and working up to the outermost element. |
||
148 | * |
||
149 | * @param ElementInterface $element |
||
150 | */ |
||
151 | 90 | private function convertChildren(ElementInterface $element) |
|
152 | { |
||
153 | // Don't convert HTML code inside <code> and <pre> blocks to Markdown - that should stay as HTML |
||
154 | // except if the current node is a code tag, which needs to be converted by the CodeConverter. |
||
155 | 90 | if ($element->isDescendantOf(array('pre', 'code')) && $element->getTagName() !== 'code') { |
|
156 | 18 | return; |
|
157 | } |
||
158 | |||
159 | // If the node has children, convert those to Markdown first |
||
160 | 90 | if ($element->hasChildren()) { |
|
161 | 90 | foreach ($element->getChildren() as $child) { |
|
162 | 90 | $this->convertChildren($child); |
|
163 | } |
||
164 | } |
||
165 | |||
166 | // Now that child nodes have been converted, convert the original node |
||
167 | 90 | $markdown = $this->convertToMarkdown($element); |
|
168 | |||
169 | // Create a DOM text node containing the Markdown equivalent of the original node |
||
170 | |||
171 | // Replace the old $node e.g. '<h3>Title</h3>' with the new $markdown_node e.g. '### Title' |
||
172 | 90 | $element->setFinalMarkdown($markdown); |
|
|
|||
173 | 90 | } |
|
174 | |||
175 | /** |
||
176 | * Convert to Markdown |
||
177 | * |
||
178 | * Converts an individual node into a #text node containing a string of its Markdown equivalent. |
||
179 | * |
||
180 | * Example: An <h3> node with text content of 'Title' becomes a text node with content of '### Title' |
||
181 | * |
||
182 | * @param ElementInterface $element |
||
183 | * |
||
184 | * @return string The converted HTML as Markdown |
||
185 | */ |
||
186 | 90 | protected function convertToMarkdown(ElementInterface $element) |
|
200 | |||
201 | /** |
||
202 | * @param string $markdown |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | 90 | protected function sanitize($markdown) |
|
234 | |||
235 | /** |
||
236 | * Pass a series of key-value pairs in an array; these will be passed |
||
237 | * through the config and set. |
||
238 | * The advantage of this is that it can allow for static use (IE in Laravel). |
||
239 | * An example being: |
||
240 | * |
||
241 | * HtmlConverter::setOptions(['strip_tags' => true])->convert('<h1>test</h1>'); |
||
242 | */ |
||
243 | public function setOptions(array $options) |
||
253 | } |
||
254 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
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 returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.