-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmltree
executable file
·174 lines (142 loc) · 3.78 KB
/
htmltree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl
# Print HTML tree
# Copyright (C) 2011 Bruno BEAUFILS <[email protected]>
# This software comes with ABSOLUTELY NO WARRANTY.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation in its version 2.
# See the README or COPYING file for details.
##############################################################################
use strict;
use warnings;
use Getopt::Long;
use HTML::TreeBuilder;
# Documentation message
sub usage {
print <<EOF;
usage: $0 [-a|--attr] [-v|--val]
Print HTML elements tree from HTML chunks read from standard input.
Use -a or --attr to print attributes name list for each element. Use -v or
--val to print values of each attributes (implies --attr).
EOF
}
# Check options
my $elt_only = 0;
my $attr_only = 0;
if (!GetOptions('attr'=>\$elt_only,
'val'=>\$attr_only))
{
usage();
exit(1);
}
$attr_only = ! $attr_only;
$elt_only = ! $elt_only;
$elt_only = 0 if (!$attr_only);
# Prepare the tree
my $tree = HTML::TreeBuilder->new();
$tree->ignore_text(1);
$tree->empty_element_tags(1);
$tree->unbroken_text(1);
# We'd like to clean up before printing it (memory is cheaper than I/O anyway)
my $out = '';
my $prefix = '';
# Parse the HTML from standard input and create the corresponding HTML tree
$tree->parse_file(*STDIN);
$tree->elementify();
# Prepare tree representation
print_element($tree, '');
print_tree($tree, '');
# Delete the tree
$tree->delete;
# Print result
print($out);
exit(0);
##############################################################################
# Print a tree of HTML elements (text segment are not printed)
sub print_tree {
my ($root, $prefix) = @_;
if (!defined($root) ||
!ref($root))
{
return;
}
my @nodes = ($root->content_list());
my $node;
while (@nodes != 1)
{
$node = shift(@nodes);
if (ref($node))
{
print_element($node, "$prefix", 0);
if (!$node->is_empty())
{
print_tree($node, "$prefix| ");
}
}
}
$node = shift(@nodes);
if (ref($node))
{
print_element($node, "$prefix", 1);
if (!$node->is_empty())
{
print_tree($node, "$prefix ");
}
}
}
# Print a given element with attributes list
sub print_element {
my ($node, $prefix, $last) = @_;
my $tag = $node->tag();
if ($tag eq '~comment' ||
$tag eq '~declaration' ||
$tag eq '~pi' ||
$tag eq '~literal')
{
return;
}
my @attrs = ($node->all_external_attr_names());
my $type;
# Set last prefix (just before element name)
if (!defined($last))
{
$type = "";
}
elsif ($last)
{
$type = "`-- ";
}
else
{
$type = "|-- ";
}
# Print element name
$out .= "$prefix$type".$node->tag();
# Print attribute list
if (!$elt_only && @attrs != 0)
{
# Print only attribute list
if ($attr_only)
{
$out .= " (@attrs)";
}
# Print attribute/values list
else
{
# FIXME: try to find an elegant way of presenting attribute by line
# my $c = "|";
# $c = " " if $last;
#
# my $d = " ";
# $d = "|" if defined($last) && !$node->is_empty();
#
# @attrs = map($_ = "\n$prefix"."$c "."$d . $_=".$node->attr($_), @attrs);
# $" = "";
# $out .= "@attrs";
@attrs = map($_ = "$_=\'".$node->attr($_)."'", @attrs);
$out .= " (@attrs)";
}
}
$out .= "\n";
}
##############################################################################