1 | <?php |
||
13 | class Tocifier |
||
14 | { |
||
15 | // Prefix to prepend to every URL fragment |
||
16 | public static $prefix = 'TOC-'; |
||
17 | |||
18 | // The original HTML |
||
19 | private $raw_html = ''; |
||
20 | |||
21 | // $raw_html augmented for proper navigation |
||
22 | private $html = ''; |
||
23 | |||
24 | // The most recently generated TOC tree. |
||
25 | private $tree; |
||
26 | |||
27 | // Array of references to the potential parents |
||
28 | private $dangling = array(); |
||
29 | |||
30 | // Callback for augmenting a single DOMElement |
||
31 | private $augment_callback; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Get the TOC node closest to a given nesting level. |
||
36 | * |
||
37 | * @param int $level The requested nesting level. |
||
38 | * @return array |
||
39 | */ |
||
40 | private function &getParent($level) |
||
50 | |||
51 | /** |
||
52 | * Get the plain text content from a DOM element. |
||
53 | * |
||
54 | * @param DOMElement $tag The DOM element to inspect. |
||
55 | * @return string |
||
56 | */ |
||
57 | private function getPlainText(DOMElement $tag) |
||
69 | |||
70 | /** |
||
71 | * Create a new TOC node. |
||
72 | * |
||
73 | * @param string $id Node id, used for anchoring |
||
74 | * @param string $text Title text |
||
75 | * @param int $level The nesting level of the node |
||
76 | * @return array |
||
77 | */ |
||
78 | private function &newNode($id, $text, $level) |
||
97 | |||
98 | /** |
||
99 | * Process the specific document. |
||
100 | * |
||
101 | * @param DOMDocument $doc The document to process. |
||
102 | */ |
||
103 | private function processDocument($doc) |
||
131 | |||
132 | /** |
||
133 | * Debug function for dumping a TOC node and its children. |
||
134 | * |
||
135 | * @param array $node The TOC node to dump |
||
136 | * @param string $indent Indentation string. |
||
137 | */ |
||
138 | private function dumpBranch($node, $indent = '') |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Create a new TOCifier instance. |
||
151 | * |
||
152 | * A string containing the HTML to parse for TOC must be passed |
||
153 | * in. The real processing will be triggered by the process() |
||
154 | * method. |
||
155 | * |
||
156 | * Parsing a file can be easily performed by using |
||
157 | * file_get_contents(): |
||
158 | * |
||
159 | * <code> |
||
160 | * $tocifier = new Tocifier(@file_get_content($file)); |
||
161 | * </code> |
||
162 | * |
||
163 | * @param string $html A chunk of valid HTML (UTF-8 encoded). |
||
164 | */ |
||
165 | public function __construct($html) |
||
170 | |||
171 | /** |
||
172 | * Change the augment method used by this Tocifier instance. |
||
173 | * |
||
174 | * By default the HTML is augmented prepending an anchor before |
||
175 | * every valid destination. This behavior can be changed by using |
||
176 | * Tocifier::setId() (that directly sets the ID on the destination |
||
177 | * elements) or by providing your own callback. |
||
178 | * |
||
179 | * The signature of the callback to pass in should be compatible |
||
180 | * with: |
||
181 | * |
||
182 | * function callback(DOMDocument $dom, DOMElement $element, $id) |
||
183 | * |
||
184 | * @param callable $callback The new function to call for |
||
185 | * augmenting DOMElement |
||
186 | */ |
||
187 | public function setAugmentCallback($callback) |
||
191 | |||
192 | /** |
||
193 | * Parse and process the HTML chunk. |
||
194 | * |
||
195 | * The parsing phase involves picking up all the HTML header |
||
196 | * elements (from <h1> to <h6>), so if the HTML is not well formed |
||
197 | * or any other error is encountered this function will fail. |
||
198 | * |
||
199 | * @return boolean true on success, false on errors. |
||
200 | */ |
||
201 | public function process() |
||
223 | |||
224 | /** |
||
225 | * Get the TOC (Table Of Contents) from the provided HTML. |
||
226 | * |
||
227 | * The HTML must be provided throught the constructor. |
||
228 | * |
||
229 | * The TOC is represented in the form of: |
||
230 | * |
||
231 | * <code> |
||
232 | * array( |
||
233 | * array('id' => 'TOC-1', |
||
234 | * 'title' => 'Item 1', |
||
235 | * 'children' => array( |
||
236 | * array('id' => 'TOC-2', |
||
237 | * 'title' => 'Subitem 1.1' |
||
238 | * ), |
||
239 | * array('id' => 'TOC-3', |
||
240 | * 'title' => 'Subitem 1.2', |
||
241 | * 'children' => array( |
||
242 | * array('id' => 'TOC-4', |
||
243 | * 'title => 'Subsubitem 1.2.1' |
||
244 | * ))))), |
||
245 | * array('id' => 'TOC-5, |
||
246 | * 'title' => 'Item 2', |
||
247 | * 'children' => array( |
||
248 | * array('id' => 'TOC-6', |
||
249 | * 'title' => 'Subitem 2.1' |
||
250 | * ), |
||
251 | * array('id' => 'TOC-7', |
||
252 | * 'title' => 'Subitem 2.2' |
||
253 | * )))); |
||
254 | * </code> |
||
255 | * |
||
256 | * The TOC is cached, so subsequent calls will return the same tree. |
||
257 | * |
||
258 | * @return Array An array representing the TOC. A valid array is |
||
259 | * always returned. |
||
260 | */ |
||
261 | public function getTOC() |
||
265 | |||
266 | /** |
||
267 | * Get the HTML augmented for proper navigation. |
||
268 | * |
||
269 | * The HTML must be provided throught the feedHtml() method. |
||
270 | * The returned string is cached, so subsequent calls will return |
||
271 | * the same string without further processing. |
||
272 | * |
||
273 | * @return String The augmented HTML. |
||
274 | */ |
||
275 | public function getHtml() |
||
279 | |||
280 | /** |
||
281 | * Dump the TOC to stdout for debugging purpose. |
||
282 | */ |
||
283 | public function dumpTOC() |
||
287 | |||
288 | /** |
||
289 | * Augment a DOMElement by prepending an anchor. |
||
290 | * |
||
291 | * An HTML fragment such as: |
||
292 | * |
||
293 | * <h1>First</h2> |
||
294 | * <h2>Second</h1> |
||
295 | * |
||
296 | * will become: |
||
297 | * |
||
298 | * <a id="TOC-1" class="anchor"></a><h1>First</h2> |
||
299 | * <a id="TOC-2" class="anchor"></a><h2>Second</h1> |
||
300 | * |
||
301 | * @param DOMDocument $dom The DOM owning $element |
||
302 | * @param DOMElement $element The element to augment |
||
303 | * @param string $id The destination ID |
||
304 | */ |
||
305 | public static function prependAnchor(DOMDocument $dom, DOMElement $element, $id) |
||
312 | |||
313 | /** |
||
314 | * Augment a DOMElement by setting its ID. |
||
315 | * |
||
316 | * An HTML fragment such as: |
||
317 | * |
||
318 | * <h1>First</h2> |
||
319 | * <h2>Second</h1> |
||
320 | * |
||
321 | * will become: |
||
322 | * |
||
323 | * <h1 id="TOC-1" class="anchor">First</h2> |
||
324 | * <h2 id="TOC-2" class="anchor">Second</h1> |
||
325 | * |
||
326 | * @param DOMDocument $dom The DOM owning $element |
||
327 | * @param DOMElement $element The element to augment |
||
328 | * @param string $id The destination ID |
||
329 | */ |
||
330 | public static function setId(DOMDocument $dom, DOMElement $element, $id) |
||
335 | } |
||
336 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.