| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package HTML::Clean::Human; |
|
2
|
2
|
|
|
2
|
|
68619
|
use strict; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
79
|
|
|
3
|
2
|
|
|
2
|
|
14099
|
use LEOCHARRE::Class2; |
|
|
2
|
|
|
|
|
13768
|
|
|
|
2
|
|
|
|
|
296
|
|
|
4
|
|
|
|
|
|
|
__PACKAGE__->make_accessor_setget_ondisk_file('abs_path'); |
|
5
|
|
|
|
|
|
|
#__PACKAGE__->make_constructor; |
|
6
|
|
|
|
|
|
|
__PACKAGE__->make_accessor_setget('errstr','html_original','html_cleaned'); |
|
7
|
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
26
|
use Exporter; |
|
|
2
|
|
|
|
|
12
|
|
|
|
2
|
|
|
|
|
69
|
|
|
9
|
2
|
|
|
2
|
|
12
|
use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION @ISA); |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
857
|
|
|
10
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)/g; |
|
11
|
|
|
|
|
|
|
@ISA = qw/Exporter/; |
|
12
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
|
13
|
|
|
|
|
|
|
fix_whitespace |
|
14
|
|
|
|
|
|
|
rip_tag |
|
15
|
|
|
|
|
|
|
rip_tables |
|
16
|
|
|
|
|
|
|
rip_lists |
|
17
|
|
|
|
|
|
|
rip_fonts |
|
18
|
|
|
|
|
|
|
rip_formatting |
|
19
|
|
|
|
|
|
|
rip_headers |
|
20
|
|
|
|
|
|
|
rip_comments |
|
21
|
|
|
|
|
|
|
rip_forms |
|
22
|
|
|
|
|
|
|
); |
|
23
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _read { |
|
28
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
|
29
|
1
|
50
|
0
|
|
|
15
|
my $abs_path = $self->abs_path |
|
30
|
|
|
|
|
|
|
or $self->errstr("no abs_path set") and return; |
|
31
|
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
18
|
local $/; |
|
33
|
1
|
50
|
0
|
|
|
80
|
open(FILE,'<',$abs_path) |
|
34
|
|
|
|
|
|
|
or $self->errstr("Cant open $abs_path for reading, $!") and return; |
|
35
|
1
|
|
|
|
|
114
|
my $text = ; |
|
36
|
1
|
|
|
|
|
11
|
close FILE; |
|
37
|
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
7
|
$self->html_original($text); |
|
39
|
1
|
|
|
|
|
15
|
$self->html_cleaned($text); |
|
40
|
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
11
|
return $text; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
|
45
|
1
|
|
|
1
|
0
|
16
|
my $class = shift; |
|
46
|
1
|
|
|
|
|
4
|
my $self = {}; |
|
47
|
1
|
|
|
|
|
3
|
bless $self, $class; |
|
48
|
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
4
|
while ( my $arg = shift @_ ){ |
|
50
|
1
|
50
|
|
|
|
5
|
defined $arg or next; |
|
51
|
1
|
50
|
|
|
|
6
|
if( ref $arg eq 'HASH'){ |
|
52
|
0
|
|
|
|
|
0
|
$self = $arg; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
|
|
|
|
|
|
else { |
|
55
|
1
|
|
|
|
|
13
|
$self->{abs_path} = $arg; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
5
|
$self->_read; |
|
60
|
1
|
|
|
|
|
2
|
return $self; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub clean { |
|
64
|
1
|
|
|
1
|
0
|
13746
|
my $self = shift; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
#all |
|
67
|
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
7
|
my $html = $self->html_cleaned; |
|
69
|
|
|
|
|
|
|
|
|
70
|
2
|
|
|
2
|
|
12
|
no strict 'refs'; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
2070
|
|
|
71
|
1
|
|
|
|
|
11
|
for my $sub ( qw(rip_headers rip_tables rip_lists rip_fonts rip_formatting fix_whitespace) ){ |
|
72
|
6
|
|
|
|
|
33
|
$html = &$sub($html); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
1
|
|
|
|
|
11
|
return $self->html_cleaned($html); |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=pod |
|
79
|
|
|
|
|
|
|
for my $methname( qw() ){ |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $subname = __PACKAGE__."::$methname"; |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
*{__PACKAGE__."\::$methname |
|
84
|
|
|
|
|
|
|
sub { |
|
85
|
|
|
|
|
|
|
my $self = shift; |
|
86
|
|
|
|
|
|
|
return $self->html_cleaned( $subname($self->html_cleaned) ); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
=cut |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# NON OO |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# takes a tag and rips all out, including with atts |
|
102
|
|
|
|
|
|
|
sub rip_tag { |
|
103
|
18
|
|
|
18
|
1
|
8659
|
my $html = shift; |
|
104
|
|
|
|
|
|
|
|
|
105
|
18
|
|
|
|
|
39
|
for my $tag ( @_ ){ |
|
106
|
37
|
50
|
|
|
|
103
|
defined $tag or next; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# no atts |
|
109
|
37
|
|
|
|
|
2576
|
$html=~s/<$tag>|<\/$tag>/ /sig; |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# with atts |
|
112
|
37
|
|
|
|
|
996
|
$html=~s/<$tag [^<>]+>/ /sig; |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# without endtag |
|
115
|
37
|
|
|
|
|
637
|
$html=~s/<$tag +\/>/ /sig; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
18
|
|
|
|
|
67
|
return $html; |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub _rip_chunk { # needs to be refined, careful using this |
|
122
|
0
|
|
|
0
|
|
0
|
my $html = shift; |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
0
|
for my $tag ( @_ ){ |
|
125
|
0
|
0
|
|
|
|
0
|
defined $tag or next; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# no atts |
|
129
|
0
|
|
|
|
|
0
|
$html=~s/<$tag><\/$tag>/ /sig; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# with atts |
|
132
|
0
|
|
|
|
|
0
|
$html=~s/<$tag [^<>]+>/ /sig; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# without endtag |
|
135
|
0
|
|
|
|
|
0
|
$html=~s/<$tag +\/>/ /sig; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
0
|
return $html; |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub rip_forms { |
|
145
|
0
|
|
|
0
|
1
|
0
|
my $html = shift; |
|
146
|
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
0
|
$html = rip_tag($html,qw(input option form textarea checkbox)); |
|
148
|
0
|
|
|
|
|
0
|
return $html; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub fix_whitespace { # in question |
|
152
|
2
|
|
|
2
|
1
|
543
|
my $html = shift; |
|
153
|
|
|
|
|
|
|
|
|
154
|
2
|
|
|
|
|
241
|
$html=~s/\t| / /sig; |
|
155
|
|
|
|
|
|
|
|
|
156
|
2
|
|
|
|
|
387
|
$html=~s/([\w\,])\s+([a-z])/$1 $2/sg; # no linebreaks between words |
|
157
|
|
|
|
|
|
|
|
|
158
|
2
|
|
|
|
|
9
|
$html=~s/([,])\s+([a-z])/$1 $2/sig; # no linebreaks between word chars |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
#$html=~s/(\w)\s{2,}(\w)/$1 $2/sig; # no more then x whitespace between word chars |
|
162
|
|
|
|
|
|
|
|
|
163
|
2
|
|
|
|
|
51
|
$html=~s/(\S) {2,}/$1 /sig; |
|
164
|
|
|
|
|
|
|
|
|
165
|
2
|
|
|
|
|
42
|
$html=~s/\n[ \t]+/\n/g; |
|
166
|
|
|
|
|
|
|
|
|
167
|
2
|
|
|
|
|
22
|
$html=~s/\n\s+\n\s*/\n\n/g; |
|
168
|
|
|
|
|
|
|
|
|
169
|
2
|
|
|
|
|
10
|
return $html; |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub rip_tables { |
|
174
|
1
|
|
|
1
|
1
|
7
|
return rip_tag(+shift, qw(table tr td tbody)); |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub rip_fonts { |
|
178
|
1
|
|
|
1
|
1
|
35
|
return rip_tag(+shift, qw(font i b ul strike em strong cite u)); |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub rip_lists { |
|
182
|
1
|
|
|
1
|
1
|
5
|
return rip_tag(+shift, qw(ul ol li)); |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub rip_formatting { |
|
186
|
1
|
|
|
1
|
1
|
592
|
return rip_tag(+shift, qw(div br hr p span blockquote center)); |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub rip_headers { #tags |
|
191
|
1
|
|
|
1
|
1
|
3
|
my $html = shift; |
|
192
|
1
|
|
|
|
|
6
|
$html = rip_tag($html,qw(html)); |
|
193
|
|
|
|
|
|
|
|
|
194
|
1
|
|
|
|
|
58181
|
$html=~s/.+]*>//is; |
|
195
|
1
|
|
|
|
|
17
|
$html=~s/<\/body>//i; |
|
196
|
|
|
|
|
|
|
|
|
197
|
1
|
|
|
|
|
11
|
return $html; |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub rip_comments { |
|
202
|
0
|
|
|
0
|
1
|
|
my $html = shift; |
|
203
|
0
|
|
|
|
|
|
$html=~s/\<\!\-\-[^<>]+\-\-\>//sg; |
|
204
|
0
|
|
|
|
|
|
return $html; |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
|
|
207
|
0
|
|
|
0
|
1
|
|
sub rip_javascript {} |
|
208
|
0
|
|
|
0
|
1
|
|
sub rip_styles {} |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
1; |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub headings2text { |
|
223
|
0
|
|
|
0
|
1
|
|
my $html = shift; |
|
224
|
0
|
|
|
|
|
|
my $change = $html; |
|
225
|
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
HEADING: while( $html=~/]*>([^<>]+)<\/h\1>/i ){ |
|
227
|
0
|
|
|
|
|
|
my($h,$text) = ($1,$2); |
|
228
|
|
|
|
|
|
|
|
|
229
|
0
|
|
|
|
|
|
my $text_altered; |
|
230
|
0
|
0
|
|
|
|
|
if($h == 1 ){ |
|
231
|
0
|
|
|
|
|
|
$text_altered = uc($text); |
|
232
|
0
|
|
|
|
|
|
$change=~s/]*>$text<\/h$h>/\n$text_altered\n/sig; |
|
233
|
0
|
|
|
|
|
|
next HEADING; |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
else { |
|
237
|
0
|
|
|
|
|
|
$text_altered = lc($text); |
|
238
|
0
|
|
|
|
|
|
$text_altered=~tr/\b[a-z]/[A-Z]/; |
|
239
|
|
|
|
|
|
|
|
|
240
|
0
|
|
|
|
|
|
$change=~s/]*>$text<\/h$h>/\n$text_altered\n/sig; |
|
241
|
0
|
|
|
|
|
|
next HEADING; |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
} |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
} |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# turn to CAPITALIZED and turn To Cap First Letter |
|
250
|
0
|
|
|
|
|
|
return $change; |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
} |
|
253
|
|
|
|
|
|
|
# inverse.. |
|
254
|
|
|
|
|
|
|
#sub text2headings {} |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
|