1 | <?php |
||
7 | class Tocifier |
||
|
|||
8 | { |
||
9 | // Prefix to prepend to every URL fragment |
||
10 | public static $prefix = 'TOC-'; |
||
11 | |||
12 | // The original HTML |
||
13 | private $_raw_html = ''; |
||
14 | |||
15 | // $_raw_html augmented with anchor ids for proper navigation |
||
16 | private $_html = ''; |
||
17 | |||
18 | // The most recently generated TOC tree. |
||
19 | private $_tree; |
||
20 | |||
21 | // Array of references to the potential parents |
||
22 | private $_dangling = array(); |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Get the TOC node closest to a given nesting level. |
||
27 | * |
||
28 | * @param int $level The requested nesting level. |
||
29 | * @return array |
||
30 | */ |
||
31 | private function &_getParent($level) |
||
41 | |||
42 | /** |
||
43 | * Get the plain text content from a DOM element. |
||
44 | * |
||
45 | * @param DOMElement $tag The DOM element to inspect. |
||
46 | * @return string |
||
47 | */ |
||
48 | private function _getPlainText(DOMElement $tag) |
||
60 | |||
61 | /** |
||
62 | * Create a new TOC node. |
||
63 | * |
||
64 | * @param string $id Node id, used for anchoring |
||
65 | * @param string $text Title text |
||
66 | * @param int $level The nesting level of the node |
||
67 | * @return array |
||
68 | */ |
||
69 | private function &_newNode($id, $text, $level) |
||
88 | |||
89 | /** |
||
90 | * Process the specific document. |
||
91 | * |
||
92 | * @param DOMDocument $doc The document to process. |
||
93 | */ |
||
94 | private function _processDocument($doc) |
||
127 | |||
128 | /** |
||
129 | * Debug function for dumping a TOC node and its children. |
||
130 | * |
||
131 | * @param array $node The TOC node to dump |
||
132 | * @param string $indent Indentation string. |
||
133 | */ |
||
134 | private function _dumpBranch($node, $indent = '') |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Create a new TOCifier instance. |
||
147 | * |
||
148 | * A string containing the HTML to parse for TOC must be passed |
||
149 | * in. The real processing will be triggered by the process() |
||
150 | * method. |
||
151 | * |
||
152 | * Parsing a file can be easily performed by using |
||
153 | * file_get_contents(): |
||
154 | * |
||
155 | * <code> |
||
156 | * $tocifier = new Tocifier(@file_get_content($file)); |
||
157 | * </code> |
||
158 | * |
||
159 | * @param string $html A chunk of valid HTML (UTF-8 encoded). |
||
160 | */ |
||
161 | public function __construct($html) |
||
165 | |||
166 | /** |
||
167 | * Parse and process the HTML chunk. |
||
168 | * |
||
169 | * The parsing phase involves picking up all the HTML header |
||
170 | * elements (from <h1> to <h6>), so if the HTML is not well formed |
||
171 | * or any other error is encountered this function will fail. |
||
172 | * |
||
173 | * @return boolean true on success, false on errors. |
||
174 | */ |
||
175 | public function process() |
||
197 | |||
198 | /** |
||
199 | * Get the TOC (Table Of Contents) from the provided HTML. |
||
200 | * |
||
201 | * The HTML must be provided throught the constructor. |
||
202 | * |
||
203 | * The TOC is represented in the form of: |
||
204 | * |
||
205 | * <code> |
||
206 | * array( |
||
207 | * array('id' => 'TOC-1', |
||
208 | * 'title' => 'Item 1', |
||
209 | * 'children' => array( |
||
210 | * array('id' => 'TOC-2', |
||
211 | * 'title' => 'Subitem 1.1' |
||
212 | * ), |
||
213 | * array('id' => 'TOC-3', |
||
214 | * 'title' => 'Subitem 1.2', |
||
215 | * 'children' => array( |
||
216 | * array('id' => 'TOC-4', |
||
217 | * 'title => 'Subsubitem 1.2.1' |
||
218 | * ))))), |
||
219 | * array('id' => 'TOC-5, |
||
220 | * 'title' => 'Item 2', |
||
221 | * 'children' => array( |
||
222 | * array('id' => 'TOC-6', |
||
223 | * 'title' => 'Subitem 2.1' |
||
224 | * ), |
||
225 | * array('id' => 'TOC-7', |
||
226 | * 'title' => 'Subitem 2.2' |
||
227 | * )))); |
||
228 | * </code> |
||
229 | * |
||
230 | * The TOC is cached, so subsequent calls will return the same tree. |
||
231 | * |
||
232 | * @return Array An array representing the TOC. A valid array is |
||
233 | * always returned. |
||
234 | */ |
||
235 | public function getTOC() |
||
239 | |||
240 | /** |
||
241 | * Get the HTML augmented with anchors for proper navigation. |
||
242 | * |
||
243 | * The HTML must be provided throught the feedHtml() method. |
||
244 | * The returned string is cached, so subsequent calls will return |
||
245 | * the same string without further processing. |
||
246 | * |
||
247 | * @return String The augmented HTML. |
||
248 | */ |
||
249 | public function getHtml() |
||
253 | |||
254 | /** |
||
255 | * Dump the TOC to stdout for debugging purpose. |
||
256 | */ |
||
257 | public function dumpTOC() |
||
261 | } |
||
262 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.