line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Summary::MediaWiki; |
2
|
1
|
|
|
1
|
|
23173
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
84
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
7
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
83
|
|
5
|
1
|
|
|
1
|
|
1065
|
use HTML::Entities; |
|
1
|
|
|
|
|
7180
|
|
|
1
|
|
|
|
|
1782
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my %TMP_WHITELIST = ( |
10
|
|
|
|
|
|
|
IPA => 1, |
11
|
|
|
|
|
|
|
'Lang-ru' => 1, |
12
|
|
|
|
|
|
|
'Nihongo' => 1, # not sure about this one |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
0
|
|
|
0
|
1
|
|
my($class, %opt) = @_; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
0
|
|
|
|
return bless { |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
20
|
|
|
|
|
|
|
url => $opt{url} || croak("No URL provided"), |
21
|
|
|
|
|
|
|
get => $opt{get} || croak("No get callback provided"), |
22
|
|
|
|
|
|
|
redirect_limit => $opt{redirect_limit} || 5, |
23
|
|
|
|
|
|
|
approx_length => $opt{approx_length} || 200, |
24
|
|
|
|
|
|
|
template_whitelist => {%TMP_WHITELIST, $opt{template_whitelist}}, |
25
|
|
|
|
|
|
|
}, $class; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub get { |
29
|
0
|
|
|
0
|
1
|
|
my($self, $name, $redirects) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
0
|
|
|
|
return "Too many redirects" if defined @$redirects && |
32
|
|
|
|
|
|
|
@$redirects > $self->{redirect_limit}; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$name = _name($name); |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $text = $self->{get}->($self->{url} . $name); |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
|
return "" unless $text =~ /\w/; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
return $self->format($text, $name, $redirects); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _name { |
44
|
0
|
|
|
0
|
|
|
my $name = ucfirst shift; |
45
|
0
|
|
|
|
|
|
$name =~ s/ /_/g; |
46
|
0
|
|
|
|
|
|
return $name; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub format { |
50
|
0
|
|
|
0
|
1
|
|
my($self, $text, $title, $redirects) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# handle redirects |
53
|
0
|
0
|
|
|
|
|
if($text =~ /^#REDIRECT[ :]*\[\[(.*?)(?:#.*?)?\]\]/i) { |
54
|
0
|
|
|
|
|
|
push @$redirects, $title; |
55
|
0
|
|
|
|
|
|
return $self->get($1, $redirects); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Remove comments and templates (maybe should handle templates..?) |
59
|
0
|
|
|
|
|
|
$text =~ s/{{.*?}}//sg; |
60
|
0
|
|
|
|
|
|
$text =~ s///sg; |
61
|
|
|
|
|
|
|
# Don't want references.. |
62
|
0
|
|
|
|
|
|
$text =~ s/[.*?<\/ref>//sg; ] |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my($line, $maybe) = ("", 0); |
65
|
0
|
|
|
|
|
|
for(split /\n/, $text) { |
66
|
0
|
|
|
|
|
|
s/\r//g; |
67
|
0
|
0
|
|
|
|
|
next if /^\s*$/; |
68
|
|
|
|
|
|
|
|
69
|
0
|
0
|
0
|
|
|
|
if(/^\s*(?:[-_#!\t}{:|<=\[]|\W*$)/ |
|
|
|
0
|
|
|
|
|
70
|
|
|
|
|
|
|
&& (!/^\s*\[\[/ || /\[\[Image:/i)) { |
71
|
0
|
0
|
0
|
|
|
|
if($maybe == 1 && /[#!{}|]/) { $maybe = 0; } |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
next; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
0
|
|
|
|
next if /^\s*\*/ and not $line; # lists in templates, etc. |
76
|
0
|
0
|
|
|
|
|
next if /^\s*\w+\s*=/; # info boxes.. |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
0
|
|
|
|
if($maybe < 1 && /^(?:the\s+)?'/i) { # '''Thing'' is .... |
79
|
0
|
0
|
|
|
|
|
$line = "" if $maybe == 0; |
80
|
0
|
|
|
|
|
|
$maybe = 2; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
if($maybe == 1) { |
|
|
0
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
$maybe++; |
85
|
|
|
|
|
|
|
} elsif($maybe == 0) { |
86
|
0
|
0
|
|
|
|
|
$line = "" if $line; |
87
|
0
|
|
|
|
|
|
$maybe = 1; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
s/\t/ /g; |
91
|
|
|
|
|
|
|
|
92
|
0
|
0
|
0
|
|
|
|
if(/\*/ || $maybe == 3) { |
93
|
0
|
|
|
|
|
|
$maybe = 3; |
94
|
0
|
0
|
|
|
|
|
$line =~ s/,$//, last unless /^\*/; |
95
|
0
|
0
|
|
|
|
|
if(/^\s*\*+\s*\[\[.*?\]\]\s*-(\s*.*?)\.?\s*$/) { |
96
|
0
|
|
|
|
|
|
$line .= "$1,"; |
97
|
|
|
|
|
|
|
}else{ |
98
|
0
|
|
|
|
|
|
/^\s*\*+\s*(.*?)\.?$/; |
99
|
0
|
|
|
|
|
|
my $st = $1; |
100
|
0
|
0
|
|
|
|
|
$line .= ($st =~ /[;:,.]$/ ? " $st" : " $st,"); |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
next; |
103
|
|
|
|
|
|
|
}else{ |
104
|
0
|
0
|
|
|
|
|
$line =~ s/\.$/. / if $line; |
105
|
0
|
|
|
|
|
|
$line .= $_; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
next if length($line) < $self->{approx_length}; |
109
|
0
|
|
|
|
|
|
last; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
if(defined $line) { |
113
|
0
|
|
|
|
|
|
$line =~ s/'''//g; |
114
|
0
|
|
|
|
|
|
$line =~ s/''//g; |
115
|
0
|
|
|
|
|
|
$line =~ s/{{([^|]+)|(.*?)}}/ |
116
|
0
|
0
|
|
|
|
|
exists $self->{template_whitelist}->{_name($1)} ? _tl_fixup($2) : ""/ge; |
117
|
0
|
|
|
|
|
|
$line =~ s/\[\[(.*?)\]\]/_wp_link($1)/ge; |
|
0
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
$line =~ s/\[[^ ]+ (.*?)\]/$1/g; |
119
|
0
|
|
|
|
|
|
$line =~ s/<[^>]+>//g; |
120
|
0
|
|
|
|
|
|
$line =~ s/\{\{(.*?)\}\}//g; |
121
|
0
|
|
|
|
|
|
$line = decode_entities($line); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
if(length($line) > 350) { |
125
|
0
|
|
|
|
|
|
$line = substr($line, 0, 380); |
126
|
0
|
|
|
|
|
|
$line =~ s/ +/ /g; |
127
|
0
|
0
|
|
|
|
|
if(not($line =~ s/^(.{330}[^\.]+\.).*/$1/)) { |
128
|
0
|
|
|
|
|
|
$line =~ s/^(.{345,}\w+)\W.*/$1/; |
129
|
|
|
|
|
|
|
} |
130
|
0
|
|
|
|
|
|
$line =~ s/(?:\.)?\s*$//; |
131
|
0
|
|
|
|
|
|
$line .= "..."; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# fixup places where we've stripped templates |
135
|
0
|
|
|
|
|
|
$line =~ s/\s*,\s*\)/)/g; |
136
|
0
|
|
|
|
|
|
$line =~ s/\(\s*,\s*/(/g; |
137
|
0
|
|
|
|
|
|
$line =~ s/\(\s*\)//g; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# get rid of extra spacing |
140
|
0
|
|
|
|
|
|
$line =~ s/ +/ /g; |
141
|
0
|
|
|
|
|
|
$line =~ s/(^ | $)//g; |
142
|
|
|
|
|
|
|
|
143
|
0
|
0
|
|
|
|
|
return $line, "$self->{url}$title" if wantarray; |
144
|
0
|
|
|
|
|
|
return $line; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub _tl_fixup { |
148
|
0
|
|
|
0
|
|
|
my $name = shift; |
149
|
0
|
|
|
|
|
|
$name =~ s/|/ /g; |
150
|
0
|
|
|
|
|
|
return $name; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub _wp_link { |
154
|
0
|
|
|
0
|
|
|
my $link = shift; |
155
|
0
|
|
|
|
|
|
my $x = index($link, '|'); |
156
|
0
|
0
|
|
|
|
|
return substr($link, $x + 1) if $x != -1; |
157
|
0
|
|
|
|
|
|
return $link |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
1; |
161
|
|
|
|
|
|
|
__END__ |