line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LEOCHARRE::HTML::Text; |
2
|
2
|
|
|
2
|
|
28487
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
145
|
|
3
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
53
|
|
4
|
2
|
|
|
2
|
|
10
|
use Exporter; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
73
|
|
5
|
2
|
|
|
2
|
|
1559
|
use LEOCHARRE::DEBUG; |
|
2
|
|
|
|
|
3849
|
|
|
2
|
|
|
|
|
11
|
|
6
|
2
|
|
|
2
|
|
208
|
use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2045
|
|
7
|
|
|
|
|
|
|
@ISA = qw/Exporter/; |
8
|
|
|
|
|
|
|
@EXPORT_OK = qw/html2txt slurp_url/; |
9
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
10
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)/g; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
*_slurp_url = \&_slurp_url_safe_w32; |
13
|
|
|
|
|
|
|
*slurp_url = \&_slurp_url_safe_w32; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# proc api |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub html2txt { |
21
|
1
|
|
|
1
|
1
|
10
|
my $arg = shift; |
22
|
1
|
50
|
0
|
|
|
4
|
$arg or carp("Missing argument.") and return; |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
8
|
debug("arg $arg"); |
25
|
1
|
50
|
0
|
|
|
249
|
my $_type = _source_type($arg) |
26
|
|
|
|
|
|
|
or carp("Don't understand the source type, is it html, url, or file???") and return; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
7
|
debug("arg $arg, type:$_type \n"); |
29
|
|
|
|
|
|
|
|
30
|
1
|
0
|
0
|
|
|
105
|
my $html = |
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$_type eq 'file' ? _slurp_file($arg) : |
32
|
|
|
|
|
|
|
$_type eq 'url' ? _slurp_url($arg) : |
33
|
|
|
|
|
|
|
$_type eq 'html' ? $arg : ( warn("Don't know what to do with arg '$arg'\n") and return ); |
34
|
|
|
|
|
|
|
|
35
|
1
|
50
|
0
|
|
|
4
|
$html or warn("Could not get html.\n") and return; |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
6
|
debug("length ".length($html)); |
38
|
|
|
|
|
|
|
|
39
|
1
|
50
|
0
|
|
|
97
|
my $text = _generate_text_from_html($html) or warn("Could not generate text from html.\n") and return; |
40
|
1
|
|
|
|
|
11
|
$text; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# subs |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _source_type { |
49
|
1
|
|
|
1
|
|
3
|
my $arg = shift; |
50
|
1
|
50
|
|
|
|
31
|
-f $arg and return 'file'; |
51
|
0
|
0
|
0
|
|
|
0
|
$arg=~/^http|\.com$|\.net$|\.org$/ and $arg!~/\s/ and ( length $arg < 300 ) and return 'url'; |
|
|
|
0
|
|
|
|
|
52
|
0
|
0
|
|
|
|
0
|
$arg=~/\ and return 'html'; |
53
|
0
|
|
|
|
|
0
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _slurp_url_linux { |
57
|
1
|
|
|
1
|
|
7
|
my $arg = shift; |
58
|
1
|
|
|
|
|
21
|
require File::Which; |
59
|
1
|
50
|
|
|
|
11
|
my $bin = File::Which::which('wget') or die("Missing 'wget' from system.\n"); |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
2048061
|
my $out = `$bin -q -O - '$arg'`; |
62
|
1
|
50
|
33
|
|
|
37
|
$? and carp("Something went wrong with wget, $?\n") and return; |
63
|
1
|
50
|
0
|
|
|
15
|
$out or carp("It seems '$arg' produces no output.\n") and return; |
64
|
1
|
|
|
|
|
55
|
return $out; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# THIS IS A MODIFICATION THAT WORKS ON BOTH WIN32 AND LINUX (ONE CHANGE ONLY) |
69
|
|
|
|
|
|
|
# contributed by Gordon Van Amburg |
70
|
|
|
|
|
|
|
sub _slurp_url_safe_w32{ |
71
|
3
|
|
|
3
|
|
18
|
my $arg = shift; |
72
|
3
|
|
|
|
|
940
|
require File::Which; |
73
|
3
|
50
|
|
|
|
1032
|
my $bin = File::Which::which('wget') or die("Missing 'wget' from system.\n"); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
#REMOVING SINGLE QUOTES AROUND $arg STOPS FAILURE IN WINDOWS VISTA USING GNUWIN32 wget. |
76
|
3
|
|
|
|
|
5996319
|
my $out = `$bin -q -O - $arg`; |
77
|
3
|
50
|
33
|
|
|
116
|
$? and carp("Something went wrong with wget, $?\n") and return; |
78
|
3
|
50
|
0
|
|
|
18
|
$out or carp("It seems '$arg' produces no output.\n") and return; |
79
|
3
|
|
|
|
|
1478
|
return $out; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _slurp_file { |
84
|
1
|
|
|
1
|
|
3
|
my $arg = shift; |
85
|
1
|
|
|
|
|
4
|
local $/; |
86
|
1
|
50
|
|
|
|
43
|
open(FILE,'<',$arg) or die("Can't open $arg, $!\n"); |
87
|
1
|
|
|
|
|
210
|
my $out = ; |
88
|
1
|
|
|
|
|
11
|
close FILE; |
89
|
1
|
50
|
0
|
|
|
3
|
$out or carp("It seems '$arg' produces no output.\n") and return; |
90
|
1
|
|
|
|
|
7
|
return $out; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# at this point we should be sure already that the argument is html |
98
|
|
|
|
|
|
|
sub _generate_text_from_html { |
99
|
1
|
|
|
1
|
|
2
|
my $html = shift; |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
|
|
7
|
debug(length $html); |
102
|
|
|
|
|
|
|
# rip out scripts |
103
|
1
|
|
|
|
|
778
|
require LEOCHARRE::HTML::Rip; |
104
|
1
|
|
|
|
|
6
|
$html = LEOCHARRE::HTML::Rip::rip_tag($html,'script'); |
105
|
1
|
|
|
|
|
8
|
debug(length $html); |
106
|
1
|
|
|
|
|
155
|
$html = LEOCHARRE::HTML::Rip::rip_tag($html,'style'); |
107
|
1
|
|
|
|
|
10
|
debug(length $html); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
2946
|
require HTML::Entities; |
111
|
|
|
|
|
|
|
|
112
|
1
|
|
|
|
|
10266
|
$html= HTML::Entities::decode($html); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# $html=~/
115
|
1
|
|
|
|
|
414
|
$html=~s/<\/p>/\n/isg; |
116
|
1
|
|
|
|
|
1712
|
$html=~s/<[^<>]+>/ /sg; |
117
|
1
|
|
|
|
|
1060
|
$html=~s/ {2,}/ /g; |
118
|
|
|
|
|
|
|
|
119
|
1
|
|
|
|
|
1264
|
$html=~s/(\w)\s+(\w)/$1 $2/sig; |
120
|
|
|
|
|
|
|
|
121
|
1
|
|
|
|
|
311
|
$html=~s/\n[\t ]{2,}/\n /g; |
122
|
1
|
|
|
|
|
156
|
$html=~s/\n /\n/g; |
123
|
1
|
|
|
|
|
155
|
$html=~s/[\n\r]{2,}/\n/g; |
124
|
1
|
|
|
|
|
7190
|
$html=~s/^\s+|\s+$//g; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
1
|
|
|
|
|
151
|
$html=~s/\s{5,}/\n\n/gs; |
128
|
|
|
|
|
|
|
|
129
|
1
|
|
|
|
|
24
|
$html; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |