blib/lib/Formatter/HTML/HTML.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Formatter::HTML::HTML;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use HTML::Tidy;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use HTML::TokeParser;
8  
9 use base qw( HTML::Tidy );
10  
11  
12 our $VERSION = '0.97';
13  
14 =head1 NAME
15  
16 Formatter::HTML::HTML - Formatter to clean existing HTML
17  
18 =head1 SYNOPSIS
19  
20 use Formatter::HTML::HTML;
21 my $formatter = Formatter::HTML::HTML->format($data);
22 print $formatter->document;
23 print $formatter->title;
24 my $links = $text->links;
25 print ${$links}[0]->{url};
26  
27 =head1 DESCRIPTION
28  
29 This module will clean the document using L<HTML::Tidy>. It also
30 inherits from that module, so you can use methods of that class. It
31 can also parse and return links and the title (using
32 L<HTML::TokeParser>).
33  
34 =head1 METHODS
35  
36 This module conforms with the L<Formatter> API specification, version 0.95:
37  
38 =over
39  
40 =item C<format($string [, {config_file =E<gt> 'path/to/tidy.cfg'} )>
41  
42 The format function that you call to initialise the formatter. It
43 takes the plain text as a string argument and returns an object of
44 this class.
45  
46 Optionally, you may give a hashref with the full file name of the tidy
47 config. This enables you to have this Formatter return valid XHTML,
48 just set it correctly in the config. Note also that you may break the
49 Formatter by e.g. returning configuring tidy to return just a
50 fragment, and it is your own resonsibility to make sure you don't.
51  
52 =cut
53  
54 sub format {
55 my ($that, $text, $config) = @_;
56 my $class = ref($that) || $that;
57 my $tidy = new HTML::Tidy($config); # In fact, we let it do the hard work
58 my $clean = $tidy->clean($text); # allready. It has to be done anyway.
59 my $self = {
60 _out => $clean,
61 };
62 bless($self, $class);
63 return $self;
64 }
65  
66  
67 =item C<document([$charset])>
68  
69 Will return a full, cleaned and valid HTML document. You may specify
70 an optional C<$charset> parameter. This will include a HTML C<meta>
71 element with the chosen character set. It will still be your
72 responsibility to ensure that the document served is encoded with this
73 character set.
74  
75  
76 =cut
77  
78 sub document {
79 my $self = shift;
80 my $charset = shift;
81 my $cleaned = $self->{_out};
82 if (($charset) && ($cleaned !~ m/charset/)) {
83 $cleaned =~ s|(<head.*?>)|$1\n<meta http-equiv="Content-Type" content="text/html; charset=$charset">|si;
84 }
85 return $cleaned;
86 }
87  
88  
89 =item C<fragment>
90  
91 This will return only the contents of the C<body> element.
92  
93 =cut
94  
95 sub fragment {
96 my $self = shift;
97 if ($self->{_out} =~ m|<body.*?>(.*)</body>|si) {
98 return $1;
99 } else {
100 return $self->{_out}
101 }
102 }
103  
104 =item C<links>
105  
106 Will return all links found the input plain text string as an
107 arrayref. The arrayref will for each element keys url and title, the
108 former containing the URL, the latter the text of the link.
109  
110  
111 =cut
112  
113 sub links {
114 my $self = shift;
115 my @arr;
116 my $p = HTML::TokeParser->new(\$self->{_out});
117  
118 while (my $token = $p->get_tag("a")) {
119 my $url = $token->[1]{href} || "-";
120 my $text = $p->get_trimmed_text("/a");
121 push(@arr, {url => $url, title => $text});
122 }
123 return \@arr;
124 }
125  
126 # Both links and title are taken right from examples in TokeParser!
127 # Nice of them, huh? :-)
128  
129 =item C<title>
130  
131 Will return the title of the document as seen in the HTML C<title>
132 element or undef if none can be found.
133  
134 =cut
135  
136  
137 sub title {
138 my $self = shift;
139 my $p = HTML::TokeParser->new(\$self->{_out});
140  
141 if ($p->get_tag("title")) {
142 return $p->get_trimmed_text;
143 }
144 return undef;
145 }
146  
147  
148 1;
149 __END__
↑ Summary