1 | <?php |
||
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()) |
|
49 | |||
50 | /** |
||
51 | * @return Environment |
||
52 | */ |
||
53 | public function getEnvironment() |
||
57 | |||
58 | /** |
||
59 | * @return Configuration |
||
60 | */ |
||
61 | 78 | public function getConfig() |
|
65 | |||
66 | /** |
||
67 | * Convert |
||
68 | * |
||
69 | * @see HtmlConverter::convert |
||
70 | * |
||
71 | * @param string $html |
||
72 | * |
||
73 | * @return string The Markdown version of the html |
||
74 | */ |
||
75 | 3 | public function __invoke($html) |
|
79 | |||
80 | /** |
||
81 | * Convert |
||
82 | * |
||
83 | * Loads HTML and passes to getMarkdown() |
||
84 | * |
||
85 | * @param string $html |
||
86 | * |
||
87 | * @throws \InvalidArgumentException |
||
88 | * |
||
89 | * @return string The Markdown version of the html |
||
90 | */ |
||
91 | 81 | public function convert($html) |
|
92 | { |
||
93 | 81 | if (trim($html) === '') { |
|
94 | 3 | return ''; |
|
95 | } |
||
96 | |||
97 | 78 | $document = $this->createDOMDocument($html); |
|
98 | |||
99 | // Work on the entire DOM tree (including head and body) |
||
100 | 78 | if (!($root = $document->getElementsByTagName('html')->item(0))) { |
|
101 | throw new \InvalidArgumentException('Invalid HTML was provided'); |
||
102 | } |
||
103 | |||
104 | 78 | $rootElement = new Element($root); |
|
105 | 78 | $this->convertChildren($rootElement); |
|
106 | |||
107 | // Store the now-modified DOMDocument as a string |
||
108 | 78 | $markdown = $document->saveHTML(); |
|
109 | |||
110 | 78 | return $this->sanitize($markdown); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $html |
||
115 | * |
||
116 | * @return \DOMDocument |
||
117 | */ |
||
118 | 78 | private function createDOMDocument($html) |
|
137 | |||
138 | /** |
||
139 | * Convert Children |
||
140 | * |
||
141 | * Recursive function to drill into the DOM and convert each node into Markdown from the inside out. |
||
142 | * |
||
143 | * Finds children of each node and convert those to #text nodes containing their Markdown equivalent, |
||
144 | * starting with the innermost element and working up to the outermost element. |
||
145 | * |
||
146 | * @param ElementInterface $element |
||
147 | */ |
||
148 | 78 | private function convertChildren(ElementInterface $element) |
|
171 | |||
172 | /** |
||
173 | * Convert to Markdown |
||
174 | * |
||
175 | * Converts an individual node into a #text node containing a string of its Markdown equivalent. |
||
176 | * |
||
177 | * Example: An <h3> node with text content of 'Title' becomes a text node with content of '### Title' |
||
178 | * |
||
179 | * @param ElementInterface $element |
||
180 | * |
||
181 | * @return string The converted HTML as Markdown |
||
182 | */ |
||
183 | 78 | protected function convertToMarkdown(ElementInterface $element) |
|
197 | |||
198 | /** |
||
199 | * @param string $markdown |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 78 | protected function sanitize($markdown) |
|
231 | } |
||
232 |
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
.