xref: /PHP-5.5/ext/tidy/examples/dumpit5.php (revision 2f4ca9a2)
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
15    $tidy = tidy_parse_file($_SERVER['argv'][1]);
16
17    /* Optionally you can do this here if you want to fix up the document */
18
19    /* $tidy->clean_repair() */
20
21    $tree = $tidy->root();
22    dump_tree($tree);
23    echo "\n";
24
25    function node_type($type) {
26
27        switch($type) {
28
29            case TIDY_NODETYPE_ROOT: return "Root Node";
30            case TIDY_NODETYPE_DOCTYPE: return "DocType Node";
31            case TIDY_NODETYPE_COMMENT: return "Comment Node";
32            case TIDY_NODETYPE_PROCINS: return "ProcIns Node";
33            case TIDY_NODETYPE_TEXT: return "Text Node";
34            case TIDY_NODETYPE_START: return "Start Node";
35            case TIDY_NODETYPE_END: return "End Node";
36            case TIDY_NODETYPE_STARTEND: return "Start/End Node";
37            case TIDY_NODETYPE_CDATA: return "CDATA Node";
38            case TIDY_NODETYPE_SECTION: return "Section Node";
39            case TIDY_NODETYPE_ASP: return "ASP Source Code Node";
40            case TIDY_NODETYPE_PHP: return "PHP Source Code Node";
41            case TIDY_NODETYPE_JSTE: return "JSTE Source Code";
42            case TIDY_NODETYPE_XMLDECL: return "XML Declaration Node";
43            default: return "Unknown Node";
44        }
45    }
46
47    function do_leaf($string, $indent) {
48        for($i = 0; $i < $indent; $i++) {
49            echo " ";
50        }
51        echo $string;
52    }
53
54    function dump_tree(tidyNode $node, $indent = 0) {
55
56        /* Put something there if the node name is empty */
57        $nodename = trim(strtoupper($node->name));
58        $nodename = (empty($nodename)) ? "[EMPTY]" : $nodename;
59
60        /* Generate the Node, and a pretty name for it */
61        do_leaf(" + $nodename (".node_type($node->type).")\n", $indent);
62
63        /* Check to see if this node is a text node. Text nodes are
64           generated by start/end tags and contain the text in between.
65           i.e. <B>foo</B> will create a text node with $node->value
66           equal to 'foo' */
67        if($node->type == TIDY_NODETYPE_TEXT) {
68            do_leaf("     |\n", $indent);
69            do_leaf("     +---- Value: '{$node->value}'\n", $indent);
70        }
71
72        if(count($node->attribute)) {
73            do_leaf(" |\n", $indent);
74            do_leaf(" +---- Attributes\n", $indent);
75
76            foreach($node->attribute as $name=>$value) {
77                @do_leaf("            +-- $name\n", $indent);
78                do_leaf("             |     +-- Value: $value\n", $indent);
79            }
80        }
81
82        /* Recurse along the children to generate the remaining nodes */
83        if($node->hasChildren()) {
84            foreach($node->child as $child) {
85                dump_tree($child, $indent + 3);
86            }
87        }
88
89    }
90
91
92?>