Back to index

php5  5.3.10
dumpit5.php
Go to the documentation of this file.
00001 <?php
00002     /*
00003      * dumpit5.php
00004      *
00005      * a command-line script which dumps the given HTML, PHP, ASP, XHTML, etc.
00006      * file as it is represented in the document model.
00007      *
00008      * NOTE: Only works with tidy for PHP 5+, for tidy in 4.3.x, see dumpit.php
00009      *
00010      * By: John Coggeshall <john@php.net>
00011      *
00012      * Usage; php dumpit5.php <filename>
00013      */
00014     
00015     $tidy = tidy_parse_file($_SERVER['argv'][1]);
00016     
00017     /* Optionally you can do this here if you want to fix up the document */
00018     
00019     /* $tidy->clean_repair() */
00020               
00021     $tree = $tidy->root();
00022     dump_tree($tree);
00023     echo "\n";
00024     
00025     function node_type($type) {
00026         
00027         switch($type) {
00028             
00029             case TIDY_NODETYPE_ROOT: return "Root Node";
00030             case TIDY_NODETYPE_DOCTYPE: return "DocType Node";
00031             case TIDY_NODETYPE_COMMENT: return "Comment Node";
00032             case TIDY_NODETYPE_PROCINS: return "ProcIns Node";
00033             case TIDY_NODETYPE_TEXT: return "Text Node";
00034             case TIDY_NODETYPE_START: return "Start Node";
00035             case TIDY_NODETYPE_END: return "End Node";
00036             case TIDY_NODETYPE_STARTEND: return "Start/End Node";
00037             case TIDY_NODETYPE_CDATA: return "CDATA Node";
00038             case TIDY_NODETYPE_SECTION: return "Section Node";
00039             case TIDY_NODETYPE_ASP: return "ASP Source Code Node";
00040             case TIDY_NODETYPE_PHP: return "PHP Source Code Node";
00041             case TIDY_NODETYPE_JSTE: return "JSTE Source Code";
00042             case TIDY_NODETYPE_XMLDECL: return "XML Declaration Node";
00043             default: return "Unknown Node";
00044         }
00045     }
00046     
00047     function do_leaf($string, $indent) {
00048         for($i = 0; $i < $indent; $i++) {
00049             echo " ";
00050         }
00051         echo $string;
00052     }
00053     
00054     function dump_tree(tidyNode $node, $indent = 0) {
00055         
00056         /* Put something there if the node name is empty */
00057         $nodename = trim(strtoupper($node->name));
00058         $nodename = (empty($nodename)) ? "[EMPTY]" : $nodename;
00059         
00060         /* Generate the Node, and a pretty name for it */
00061         do_leaf(" + $nodename (".node_type($node->type).")\n", $indent);
00062         
00063         /* Check to see if this node is a text node. Text nodes are
00064            generated by start/end tags and contain the text in between.
00065            i.e. <B>foo</B> will create a text node with $node->value
00066            equal to 'foo' */
00067         if($node->type == TIDY_NODETYPE_TEXT) {
00068             do_leaf("     |\n", $indent);
00069             do_leaf("     +---- Value: '{$node->value}'\n", $indent);
00070         }
00071         
00072         if(count($node->attribute)) {
00073             do_leaf(" |\n", $indent);
00074             do_leaf(" +---- Attributes\n", $indent);
00075             
00076             foreach($node->attribute as $name=>$value) {
00077                 @do_leaf("            +-- $name\n", $indent);
00078                 do_leaf("             |     +-- Value: $value\n", $indent);
00079             }
00080         }
00081         
00082         /* Recurse along the children to generate the remaining nodes */
00083         if($node->hasChildren()) {
00084             foreach($node->child as $child) {
00085                 dump_tree($child, $indent + 3);
00086             }
00087         }
00088         
00089     }
00090 
00091     
00092 ?>