| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package HTML::PrettyPrinter; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
HTML::PrettyPrinter - generate nice HTML files from HTML syntax trees |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use HTML::TreeBuilder; |
|
10
|
|
|
|
|
|
|
# generate a HTML syntax tree |
|
11
|
|
|
|
|
|
|
my $tree = new HTML::TreeBuilder; |
|
12
|
|
|
|
|
|
|
$tree->parse_file($file_name); |
|
13
|
|
|
|
|
|
|
# modify the tree if you want |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use HTML::PrettyPrinter; |
|
16
|
|
|
|
|
|
|
my $hpp = new HTML::PrettyPrinter ('linelength' => 130, |
|
17
|
|
|
|
|
|
|
'quote_attr' => 1); |
|
18
|
|
|
|
|
|
|
# configure |
|
19
|
|
|
|
|
|
|
$tree->address("0.1.0")->attr(_hpp_indent,0); # for an individual element |
|
20
|
|
|
|
|
|
|
$hpp->set_force_nl(1,qw(body head)); # for tags |
|
21
|
|
|
|
|
|
|
$hpp->set_force_nl(1,qw(@SECTIONS)); # as above |
|
22
|
|
|
|
|
|
|
$hpp->set_nl_inside(0,'default!'); # for all tags |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# format the source |
|
25
|
|
|
|
|
|
|
my $linearray_ref = $hpp->format($tree); |
|
26
|
|
|
|
|
|
|
print @$linearray_ref; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# alternative: print directly to filehandle |
|
29
|
|
|
|
|
|
|
use FileHandle; |
|
30
|
|
|
|
|
|
|
my $fh = new FileHandel ">$filenaem2"; |
|
31
|
|
|
|
|
|
|
if (defined $fh) { |
|
32
|
|
|
|
|
|
|
$hpp->select($fh); |
|
33
|
|
|
|
|
|
|
$hpp->format(); |
|
34
|
|
|
|
|
|
|
undef $fh; |
|
35
|
|
|
|
|
|
|
$hpp->select(undef), |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
HTML::PrettyPrinter produces nicely formatted HTML code from |
|
41
|
|
|
|
|
|
|
a HTML syntax tree. It is especially usefull if the produced HTML file |
|
42
|
|
|
|
|
|
|
shall be read or edited manually afterwards. Various parameters let you |
|
43
|
|
|
|
|
|
|
adapt the output to different styles and requirements. |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
If you don't care how the HTML source looks like as long as it is valid |
|
46
|
|
|
|
|
|
|
and readable by browsers, you should use the F method of |
|
47
|
|
|
|
|
|
|
HTML::Element instead of the pretty printer. It is about five times faster. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
The pretty printer will handle line wrapping, indention and structuring |
|
50
|
|
|
|
|
|
|
by the way the whitespace in the tree is represented in the output. |
|
51
|
|
|
|
|
|
|
Furthermore upper/lowercase markup and markup minimization, quoting of |
|
52
|
|
|
|
|
|
|
attribute values, the encoding of entities and the presence of optional |
|
53
|
|
|
|
|
|
|
end tags are configurable. |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
There are two types of parameters to influence the output, individual |
|
56
|
|
|
|
|
|
|
parameters that are set on a per element and per tag basis and common |
|
57
|
|
|
|
|
|
|
parameters that are set only once for each instance of a pretty printer. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
In order to faciliate the configuration a mechanism to handle tag groups |
|
60
|
|
|
|
|
|
|
is provided. Thus, it is possible to modify a parameter for a group of tags |
|
61
|
|
|
|
|
|
|
(e.g. all known block elements) without writing each tag name explicitly. |
|
62
|
|
|
|
|
|
|
Perhaps the code for tag groups will move to an other Perl module in the |
|
63
|
|
|
|
|
|
|
future. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
For HTML::Elements that require a special treatment like |
|
66
|
|
|
|
|
|
|
, , |