dumpit5.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * dumpit5.php
  4. *
  5. * a command-line script which dumps the given HTML, PHP, ASP, XHTML, etc.
  6. * file as it is represented in the document model.
  7. *
  8. * NOTE: Only works with tidy for PHP 5+, for tidy in 4.3.x, see dumpit.php
  9. *
  10. * By: John Coggeshall <john@php.net>
  11. *
  12. * Usage; php dumpit5.php <filename>
  13. */
  14. $tidy = tidy_parse_file($_SERVER['argv'][1]);
  15. /* Optionally you can do this here if you want to fix up the document */
  16. /* $tidy->clean_repair() */
  17. $tree = $tidy->root();
  18. dump_tree($tree);
  19. echo "\n";
  20. function node_type($type) {
  21. switch($type) {
  22. case TIDY_NODETYPE_ROOT: return "Root Node";
  23. case TIDY_NODETYPE_DOCTYPE: return "DocType Node";
  24. case TIDY_NODETYPE_COMMENT: return "Comment Node";
  25. case TIDY_NODETYPE_PROCINS: return "ProcIns Node";
  26. case TIDY_NODETYPE_TEXT: return "Text Node";
  27. case TIDY_NODETYPE_START: return "Start Node";
  28. case TIDY_NODETYPE_END: return "End Node";
  29. case TIDY_NODETYPE_STARTEND: return "Start/End Node";
  30. case TIDY_NODETYPE_CDATA: return "CDATA Node";
  31. case TIDY_NODETYPE_SECTION: return "Section Node";
  32. case TIDY_NODETYPE_ASP: return "ASP Source Code Node";
  33. case TIDY_NODETYPE_PHP: return "PHP Source Code Node";
  34. case TIDY_NODETYPE_JSTE: return "JSTE Source Code";
  35. case TIDY_NODETYPE_XMLDECL: return "XML Declaration Node";
  36. default: return "Unknown Node";
  37. }
  38. }
  39. function do_leaf($string, $indent) {
  40. for($i = 0; $i < $indent; $i++) {
  41. echo " ";
  42. }
  43. echo $string;
  44. }
  45. function dump_tree(tidyNode $node, $indent = 0) {
  46. /* Put something there if the node name is empty */
  47. $nodename = trim(strtoupper($node->name));
  48. $nodename = (empty($nodename)) ? "[EMPTY]" : $nodename;
  49. /* Generate the Node, and a pretty name for it */
  50. do_leaf(" + $nodename (".node_type($node->type).")\n", $indent);
  51. /* Check to see if this node is a text node. Text nodes are
  52. generated by start/end tags and contain the text in between.
  53. i.e. <B>foo</B> will create a text node with $node->value
  54. equal to 'foo' */
  55. if($node->type == TIDY_NODETYPE_TEXT) {
  56. do_leaf(" |\n", $indent);
  57. do_leaf(" +---- Value: '{$node->value}'\n", $indent);
  58. }
  59. if(count($node->attribute)) {
  60. do_leaf(" |\n", $indent);
  61. do_leaf(" +---- Attributes\n", $indent);
  62. foreach($node->attribute as $name=>$value) {
  63. @do_leaf(" +-- $name\n", $indent);
  64. do_leaf(" | +-- Value: $value\n", $indent);
  65. }
  66. }
  67. /* Recurse along the children to generate the remaining nodes */
  68. if($node->hasChildren()) {
  69. foreach($node->child as $child) {
  70. dump_tree($child, $indent + 3);
  71. }
  72. }
  73. }
  74. ?>