blib/lib/WWW/FetchStory/Fetcher.pm

filter: Statementstmt 7.5% 51 / 675 Branchbran 0.0% 0 / 250 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 30.1% 16 / 53 Podpod 100% 35 / 35 totaltotal 9.1% 102 / 1111 CC 158 SCAR 48.5
CC
158
Cov
5%
init
306.0
fetch
272.0
derive_values
272.0
CRAP 127.6
« blib/lib/WWW/FetchStory/Fetcher/FanfictionNet.pm ↑ Summary blib/lib/HTTP/Cookies/Wget.pm »
1 package WWW::FetchStory::Fetcher;
2 2 $WWW::FetchStory::Fetcher::VERSION = '0.2001';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 =head1 NAME
6  
7 WWW::FetchStory::Fetcher - fetching module for WWW::FetchStory
8  
9 =head1 VERSION
10  
11 version 0.2001
12  
13 =head1 DESCRIPTION
14  
15 This is the base class for story-fetching plugins for WWW::FetchStory.
16  
17 =cut
18  
19 2 require File::Temp;
20 2 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Encode::ZapCP1252;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use HTML::Strip;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use HTML::Tidy::libXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use EBook::EPUB;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use WWW::Mechanize::Sleepy;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use HTTP::Cookies::Wget;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use HTTP::Cookies::Mozilla;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 $obj->WWW::FetchStory::Fetcher->new();
40  
41 =cut
42  
43 sub new {
44 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
45 0 my %parameters = @_;
46 0 ▶ my $self = bless ({%parameters}, ref ($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
47  
48 0 return ($self);
49 } # new
50  
51 =head2 init
52  
53 Initialize the object.
54  
55 $obj->init(%args)
56  
57 =cut
58  
59 sub init {
60 0 ▶ my $self = shift;
Subroutine init: not called CC=17 SCAR=57.2
Pod documented
61 0 my %parameters = @_;
62  
63 0 foreach my $key (keys %parameters)
64 {
65 0 $self->{$key} = $parameters{$key};
66 }
67  
68 0 ▶ if ($self->{use_wget})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_wget'}) { } 0 0 0
69 {
70 0 $self->{wget_cmd} = 'wget';
71 0 ▶ if ($self->{wget_cookies} and -f $self->{wget_cookies})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"wget_cookies"} and -f $self->{"wget_cookies"}) 0 0 0
Condition0%0/3
$self->{"wget_cookies"} and -f $self->{"wget_cookies"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"wget_cookies"} and -f $self->{"wget_cookies"}
$self->{"wget_cookies"} -f $self->{"wget_cookies"}
Truth table0%0/3
A and B
A= $self->{"wget_cookies"}B= -f $self->{"wget_cookies"}
ABresult
0X0
100
111
72 {
73 0 $self->{wget_cmd} .= " --load-cookies " . $self->{wget_cookies};
74 }
75 0 ▶ if ($self->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
76 {
77 0 $self->{wget_cmd} .= " --debug";
78 }
79 0 ▶ if ($self->{wget_options})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"wget_options"}) 0 0 0
80 {
81 0 $self->{wget_cmd} .= ' ' . $self->{wget_options};
82 }
83 }
84 else
85 {
86 0 $self->{user_agent} = WWW::Mechanize::Sleepy->new(
87 keep_alive => 1,
88 env_proxy => 1,
89 sleep => '1..10',
90 agent => ref $self,
91 );
92 0 $self->{user_agent}->show_progress($self->{verbose} > 0);
93 0 ▶ if ($self->{firefox_cookies} and -f $self->{firefox_cookies})
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'firefox_cookies'} and -f $self->{'firefox_cookies'}) { } 0 0 0
elsif ($self->{'wget_cookies'} and -f $self->{'wget_cookies'}) { } 0 0 0
Condition0%0/3
$self->{"firefox_cookies"} and -f $self->{"firefox_cookies"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"firefox_cookies"} and -f $self->{"firefox_cookies"}
$self->{"firefox_cookies"} -f $self->{"firefox_cookies"}
Truth table0%0/3
A and B
A= $self->{"firefox_cookies"}B= -f $self->{"firefox_cookies"}
ABresult
0X0
100
111
94 {
95 my $cookies = HTTP::Cookies::Mozilla->new(
96 'file' => $self->{firefox_cookies},
97 0 hide_cookie2 => 1,
98 ignore_discard => 1,
99 );
100 0 ▶ print "\n--------------\n", $cookies->as_string, "\n------------\n" if ($self->{debug} && $self->{debug} > 2);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} and $self->{"debug"} > 2 0 0 0
Condition0%0/3
$self->{"debug"} and $self->{"debug"} > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"debug"} and $self->{"debug"} > 2
$self->{"debug"} $self->{"debug"} > 2
Truth table0%0/3
A and B
A= $self->{"debug"}B= $self->{"debug"} > 2
ABresult
0X0
100
111
101 0 $self->{user_agent}->cookie_jar( $cookies );
102 }
103 ▶ elsif ($self->{wget_cookies} and -f $self->{wget_cookies})
Condition0%0/3
$self->{"wget_cookies"} and -f $self->{"wget_cookies"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"wget_cookies"} and -f $self->{"wget_cookies"}
$self->{"wget_cookies"} -f $self->{"wget_cookies"}
Truth table0%0/3
A and B
A= $self->{"wget_cookies"}B= -f $self->{"wget_cookies"}
ABresult
0X0
100
111
104 {
105 my $cookies = HTTP::Cookies::Wget->new(
106 'file' => $self->{wget_cookies},
107 0 hide_cookie2 => 1,
108 ignore_discard => 1,
109 );
110 0 ▶ print "\n--------------\n", $cookies->as_string, "\n------------\n" if ($self->{debug} && $self->{debug} > 2);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} and $self->{"debug"} > 2 0 0 0
Condition0%0/3
$self->{"debug"} and $self->{"debug"} > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"debug"} and $self->{"debug"} > 2
$self->{"debug"} $self->{"debug"} > 2
Truth table0%0/3
A and B
A= $self->{"debug"}B= $self->{"debug"} > 2
ABresult
0X0
100
111
111 0 $self->{user_agent}->cookie_jar( $cookies );
112 }
113 0 ▶ if ($self->{debug} && $self->{debug} > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} and $self->{"debug"} > 1) 0 0 0
Condition0%0/3
$self->{"debug"} and $self->{"debug"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"debug"} and $self->{"debug"} > 1
$self->{"debug"} $self->{"debug"} > 1
Truth table0%0/3
A and B
A= $self->{"debug"}B= $self->{"debug"} > 1
ABresult
0X0
100
111
114 {
115 0 ▶ $self->{user_agent}->add_handler("request_send", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 0 ▶ $self->{user_agent}->add_handler("response_done", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 }
118 }
119  
120 0 $self->{stripper} = HTML::Strip->new();
121 0 $self->{stripper}->add_striptag("head");
122  
123 0 return ($self);
124 } # init
125  
126 =head2 name
127  
128 The name of the fetcher; this is basically the last component
129 of the module name. This works as either a class function or a method.
130  
131 $name = $self->name();
132  
133 $name = WWW::FetchStory::Fetcher::name($class);
134  
135 =cut
136  
137 sub name {
138 0 ▶ my $class = shift;
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
139
140 0 ▶ my $fullname = (ref ($class) ? ref ($class) : $class);
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
141  
142 0 my @bits = split('::', $fullname);
143 0 return pop @bits;
144 } # name
145  
146 =head2 info
147  
148 Information about the fetcher.
149 By default this just returns the formatted name.
150  
151 $info = $self->info();
152  
153 =cut
154  
155 sub info {
156 0 ▶ my $self = shift;
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
157
158 0 my $name = $self->name();
159  
160 # split the name into words
161 0 my $info = $name;
162 0 $info =~ s/([A-Z])/ $1/g;
163 0 $info =~ s/^\s+//;
164  
165 0 return $info;
166 } # info
167  
168 =head2 priority
169  
170 The priority of this fetcher. Fetchers with higher priority
171 get tried first. This is useful where there may be a generic
172 fetcher for a particular site, and then a more specialized fetcher
173 for particular sections of a site. For example, there may be a
174 generic LiveJournal fetcher, and then refinements for particular
175 LiveJournal community, such as the sshg_exchange community.
176 This works as either a class function or a method.
177  
178 This must be overridden by the specific fetcher class.
179  
180 $priority = $self->priority();
181  
182 $priority = WWW::FetchStory::Fetcher::priority($class);
183  
184 =cut
185  
186 sub priority {
187 0 ▶ my $class = shift;
Subroutine priority: not called CC=1 SCAR=6.9
Pod documented
188 0 return 0;
189 } # priority
190  
191 =head2 allow
192  
193 If this fetcher can be used for the given URL, then this returns
194 true.
195 This must be overridden by the specific fetcher class.
196  
197 if ($obj->allow($url))
198 {
199 ....
200 }
201  
202 =cut
203  
204 sub allow {
205 0 ▶ my $self = shift;
Subroutine allow: not called CC=1 SCAR=6.9
Pod documented
206 0 my $url = shift;
207  
208 0 return 0;
209 } # allow
210  
211 =head2 fetch
212  
213 Fetch the story, with the given options.
214  
215 %story_info = $obj->fetch(
216 urls=>\@urls,
217 basename=>$basename,
218 toc=>0,
219 yaml=>0);
220  
221 =over
222  
223 =item basename
224  
225 Optional basename used to construct the filenames.
226 If this is not given, the basename is derived from the title of the story.
227  
228 =item epub
229  
230 Create an EPUB file, deleting the HTML files which have been downloaded.
231  
232 =item toc
233  
234 Build a table-of-contents file if this is true.
235  
236 =item yaml
237  
238 Build a YAML file with meta-data about this story if this is true.
239  
240 =item urls
241  
242 The URLs of the story.
243 The first page is scraped for meta-information about the story,
244 including the title and author. Site-specific Fetcher plugins can find additional
245 information, including the URLs of all the chapters in a multi-chapter story.
246  
247 =back
248  
249 =cut
250  
251 sub fetch {
252 0 ▶ my $self = shift;
Subroutine fetch: not called CC=16 SCAR=56.1
Pod documented
253 0 my %args = (
254 urls=>undef,
255 basename=>'',
256 @_
257 );
258  
259 0 $self->{verbose} = $args{verbose};
260  
261 0 my $first_url = $args{urls}[0];
262 0 my $toc_content = $self->get_toc($first_url);
263 0 my %story_info = $self->parse_toc(%args, content=>$toc_content,
264 url=>$first_url);
265  
266 my $basename = ($args{basename}
267 ? $args{basename}
268 0 ▶ : $self->get_story_basename($story_info{title}));
Branch: 1 branch
BranchTrueFalseTotal
$args{'basename'} ? : 0 0 0
269 0 $story_info{basename} = $basename;
270 0 my @storyfiles = ();
271  
272 0 ▶ if ($args{epub} and exists $story_info{epub_url} and $story_info{epub_url})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'epub'} and exists $story_info{'epub_url'} and $story_info{'epub_url'}) { } 0 0 0
Condition0%0/6
$args{"epub"} and exists $story_info{"epub_url"} and $story_info{"epub_url"}
!ll&&!rl&&r
000
$args{"epub"} and exists $story_info{"epub_url"}
!ll&&!rl&&r
000
MC/DC0%0/3
$args{"epub"} and exists $story_info{"epub_url"} and $story_info{"epub_url"}
$args{"epub"} exists $story_info{"epub_url"} $story_info{"epub_url"}
Truth table0%0/4
A and B and C
A= $args{"epub"}B= exists $story_info{"epub_url"}C= $story_info{"epub_url"}
ABCresult
0XX0
10X0
1100
1111
273 {
274 my %epub_info = $self->get_epub(base=>$basename,
275 url=>$story_info{epub_url},
276 0 meta=>\%story_info);
277 0 $story_info{storyfiles} = [$epub_info{filename}];
278  
279 0 $self->derive_values(info=>\%story_info);
280 0 ▶ warn Dump(\%story_info) if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
281 }
282 else
283 {
284 0 my @ch_urls = @{$story_info{chapters}};
285 0 my $one_chapter = (@ch_urls == 1);
286 my $first_chapter_is_toc =
287 0 ▶ $story_info{toc_first} || $self->{first_is_toc};
Condition0%0/3
$story_info{'toc_first'} || $self->{'first_is_toc'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$story_info{'toc_first'} || $self->{'first_is_toc'}
$story_info{'toc_first'} $self->{'first_is_toc'}
Truth table0%0/3
A || B
A= $story_info{'toc_first'}B= $self->{'first_is_toc'}
ABresult
1X1
011
000
288 0 delete $story_info{toc_first};
289 0 my @ch_titles = ();
290 0 my @ch_wc = ();
291 0 ▶ my $count = (($one_chapter or $first_chapter_is_toc) ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$one_chapter || $first_chapter_is_toc ? : 0 0 0
Condition0%0/3
$one_chapter or $first_chapter_is_toc
l!l&&r!l&&!r
000
MC/DC0%0/2
$one_chapter or $first_chapter_is_toc
$one_chapter $first_chapter_is_toc
Truth table0%0/3
A or B
A= $one_chapterB= $first_chapter_is_toc
ABresult
1X1
011
000
292 0 ▶ foreach (my $i = 0; $i < @ch_urls; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ch_urls) 0 0 0
293 {
294 0 my $ch_title = sprintf("%s (%d)", $story_info{title}, $i+1);
295 0 my %ch_info = $self->get_chapter(base=>$basename,
296 count=>$count,
297 url=>$ch_urls[$i],
298 title=>$ch_title);
299 0 push @storyfiles, $ch_info{filename};
300 0 push @ch_titles, $ch_info{title};
301 0 push @ch_wc, $ch_info{wordcount};
302 0 $story_info{wordcount} += $ch_info{wordcount};
303 0 $count++;
304 0 sleep 1; # try not to overload the archive
305 }
306 0 $self->derive_values(info=>\%story_info);
307  
308 0 ▶ warn Dump(\%story_info) if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
309  
310 0 $story_info{storyfiles} = \@storyfiles;
311 0 $story_info{chapter_titles} = \@ch_titles;
312 0 $story_info{chapter_wc} = \@ch_wc;
313 0 ▶ if ($args{toc} and !$args{epub}) # build a table-of-contents
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"toc"} and not $args{"epub"}) 0 0 0
Condition0%0/3
$args{"toc"} and not $args{"epub"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"toc"} and not $args{"epub"}
$args{"toc"} not $args{"epub"}
Truth table0%0/3
A and not(B)
A= $args{"toc"}B= not $args{"epub"}
ABresult
0X0
100
111
314 {
315 0 my $toc = $self->build_toc(info=>\%story_info);
316 0 unshift @{$story_info{storyfiles}}, $toc;
317 0 unshift @{$story_info{chapter_titles}}, "Table of Contents";
318 }
319 0 ▶ if ($args{epub})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"epub"}) 0 0 0
320 {
321 0 my $epub_file = $self->build_epub(info=>\%story_info);
322 # if we have built an EPUB file, then the storyfiles
323 # are now just one EPUB file.
324 0 $story_info{storyfiles} = [$epub_file];
325 }
326 }
327 0 ▶ if ($args{yaml})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"yaml"}) 0 0 0
328 {
329 0 my $filename = sprintf("%s.yml", $story_info{basename});
330 0 my $ofh;
331 0 ▶ open($ofh, ">", $filename) || die "Can't write to $filename";
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, ">", $filename or die "Can't write to $filename" 0 0 0
332 0 print $ofh Dump(\%story_info);
333 0 close($ofh);
334 }
335  
336 0 return %story_info;
337 } # fetch
338  
339 =head1 Private Methods
340  
341 =head2 get_story_basename
342  
343 Figure out the file basename for a story by using its title.
344  
345 $basename = $self->get_story_basename($title);
346  
347 =cut
348 sub get_story_basename {
349 0 ▶ my $self = shift;
Subroutine get_story_basename: not called CC=9 SCAR=45.0
Pod documented
350 0 my $title = shift;
351  
352 # make a word with only letters and numbers
353 # and remove HTML entities and UTF-8
354 # and with everything lowercase
355 # and the spaces replaced with underscores
356 0 my $base = $title;
357 0 $base =~ s/^The\s+//; # get rid of leading "The "
358 0 $base =~ s/^A\s+//; # get rid of leading "A "
359 0 $base =~ s/^An\s+//; # get rid of leading "An "
360 0 $base =~ s/-/ /g; # replace dashes with spaces
361 0 $base = decode_entities($base); # replace entities with UTF-8
362 0 $base =~ s/[^[:ascii:]]//g; # remove UTF-8
363 0 $base =~ s/[^\w\s]//g; # remove non-word characters
364 0 $base = lc($base);
365  
366 0 my @words = split(' ', $base);
367 0 my $max_words = 3;
368 0 my @first_words = ();
369 # if there are three words or less, use all of them
370 0 ▶ if (@words <= $max_words)
Branch: 1 branch
BranchTrueFalseTotal
if (@words <= $max_words) { } 0 0 0
371 {
372 0 @first_words = @words;
373 }
374 else
375 {
376 0 ▶ $max_words++ if (@words > 3); # four
Branch: 1 branch
BranchTrueFalseTotal
if @words > 3 0 0 0
377 0 ▶ $max_words++ if (@words > 5); # five if a lot
Branch: 1 branch
BranchTrueFalseTotal
if @words > 5 0 0 0
378 0 ▶ for (my $i = 0; $i < @words and @first_words < $max_words; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @words and @first_words < $max_words) 0 0 0
Condition0%0/3
$i < @words and @first_words < $max_words
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @words and @first_words < $max_words
$i < @words @first_words < $max_words
Truth table0%0/3
A and B
A= $i < @wordsB= @first_words < $max_words
ABresult
0X0
100
111
379 {
380 # skip little words
381 0 ▶ if ($words[$i] =~ /^(the|a|an|and)$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($words[$i] =~ /^(the|a|an|and)$/) { } 0 0 0
elsif (@words > 4 and $words[$i] =~ /^(of|to|in|or|on|by|i|is|isnt|its)$/) { } 0 0 0
382 {
383 }
384 ▶ elsif (@words > 4 and $words[$i] =~ /^(of|to|in|or|on|by|i|is|isnt|its)$/)
Condition0%0/3
@words > 4 and $words[$i] =~ /^(of|to|in|or|on|by|i|is|isnt|its)$/
!ll&&!rl&&r
000
MC/DC0%0/2
@words > 4 and $words[$i] =~ /^(of|to|in|or|on|by|i|is|isnt|its)$/
@words > 4 $words[$i] =~ /^(of|to|in|or|on|by|i|is|isnt|its)$/
Truth table0%0/3
A and B
A= @words > 4B= $words[$i] =~ /^(of|to|in|or|on|by|i|is|isnt|its)$/
ABresult
0X0
100
111
385 {
386 # if there are a lot of words, skip these little words too
387 }
388 else
389 {
390 0 push @first_words, $words[$i];
391 }
392 }
393 }
394  
395 0 return join('_', @first_words);
396  
397 } # get_story_basename
398  
399 =head2 extract_story
400  
401 Extract the story-content from the fetched content.
402  
403 my ($story, $title) = $self->extract_story(content=>$content,
404 title=>$title);
405  
406 =cut
407  
408 sub extract_story {
409 0 ▶ my $self = shift;
Subroutine extract_story: not called CC=6 SCAR=37.4
Pod documented
410 0 my %args = (
411 content=>'',
412 title=>'',
413 @_
414 );
415  
416 0 my $story = '';
417 0 my $title = '';
418 0 ▶ if ($args{content} =~ m#<title>([^<]+)</title>#is)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'content'} =~ m[<title>([^<]+)</title>]is) { } 0 0 0
419 {
420 0 $title = $1;
421 }
422 else
423 {
424 0 $title = $args{title};
425 }
426  
427 # some badly formed pages have multiple BODY tags
428 0 ▶ if ($args{content} =~ m#<body[^>]*>.*?<body[^>]*>(.*?)</body>#is)
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'content'} =~ m[<body[^>]*>.*?<body[^>]*>(.*?)</body>]is) { } 0 0 0
elsif ($args{'content'} =~ m[<body[^>]*>(.*)</body>]is) { } 0 0 0
elsif ($args{'content'} =~ m[</head>(.*)]is) { } 0 0 0
429 {
430 0 $story = $1;
431 }
432 elsif ($args{content} =~ m#<body[^>]*>(.*)</body>#is)
433 {
434 0 $story = $1;
435 }
436 elsif ($args{content} =~ m#</head>(.*)#is)
437 {
438 0 $story = $1;
439 }
440  
441 0 ▶ if ($story)
Branch: 1 branch
BranchTrueFalseTotal
if ($story) { } 0 0 0
442 {
443 0 $story = $self->tidy_chars($story);
444 }
445 else
446 {
447 0 $story = $args{content};
448 }
449  
450 0 return ($story, $title);
451  
452 } # extract_story
453  
454 =head2 make_css
455  
456 Create site-specific CSS styling.
457  
458 $css = $self->make_css();
459  
460 =cut
461  
462 sub make_css {
463 0 ▶ my $self = shift;
Subroutine make_css: not called CC=1 SCAR=6.9
Pod documented
464  
465 0 return '';
466 } # make_css
467  
468 =head2 tidy
469  
470 Make a tidy, compliant XHTML page from the given story-content.
471  
472 $content = $self->tidy(story=>$story,
473 title=>$title);
474  
475 =cut
476  
477 sub tidy {
478 0 ▶ my $self = shift;
Subroutine tidy: not called CC=2 SCAR=17.9
Pod documented
479 0 my %args = (
480 story=>'',
481 title=>'',
482 @_
483 );
484  
485 0 my $story = $args{story};
486 0 $story = $self->tidy_chars($story);
487 0 my $title = $args{title};
488 0 my $css = $self->make_css(%args);
489  
490 0 my $html = '';
491 0 $html .= "<html>\n";
492 0 $html .= "<head>\n";
493 0 $html .= "<title>$title</title>\n";
494 0 ▶ $html .= $css if $css;
Branch: 1 branch
BranchTrueFalseTotal
if $css 0 0 0
495 0 $html .= "</head>\n";
496 0 $html .= "<body>\n";
497 0 $html .= "$story\n";
498 0 $html .= "</body>\n";
499 0 $html .= "</html>\n";
500  
501 0 my $tidy = HTML::Tidy::libXML->new();
502 0 $html = encode("UTF-8", $html);
503 0 my $xhtml = $tidy->clean($html, 'UTF-8', 1);
504  
505 # fixing some errors
506 0 $xhtml =~ s!xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"!xmlns="http://www.w3.org/1999/xhtml"!;
507 0 $xhtml =~ s!<i/>!!g;
508 0 $xhtml =~ s!<b/>!!g;
509  
510 0 return $xhtml;
511 } # tidy
512  
513 =head2 get_toc
514  
515 Get a table-of-contents page.
516  
517 =cut
518 sub get_toc {
519 0 ▶ my $self = shift;
Subroutine get_toc: not called CC=1 SCAR=6.9
Pod documented
520 0 my $url = shift;
521  
522 0 return $self->get_page($url);
523 } # get_toc
524  
525 =head2 get_page
526  
527 Get the contents of a URL.
528  
529 =cut
530  
531 sub get_page {
532 0 ▶ my $self = shift;
Subroutine get_page: not called CC=12 SCAR=50.5
Pod documented
533 0 my $url = shift;
534  
535 0 ▶ warn "getting $url\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
536 0 my $content = '';
537  
538 0 ▶ if ($self->{use_wget})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_wget'}) { } 0 0 0
539 {
540 0 my $cmd = sprintf("%s -O %s '%s'", $self->{wget_cmd}, '-', $url);
541 0 ▶ warn "$cmd\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
542 0 my $ifh;
543 0 ▶ open($ifh, "${cmd}|") or die "FAILED $cmd: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $ifh, "$cmd|" or die "FAILED ${cmd}: $!" 0 0 0
544 0 ▶ while(<$ifh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 0 0 0
545 {
546 0 $content .= $_;
547 }
548 0 close($ifh);
549 }
550 else
551 {
552 0 my $can_accept = HTTP::Message::decodable;
553 0 my $res = $self->{user_agent}->get($url,
554 'Accept-Encoding' => $can_accept,
555 'Keep-Alive' => "300",
556 'Connection' => 'keep-alive',
557 );
558  
559 # Check the outcome of the response
560 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
561 0 ▶ print $res->status_line, "\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
562 }
563 else {
564 0 die "FAILED fetching $url ", $res->status_line;
565 }
566 0 ▶ $content = $res->decoded_content || $res->content;
Condition0%0/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table0%0/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
567 }
568  
569 0 ▶ if (!$content and $self->{verbose})
Branch: 1 branch
BranchTrueFalseTotal
if (not $content and $self->{"verbose"}) 0 0 0
Condition0%0/3
not $content and $self->{"verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $content and $self->{"verbose"}
not $content $self->{"verbose"}
Truth table0%0/3
not(A) and B
A= not $contentB= $self->{"verbose"}
ABresult
0X0
100
111
570 {
571 0 warn "No content from $url";
572 0 ▶ if ($self->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
573 {
574 # there's a problem, we want to debug it
575 0 exit;
576 }
577 }
578  
579 0 return $content;
580 } # get_page
581  
582 =head2 parse_toc
583  
584 Parse the table-of-contents file.
585  
586 This must be overridden by the specific fetcher class.
587  
588 %info = $self->parse_toc(content=>$content,
589 url=>$url,
590 urls=>\@urls);
591  
592 This should return a hash containing:
593  
594 =over
595  
596 =item chapters
597  
598 An array of URLs for the chapters of the story. In the case where the
599 story only takes one page, that will be the chapter.
600 In the case where multiple URLs have been passed in, it will be those URLs.
601  
602 =item title
603  
604 The title of the story.
605  
606 =back
607  
608 It may also return additional information, such as Summary.
609  
610 =cut
611  
612 sub parse_toc {
613 0 ▶ my $self = shift;
Subroutine parse_toc: not called CC=1 SCAR=6.9
Pod documented
614 0 my %args = (
615 url=>'',
616 content=>'',
617 @_
618 );
619  
620 0 my %info = ();
621 0 $info{url} = $args{url};
622 0 $info{title} = $self->parse_title(%args);
623 0 $info{author} = $self->parse_author(%args);
624 0 $info{summary} = $self->parse_summary(%args);
625 0 $info{characters} = $self->parse_characters(%args);
626 0 $info{universe} = $self->parse_universe(%args);
627 0 $info{category} = $self->parse_category(%args);
628 0 $info{rating} = $self->parse_rating(%args);
629 0 $info{chapters} = $self->parse_chapter_urls(%args);
630 0 $info{epub_url} = $self->parse_epub_url(%args);
631  
632 0 return %info;
633 } # parse_toc
634  
635 =head2 parse_chapter_urls
636  
637 Figure out the URLs for the chapters of this story.
638  
639 =cut
640 sub parse_chapter_urls {
641 0 ▶ my $self = shift;
Subroutine parse_chapter_urls: not called CC=2 SCAR=17.9
Pod documented
642 0 my %args = (
643 url=>'',
644 content=>'',
645 @_
646 );
647  
648 0 my @chapters = ();
649 0 ▶ if (defined $args{urls})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'urls'}) { } 0 0 0
650 {
651 0 @chapters = @{$args{urls}};
652 }
653 else
654 {
655 0 @chapters = ($args{url});
656 }
657  
658 0 return \@chapters;
659 } # parse_chapter_urls
660  
661 =head2 parse_epub_url
662  
663 Figure out the URL for the EPUB version of this story, if there is one.
664  
665 =cut
666 sub parse_epub_url {
667 0 ▶ my $self = shift;
Subroutine parse_epub_url: not called CC=1 SCAR=6.9
Pod documented
668 0 my %args = (
669 url=>'',
670 content=>'',
671 @_
672 );
673  
674 0 return undef;
675 } # parse_epub_url
676  
677 =head2 parse_title
678  
679 Get the title from the content
680  
681 =cut
682 sub parse_title {
683 0 ▶ my $self = shift;
Subroutine parse_title: not called CC=9 SCAR=45.0
Pod documented
684 0 my %args = (
685 content=>'',
686 @_
687 );
688  
689 0 my $content = $args{content};
690 0 my $title = '';
691 0 ▶ if ($content =~ /<(?:b|strong)>Title:?\s*<\/(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|\/p|div|\/div)/si)
Branch: 8 branches
BranchTrueFalseTotal
if ($content =~ m[<(?:b|strong)>Title:?\s*</(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|/p|div|/div)]is) { } 0 0 0
elsif ($content =~ /\bTitle:\s*"?(.*?)"?\s*<br/s) { } 0 0 0
elsif ($content =~ m[<h1>([^<]+)</h1>]is) { } 0 0 0
elsif ($content =~ m[<p class=MsoTitle>([^<]+)</p>]is) { } 0 0 0
elsif ($content =~ m[<h2>([^<]+)</h2>]is) { } 0 0 0
elsif ($content =~ m[<h3>([^<]+)</h3>]is) { } 0 0 0
elsif ($content =~ m[<h4>([^<]+)</h4>]is) { } 0 0 0
elsif ($content =~ m[<title>([^<]+)</title>]is) { } 0 0 0
692 {
693 0 $title = $1;
694 }
695 elsif ($content =~ /\bTitle:\s*"?(.*?)"?\s*<br/s)
696 {
697 0 $title = $1;
698 }
699 elsif ($content =~ m#<h1>([^<]+)</h1>#is)
700 {
701 0 $title = $1;
702 }
703 elsif ($content =~ m#<p class=MsoTitle>([^<]+)</p>#is)
704 {
705 0 $title = $1;
706 }
707 elsif ($content =~ m#<h2>([^<]+)</h2>#is)
708 {
709 0 $title = $1;
710 }
711 elsif ($content =~ m#<h3>([^<]+)</h3>#is)
712 {
713 0 $title = $1;
714 }
715 elsif ($content =~ m#<h4>([^<]+)</h4>#is)
716 {
717 0 $title = $1;
718 }
719 elsif ($content =~ m#<title>([^<]+)</title>#is)
720 {
721 0 $title = $1;
722 }
723 0 $title =~ s/<u>//ig;
724 0 $title =~ s/<\/u>//ig;
725 0 return $title;
726 } # parse_title
727  
728 =head2 parse_ch_title
729  
730 Get the chapter title from the content
731  
732 =cut
733 sub parse_ch_title {
734 0 ▶ my $self = shift;
Subroutine parse_ch_title: not called CC=2 SCAR=17.9
Pod documented
735 0 my %args = (
736 content=>'',
737 @_
738 );
739  
740 0 my $content = $args{content};
741 0 my $title = '';
742 0 ▶ if ($content =~ /Chapter \d+[:.]?\s*([^<]+)/si)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /Chapter \d+[:.]?\s*([^<]+)/is) { } 0 0 0
743 {
744 0 $title = $1;
745 }
746 else
747 {
748 0 $title = $self->parse_title(%args);
749 }
750 0 $title =~ s/<u>//ig;
751 0 $title =~ s/<\/u>//ig;
752 0 return $title;
753 } # parse_ch_title
754  
755 =head2 parse_author
756  
757 Get the author from the content
758  
759 =cut
760 sub parse_author {
761 0 ▶ my $self = shift;
Subroutine parse_author: not called CC=5 SCAR=34.0
Pod documented
762 0 my %args = (
763 content=>'',
764 @_
765 );
766  
767 0 my $content = $args{content};
768 0 my $author = '';
769 0 ▶ if ($content =~ /<(?:b|strong)>Author:?\s*<\/(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|\/p|div|\/div)/si)
Branch: 4 branches
BranchTrueFalseTotal
if ($content =~ m[<(?:b|strong)>Author:?\s*</(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|/p|div|/div)]is) { } 0 0 0
elsif ($content =~ /\bAuthor:\s*"?(.*?)"?\s*<br/is) { } 0 0 0
elsif ($content =~ /<meta name="author" content="(.*?)"/is) { } 0 0 0
elsif ($content =~ /<p>by (.*?)<br/is) { } 0 0 0
770 {
771 0 $author = $1;
772 }
773 elsif ($content =~ /\bAuthor:\s*"?(.*?)"?\s*<br/si)
774 {
775 0 $author = $1;
776 }
777 elsif ($content =~ /<meta name="author" content="(.*?)"/si)
778 {
779 0 $author = $1;
780 }
781 elsif ($content =~ /<p>by (.*?)<br/si)
782 {
783 0 $author = $1;
784 }
785 0 return $author;
786 } # parse_author
787  
788 =head2 parse_summary
789  
790 Get the summary from the content
791  
792 =cut
793 sub parse_summary {
794 0 ▶ my $self = shift;
Subroutine parse_summary: not called CC=10 SCAR=47.0
Pod documented
795 0 my %args = (
796 content=>'',
797 @_
798 );
799  
800 0 my $content = $args{content};
801 0 my $summary = '';
802 0 ▶ if ($content =~ /<(?:b|strong)>Summary:?\s*<\/(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|\/p|div|\/div)/si)
Branch: 9 branches
BranchTrueFalseTotal
if ($content =~ m[<(?:b|strong)>Summary:?\s*</(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|/p|div|/div)]is) { } 0 0 0
elsif ($content =~ m[<i>Summary:</i>\s*([^<]+)\s*<br>]s) { } 0 0 0
elsif ($content =~ m[>Summary:\s*</span>\s*([^<]+)\s*<br]s) { } 0 0 0
elsif ($content =~ m[<i>Summary:</i>\s*(.*?)\s*$]m) { } 0 0 0
elsif ($content =~ m[<tr><(?:th|td)>Summary</(?:th|td)><td>(.*?)</td></tr>]s) { } 0 0 0
elsif ($content =~ m[\bSummary:\s*"?(.*?)"?\s*<(?:br|p|/p|div|/div)]is) { } 0 0 0
elsif ($content =~ m[(?:Prompt|Summary):</b>([^<]+)]is) { } 0 0 0
elsif ($content =~ m[(?:Prompt|Summary):</strong>([^<]+)]is) { } 0 0 0
elsif ($content =~ m[(?:Prompt|Summary):</u>([^<]+)]is) { } 0 0 0
803 {
804 0 $summary = $1;
805 }
806 elsif ($content =~ m#<i>Summary:</i>\s*([^<]+)\s*<br>#s)
807 {
808 0 $summary = $1;
809 }
810 elsif ($content =~ m#>Summary:\s*</span>\s*([^<]+)\s*<br#s)
811 {
812 0 $summary = $1;
813 }
814 elsif ($content =~ /<i>Summary:<\/i>\s*(.*?)\s*$/m)
815 {
816 0 $summary = $1;
817 }
818 elsif ($content =~ m#<tr><(?:th|td)>Summary</(?:th|td)><td>(.*?)</td></tr>#s)
819 {
820 0 $summary = $1;
821 0 $summary =~ s/<br>/ /g;
822 }
823 elsif ($content =~ /\bSummary:\s*"?(.*?)"?\s*<(?:br|p|\/p|div|\/div)/si)
824 {
825 0 $summary = $1;
826 }
827 elsif ($content =~ m#(?:Prompt|Summary):</b>([^<]+)#is)
828 {
829 0 $summary = $1;
830 }
831 elsif ($content =~ m#(?:Prompt|Summary):</strong>([^<]+)#is)
832 {
833 0 $summary = $1;
834 }
835 elsif ($content =~ m#(?:Prompt|Summary):</u>([^<]+)#is)
836 {
837 0 $summary = $1;
838 }
839 0 return $summary;
840 } # parse_summary
841  
842 =head2 parse_characters
843  
844 Get the characters from the content
845  
846 =cut
847 sub parse_characters {
848 0 ▶ my $self = shift;
Subroutine parse_characters: not called CC=6 SCAR=37.4
Pod documented
849 0 my %args = (
850 content=>'',
851 @_
852 );
853  
854 0 my $content = $args{content};
855 0 my $characters = '';
856 0 ▶ if ($content =~ />Characters:?\s*<\/(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|\/p|div|\/div)/si)
Branch: 5 branches
BranchTrueFalseTotal
if ($content =~ m[>Characters:?\s*</(?:b|strong)>:?\s*"?(.*?)"?\s*<(?:br|p|/p|div|/div)]is) { } 0 0 0
elsif ($content =~ /\bCharacters:\s*"?(.*?)"?\s*<br/is) { } 0 0 0
elsif ($content =~ m[<i>Characters:</i>\s*([^<]+)\s*<br>]s) { } 0 0 0
elsif ($content =~ m[(?:Pairings?|Characters):</(?:b|strong|u)>\s*([^<]+)]is) { } 0 0 0
elsif ($content =~ m[<tr><(?:th|td)>(?:Pairings?|Characters)</(?:th|td)><td>(.*?)</td></tr>]s) { } 0 0 0
857 {
858 0 $characters = $1;
859 }
860 elsif ($content =~ /\bCharacters:\s*"?(.*?)"?\s*<br/si)
861 {
862 0 $characters = $1;
863 }
864 elsif ($content =~ m#<i>Characters:</i>\s*([^<]+)\s*<br>#s)
865 {
866 0 $characters = $1;
867 }
868 elsif ($content =~ m#(?:Pairings?|Characters):</(?:b|strong|u)>\s*([^<]+)#is)
869 {
870 0 $characters = $1;
871 }
872 elsif ($content =~ m#<tr><(?:th|td)>(?:Pairings?|Characters)</(?:th|td)><td>(.*?)</td></tr>#s)
873 {
874 0 $characters = $1;
875 0 $characters =~ s/<br>/, /g;
876 }
877 0 return $characters;
878 } # parse_characters
879  
880 =head2 parse_universe
881  
882 Get the universe/fandom from the content
883  
884 =cut
885 sub parse_universe {
886 0 ▶ my $self = shift;
Subroutine parse_universe: not called CC=2 SCAR=17.9
Pod documented
887 0 my %args = (
888 content=>'',
889 @_
890 );
891  
892 0 my $content = $args{content};
893 0 my $universe = '';
894 0 ▶ if ($content =~ m#(?:Universe|Fandom):</(?:b|strong|u)>([^<]+)#is)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[(?:Universe|Fandom):</(?:b|strong|u)>([^<]+)]is) 0 0 0
895 {
896 0 $universe = $1;
897 }
898 0 return $universe;
899 } # parse_universe
900  
901 =head2 parse_recipient
902  
903 Get the recipient from the content
904  
905 =cut
906 sub parse_recipient {
907 0 ▶ my $self = shift;
Subroutine parse_recipient: not called CC=3 SCAR=24.8
Pod documented
908 0 my %args = (
909 content=>'',
910 @_
911 );
912  
913 0 my $content = $args{content};
914 0 my $recipient = '';
915 0 ▶ if ($content =~ m#(?:Recipient|Prompter): (\w+)#is)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /(?:Recipient|Prompter): (\w+)/is) { } 0 0 0
elsif ($content =~ m[Recipient:</(?:b|strong|u)>([^<]+)]is) { } 0 0 0
916 {
917 0 $recipient = $1;
918 }
919 elsif ($content =~ m#Recipient:</(?:b|strong|u)>([^<]+)#is)
920 {
921 0 $recipient = $1;
922 }
923 0 return $recipient;
924 } # parse_recipient
925  
926 =head2 parse_category
927  
928 Get the categories from the content
929  
930 =cut
931 sub parse_category {
932 0 ▶ my $self = shift;
Subroutine parse_category: not called CC=3 SCAR=24.8
Pod documented
933 0 my %args = (
934 content=>'',
935 @_
936 );
937  
938 0 my $content = $args{content};
939 0 my $category = '';
940 0 ▶ if ($content =~ m#(?:Category|Tags):</(?:b|strong|u)>([^<]+)#is)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[(?:Category|Tags):</(?:b|strong|u)>([^<]+)]is) { } 0 0 0
elsif ($content =~ m[<tr><(?:th|td)>Categories</(?:th|td)><td>(.*?)</td></tr>]s) { } 0 0 0
941 {
942 0 $category = $1;
943 }
944 elsif ($content =~ m#<tr><(?:th|td)>Categories</(?:th|td)><td>(.*?)</td></tr>#s)
945 {
946 0 $category = $1;
947 0 $category =~ s/<br>/, /g;
948 }
949 0 return $category;
950 } # parse_category
951  
952 =head2 parse_rating
953  
954 Get the rating from the content
955  
956 =cut
957 sub parse_rating {
958 0 ▶ my $self = shift;
Subroutine parse_rating: not called CC=3 SCAR=24.8
Pod documented
959 0 my %args = (
960 content=>'',
961 @_
962 );
963  
964 0 my $content = $args{content};
965 0 my $rating = '';
966 0 ▶ if ($content =~ m!^Rating:\s(.*?)$!m)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /^Rating:\s(.*?)$/m) { } 0 0 0
elsif ($content =~ m[Rating:</(?:b|strong|u)>\s*([^<]+)]is) { } 0 0 0
967 {
968 0 $rating = $1;
969 }
970 elsif ($content =~ m#Rating:</(?:b|strong|u)>\s*([^<]+)#is)
971 {
972 0 $rating = $1;
973 }
974 0 return $rating;
975 } # parse_rating
976  
977 =head2 derive_values
978  
979 Calculate additional Meta values, such as current date.
980  
981 =cut
982 sub derive_values {
983 0 ▶ my $self = shift;
Subroutine derive_values: not called CC=16 SCAR=56.1
Pod documented
984 0 my %args = @_;
985  
986 0 my $today = time2str('%Y-%m-%d', time);
987 0 $args{info}->{fetch_date} = $today;
988  
989 0 my $words = $args{info}->{wordcount};
990 0 ▶ if ($words)
Branch: 1 branch
BranchTrueFalseTotal
if ($words) 0 0 0
991 {
992 0 my $len = '';
993 0 ▶ if ($words == 100)
Branch: 9 branches
BranchTrueFalseTotal
if ($words == 100) { } 0 0 0
elsif ($words == 200) { } 0 0 0
elsif ($words >= 75000) { } 0 0 0
elsif ($words >= 50000) { } 0 0 0
elsif ($words >= 25000) { } 0 0 0
elsif ($words >= 7500) { } 0 0 0
elsif ($words >= 2000) { } 0 0 0
elsif ($words > 500) { } 0 0 0
elsif ($words <= 500) { } 0 0 0
994 {
995 0 $len = 'Drabble';
996 } elsif ($words == 200)
997 {
998 0 $len = 'Double Drabble';
999 } elsif ($words >= 75000)
1000 {
1001 0 $len = 'Long Novel';
1002 } elsif ($words >= 50000)
1003 {
1004 0 $len = 'Novel';
1005 } elsif ($words >= 25000)
1006 {
1007 0 $len = 'Novella';
1008 } elsif ($words >= 7500)
1009 {
1010 0 $len = 'Novelette';
1011 } elsif ($words >= 2000)
1012 {
1013 0 $len = 'Short Story';
1014 } elsif ($words > 500)
1015 {
1016 0 $len = 'Short Short';
1017 } elsif ($words <= 500)
1018 {
1019 0 $len = 'Flash';
1020 }
1021 0 ▶ $args{info}->{story_length} = $len if $len;
Branch: 1 branch
BranchTrueFalseTotal
if $len 0 0 0
1022 }
1023 0 for my $field (qw{characters universe category})
1024 {
1025 0 ▶ if (exists $args{info}->{$field}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"info"}{$field} and defined $args{"info"}{$field} and $args{"info"}{$field} =~ /,/s) 0 0 0
Condition0%0/6
exists $args{"info"}{$field} and defined $args{"info"}{$field} and $args{"info"}{$field} =~ /,/s
!ll&&!rl&&r
000
exists $args{"info"}{$field} and defined $args{"info"}{$field}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $args{"info"}{$field} and defined $args{"info"}{$field} and $args{"info"}{$field} =~ /,/s
exists $args{"info"}{$field} defined $args{"info"}{$field} $args{"info"}{$field} =~ /,/s
Truth table0%0/4
A and B and C
A= exists $args{"info"}{$field}B= defined $args{"info"}{$field}C= $args{"info"}{$field} =~ /,/s
ABCresult
0XX0
10X0
1100
1111
1026 and defined $args{info}->{$field}
1027 and $args{info}->{$field} =~ /,/s)
1028 {
1029 0 my @chars = split(/,\s*/s, $args{info}->{$field});
1030 0 $args{info}->{$field} = \@chars;
1031 }
1032 }
1033 } # derive_values
1034  
1035 =head2 get_chapter
1036  
1037 Get an individual chapter of the story, tidy it,
1038 and save it to a file.
1039  
1040 $filename = $obj->get_chapter(base=>$basename,
1041 count=>$count,
1042 url=>$url,
1043 title=>$title);
1044  
1045 =cut
1046  
1047 sub get_chapter {
1048 0 ▶ my $self = shift;
Subroutine get_chapter: not called CC=4 SCAR=30.0
Pod documented
1049 0 my %args = (
1050 base=>'',
1051 count=>0,
1052 url=>'',
1053 title=>'',
1054 @_
1055 );
1056  
1057 0 my $content = $self->get_page($args{url});
1058  
1059 0 my ($story, $title) = $self->extract_story(%args, content=>$content);
1060  
1061 0 my $chapter_title = $self->parse_ch_title(content=>$content);
1062 0 ▶ $chapter_title = $title if !$chapter_title;
Branch: 1 branch
BranchTrueFalseTotal
unless $chapter_title 0 0 0
1063  
1064 0 my $html = $self->tidy(story=>$story, title=>$chapter_title);
1065  
1066 0 my %wc = $self->wordcount(content=>$html);
1067  
1068 #
1069 # Write the file
1070 #
1071 my $filename = ($args{count}
1072 ? sprintf("%s%02d.html", $args{base}, $args{count})
1073 0 ▶ : sprintf("%s.html", $args{base}));
Branch: 1 branch
BranchTrueFalseTotal
$args{'count'} ? : 0 0 0
1074 0 my $ofh;
1075 0 ▶ open($ofh, ">", $filename) || die "Can't write to $filename";
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, ">", $filename or die "Can't write to $filename" 0 0 0
1076 0 print $ofh $html;
1077 0 close($ofh);
1078  
1079 return (
1080 filename=>$filename,
1081 title=>$chapter_title,
1082 wordcount=>$wc{words},
1083 charcount=>$wc{chars},
1084 0 );
1085 } # get_chapter
1086  
1087 =head2 get_epub
1088  
1089 Get the EPUB version of the story, tidy it,
1090 and save it to a file.
1091  
1092 $filename = $obj->get_epub(base=>$basename,
1093 url=>$url);
1094  
1095 =cut
1096  
1097 sub get_epub {
1098 0 ▶ my $self = shift;
Subroutine get_epub: not called CC=9 SCAR=45.0
Pod documented
1099 0 my %args = (
1100 base=>'',
1101 url=>'',
1102 meta=>undef,
1103 @_
1104 );
1105  
1106 0 my %meta = %{$args{meta}};
1107 0 my $content = $self->get_page($args{url});
1108 0 my %epub_info = ();
1109  
1110 #
1111 # Write the file
1112 #
1113 0 my $filename = $args{base} . '.epub';
1114 0 my $ofh;
1115 0 ▶ open($ofh, ">", $filename) || die "Can't write to $filename";
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, ">", $filename or die "Can't write to $filename" 0 0 0
1116 0 print $ofh $content;
1117 0 close($ofh);
1118  
1119 0 $epub_info{filename} = $filename;
1120  
1121 #
1122 # Update the file metadata
1123 #
1124 0 my $zip = Archive::Zip->new();
1125 0 my $status = $zip->read( $filename );
1126 0 ▶ if ($status != AZ_OK)
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 0 0 0
1127 {
1128 0 return %epub_info;
1129 }
1130 0 my @members = $zip->membersMatching('.*\.opf');
1131 0 ▶ if (@members && $members[0])
Branch: 1 branch
BranchTrueFalseTotal
if (@members and $members[0]) 0 0 0
Condition0%0/3
@members and $members[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@members and $members[0]
@members $members[0]
Truth table0%0/3
A and B
A= @membersB= $members[0]
ABresult
0X0
100
111
1132 {
1133 0 my %values = ();
1134 0 my $opf = $zip->contents($members[0]);
1135 0 my $dom = XML::LibXML->load_xml(string => $opf,
1136 load_ext_dtd => 0,
1137 no_network => 1);
1138 0 my @metanodes = $dom->getElementsByLocalName('metadata');
1139 0 foreach my $metanode (@metanodes)
1140 {
1141 0 ▶ if ($metanode->hasChildNodes)
Branch: 1 branch
BranchTrueFalseTotal
if ($metanode->hasChildNodes) 0 0 0
1142 {
1143 0 my @children = $metanode->childNodes();
1144 0 foreach my $node (@children)
1145 {
1146 0 $self->epub_parse_one_node(%args,
1147 node=>$node,
1148 values=>\%values);
1149 }
1150 }
1151 }
1152 0 ▶ print STDERR "get_epub: about to replace description\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1153 0 $self->epub_replace_description(description=>$meta{summary}, xml=>$dom);
1154 # remove meta info we don't want to be added to this
1155 0 delete $meta{description};
1156 0 delete $meta{title};
1157 0 delete $meta{chapters};
1158 0 delete $meta{epub_url};
1159 0 delete $meta{basename};
1160 0 delete $meta{toc_first};
1161 0 $self->epub_add_meta(meta=>\%meta, xml=>$dom);
1162  
1163 0 my $str = $dom->toString;
1164 0 $zip->contents($members[0], $str);
1165 0 $zip->overwrite();
1166 }
1167  
1168 0 return %epub_info;
1169 } # get_epub
1170  
1171 =head2 epub_replace_description
1172  
1173 Replace or add the description to an EPUB file.
1174  
1175 =cut
1176 sub epub_replace_description {
1177 0 ▶ my $self = shift;
Subroutine epub_replace_description: not called CC=4 SCAR=30.0
Pod documented
1178 0 my %args = @_;
1179  
1180 0 my $dom = $args{xml};
1181 0 my $desc = $args{description};
1182 0 ▶ print STDERR "epub_replace_description: description=$desc\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1183 0 my @metanodes = $dom->getElementsByLocalName('metadata');
1184 0 ▶ return unless @metanodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @metanodes 0 0 0
1185 0 my $metanode = $metanodes[0];
1186 0 my @dnodes = $metanode->getElementsByLocalName('description');
1187 0 ▶ if ($dnodes[0])
Branch: 1 branch
BranchTrueFalseTotal
if ($dnodes[0]) 0 0 0
1188 {
1189 0 $metanode->removeChild($dnodes[0]);
1190 }
1191 0 $metanode->appendTextChild('dc:description', $desc);
1192 } # epub_replace_description
1193  
1194 =head2 epub_add_meta
1195  
1196 Add the given meta-data to an EPUB file.
1197  
1198 =cut
1199 sub epub_add_meta {
1200 0 ▶ my $self = shift;
Subroutine epub_add_meta: not called CC=3 SCAR=24.8
Pod documented
1201 0 my %args = @_;
1202  
1203 0 my $dom = $args{xml};
1204 0 my @metanodes = $dom->getElementsByLocalName('metadata');
1205 0 ▶ return unless @metanodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @metanodes 0 0 0
1206 0 my $metanode = $metanodes[0];
1207  
1208 0 my %meta = %{$args{meta}};
1209 0 foreach my $key (sort keys %meta)
1210 {
1211 0 my $chunk=<<EOT;
1212 <meta name="$key" content="$meta{$key}"/>
1213 EOT
1214 0 $metanode->appendWellBalancedChunk( $chunk );
1215 }
1216  
1217 } # epub_add_meta
1218  
1219 =head2 epub_parse_one_node
1220  
1221 Parse a node of meta-information from an EPUB file.
1222  
1223 =cut
1224 sub epub_parse_one_node {
1225 0 ▶ my $self = shift;
Subroutine epub_parse_one_node: not called CC=16 SCAR=56.1
Pod documented
1226 0 my %params = @_;
1227  
1228 0 my $node = $params{node};
1229 0 my $oldvals = $params{values};
1230  
1231 0 my %newvals = ();
1232 0 my $name = $node->localname;
1233 0 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
1234  
1235 0 my $value = $node->textContent;
1236 0 $value =~ s/^\s+//s;
1237 0 $value =~ s/\s+$//s;
1238 0 $value =~ s/\s\s+/ /gs;
1239 0 ▶ if ($name eq 'meta' and $node->hasAttributes)
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'meta' and $node->hasAttributes) { } 0 0 0
elsif ($node->hasAttributes) { } 0 0 0
Condition0%0/3
$name eq "meta" and $node->hasAttributes
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "meta" and $node->hasAttributes
$name eq "meta" $node->hasAttributes
Truth table0%0/3
A and B
A= $name eq "meta"B= $node->hasAttributes
ABresult
0X0
100
111
1240 {
1241 0 my $metaname = '';
1242 0 my $metacontent = '';
1243 0 my @atts = $node->attributes();
1244 0 foreach my $att (@atts)
1245 {
1246 0 my $n = $att->localname;
1247 0 my $v = $att->textContent;
1248 0 $v =~ s/^\s+//s;
1249 0 $v =~ s/\s+$//s;
1250 0 ▶ if ($n eq 'name')
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq 'name') { } 0 0 0
1251 {
1252 0 $metaname = $v;
1253 }
1254 else
1255 {
1256 0 $metacontent = $v;
1257 }
1258 }
1259 0 $newvals{$metaname} = $metacontent;
1260 }
1261 elsif ($node->hasAttributes)
1262 {
1263 0 ▶ $newvals{$name}->{text} = $value unless !$value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
1264 0 my @atts = $node->attributes();
1265 0 foreach my $att (@atts)
1266 {
1267 0 my $n = $att->localname;
1268 0 my $v = $att->textContent;
1269 0 $v =~ s/^\s+//s;
1270 0 $v =~ s/\s+$//s;
1271 0 $newvals{$name}->{$n} = $v;
1272 }
1273 }
1274 else
1275 {
1276 0 $newvals{$name} = $value;
1277 }
1278  
1279 # Don't want to overwrite existing values
1280 0 foreach my $newname (sort keys %newvals)
1281 {
1282 0 my $newval = $newvals{$newname};
1283 0 ▶ if (!ref $newval)
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $newval) { } 0 0 0
1284 {
1285 0 ▶ if (!exists $oldvals->{$newname})
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $oldvals->{$newname}) { } 0 0 0
elsif (not ref $oldvals->{$newname}) { } 0 0 0
elsif (ref $oldvals->{$newname} eq 'ARRAY') { } 0 0 0
1286 {
1287 0 $oldvals->{$newname} = $newval;
1288 }
1289 elsif (!ref $oldvals->{$newname})
1290 {
1291 0 my $v = $oldvals->{$newname};
1292 0 $oldvals->{$newname} = [$v, $newval];
1293 }
1294 elsif (ref $oldvals->{$newname} eq 'ARRAY')
1295 {
1296 0 push @{$oldvals->{$newname}}, $newval;
1297 }
1298 else
1299 {
1300 0 $oldvals->{$newname}->{$newval} = $newval;
1301 }
1302 }
1303 else
1304 {
1305 0 ▶ if (!exists $oldvals->{$newname})
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $oldvals->{$newname}) { } 0 0 0
elsif (ref $oldvals->{$newname} eq 'ARRAY') { } 0 0 0
1306 {
1307 0 $oldvals->{$newname} = $newval;
1308 }
1309 elsif (ref $oldvals->{$newname} eq 'ARRAY')
1310 {
1311 0 push @{$oldvals->{$newname}}, $newval;
1312 }
1313 else
1314 {
1315 0 my $v = $oldvals->{$newname};
1316 0 $oldvals->{$newname} = [$v, $newval];
1317 }
1318 }
1319 }
1320 } # epub_parse_one_node
1321  
1322 =head2 wordcount
1323  
1324 Figure out the word-count.
1325  
1326 =cut
1327 sub wordcount {
1328 0 ▶ my $self = shift;
Subroutine wordcount: not called CC=3 SCAR=24.8
Pod documented
1329 0 my %args = (
1330 @_
1331 );
1332  
1333 #
1334 # Count the words
1335 #
1336 0 my $stripped = $self->{stripper}->parse($args{content});
1337 0 $self->{stripper}->eof;
1338 0 $stripped =~ s/[\n\r]/ /sg; # remove line splits
1339 0 $stripped =~ s/^\s+//;
1340 0 $stripped =~ s/\s+$//;
1341 0 $stripped =~ s/\s+/ /g; # remove excess whitespace
1342 0 my @words = split(' ', $stripped);
1343 0 my $wordcount = @words;
1344 0 my $chars = length($stripped);
1345 0 ▶ if ($self->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
1346 {
1347 0 my $orig_length = length($args{content});
1348 0 print "orig_length=$orig_length, words=$wordcount, chars=$chars\n";
1349 0 ▶ if ($wordcount < 200) # too short!
Branch: 1 branch
BranchTrueFalseTotal
if ($wordcount < 200) 0 0 0
1350 {
1351 0 print "====== stripped ======\n$stripped\n======\n";
1352 }
1353 }
1354 return (
1355 0 words=>$wordcount,
1356 chars=>$chars,
1357 );
1358 } # wordcount
1359  
1360 =head2 build_toc
1361  
1362 Build a local table-of-contents file from the meta-info about the story.
1363  
1364 $self->build_toc(info=>\%info);
1365  
1366 =cut
1367 sub build_toc {
1368 0 ▶ my $self = shift;
Subroutine build_toc: not called CC=5 SCAR=34.0
Pod documented
1369 0 my %args = (
1370 @_
1371 );
1372 0 my $info = $args{info};
1373  
1374 0 my $filename = sprintf("%s00.html", $info->{basename});
1375  
1376 0 my $html;
1377 my $characters = (ref $info->{characters}
1378 0 ? join( ', ', @{$info->{characters}} )
1379 0 ▶ : $info->{characters});
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'characters'} ? : 0 0 0
1380 my $universe = (ref $info->{universe}
1381 0 ? join( ', ', @{$info->{universe}} )
1382 0 ▶ : $info->{universe});
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'universe'} ? : 0 0 0
1383 0 $html = <<EOT;
1384 <html>
1385 <head><title>$info->{title}</title></head>
1386 <body>
1387 <h1>$info->{title}</h1>
1388 <p>by $info->{author}</p>
1389 <p>Fetched from <a href="$info->{url}">$info->{url}</a></p>
1390 <p><b>Summary:</b>
1391 $info->{summary}
1392 </p>
1393 <p><b>Words:</b> $info->{wordcount}<br/>
1394 <b>Universe:</b> $universe</p>
1395 <b>Characters:</b> $characters</p>
1396 <ol>
1397 EOT
1398  
1399 0 my @storyfiles = @{$info->{storyfiles}};
1400 0 my @ch_titles = @{$info->{chapter_titles}};
1401 0 my @ch_wc = @{$info->{chapter_wc}};
1402 0 ▶ for (my $i=0; $i < @storyfiles; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @storyfiles) 0 0 0
1403 {
1404 0 $html .= sprintf("<li><a href=\"%s\">%s</a> (%d)</li>",
1405 $storyfiles[$i],
1406 $ch_titles[$i],
1407 $ch_wc[$i]);
1408 }
1409 0 $html .= "\n</ol>\n</body></html>\n";
1410 0 my $ofh;
1411 0 ▶ open($ofh, ">", $filename) || die "Can't write to $filename";
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, ">", $filename or die "Can't write to $filename" 0 0 0
1412 0 print $ofh $html;
1413 0 close($ofh);
1414  
1415 0 return $filename;
1416 } # build_toc
1417  
1418 =head2 build_epub
1419  
1420 Create an EPUB file from the story files and meta information.
1421  
1422 $self->build_epub()
1423  
1424 =cut
1425 sub build_epub {
1426 0 ▶ my $self = shift;
Subroutine build_epub: not called CC=12 SCAR=50.5
Pod documented
1427 0 my %args = (
1428 @_
1429 );
1430 0 my $info = $args{info};
1431  
1432 0 my $epub = EBook::EPUB->new;
1433 0 $epub->add_title($info->{title});
1434 0 $epub->add_author($info->{author});
1435 0 $epub->add_description($info->{summary});
1436 0 $epub->add_language('en');
1437 0 $epub->add_source($info->{url}, 'URL');
1438 0 $epub->add_date($info->{fetch_date}, 'fetched');
1439  
1440 # Add Subjects and additional Meta
1441 # Also build up the title-page
1442 0 my $info_str =<<EOT;
1443 <h1>$info->{title}</h1>
1444 <p>by $info->{author}</p>
1445 <p><b>Fetched from:</b> $info->{url}</p>
1446 <p><b>Summary:</b> $info->{summary}</p>
1447 <p>
1448 EOT
1449 0 my %know = %{$info};
1450 0 delete $know{title};
1451 0 delete $know{author};
1452 0 delete $know{summary};
1453 0 delete $know{url};
1454 0 delete $know{fetch_date};
1455 0 delete $know{basename};
1456 0 delete $know{chapter_titles};
1457 0 delete $know{chapter_wc};
1458 0 delete $know{chapters};
1459 0 delete $know{storyfiles};
1460 0 foreach my $key (sort keys %know)
1461 {
1462 0 ▶ if (!$know{$key})
Branch: 1 branch
BranchTrueFalseTotal
unless ($know{$key}) 0 0 0
1463 {
1464 0 next;
1465 }
1466 0 ▶ if (!ref $know{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $know{$key}) { } 0 0 0
1467 {
1468 0 $info_str .= sprintf("<b>%s:</b> %s<br/>\n", $key, $know{$key});
1469 0 ▶ if ($know{$key} =~ /,\s*/)
Branch: 1 branch
BranchTrueFalseTotal
if ($know{$key} =~ /,\s*/) { } 0 0 0
1470 {
1471 0 my @array = split(/,\s*/, $know{$key});
1472 0 foreach my $v (@array)
1473 {
1474 0 ▶ if ($key =~ /^(?:category|story_length)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:category|story_length)$/) { } 0 0 0
1475 {
1476 0 $epub->add_subject($v);
1477 }
1478 else
1479 {
1480 0 $epub->add_meta_item($key, $v);
1481 }
1482 }
1483 }
1484 else
1485 {
1486 0 ▶ if ($key =~ /^(?:category|story_length)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:category|story_length)$/) { } 0 0 0
1487 {
1488 0 $epub->add_subject($know{$key});
1489 }
1490 else
1491 {
1492 0 $epub->add_meta_item($key, $know{$key});
1493 }
1494 }
1495 }
1496 else
1497 {
1498 0 $info_str .= sprintf("<b>%s:</b> %s<br/>\n", $key, join(', ', @{$know{$key}}));
1499 0 foreach my $cat (@{$know{$key}})
1500 {
1501 0 ▶ if ($key =~ /^(?:category|story_length)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:category|story_length)$/) { } 0 0 0
1502 {
1503 0 $epub->add_subject($cat);
1504 }
1505 else
1506 {
1507 0 $epub->add_meta_item($key, $cat);
1508 }
1509 }
1510 }
1511 }
1512  
1513 0 $info_str .= "</p>\n";
1514  
1515 0 my $titlepage = $self->tidy(story=>$info_str, title=>$info->{title});
1516 0 my $play_order = 1;
1517 0 my $id;
1518 0 $id = $epub->add_xhtml("title.html", $titlepage);
1519  
1520 # Add top-level nav-point
1521 0 my $navpoint = $epub->add_navpoint(
1522 label => "ToC",
1523 id => $id,
1524 content => "title.html",
1525 play_order => $play_order # should always start with 1
1526 );
1527  
1528 0 my @storyfiles = @{$info->{storyfiles}};
1529 0 my @ch_titles = @{$info->{chapter_titles}};
1530 0 ▶ for (my $i=0; $i < @storyfiles; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @storyfiles) 0 0 0
1531 {
1532 0 $play_order++;
1533 0 $id = $epub->copy_xhtml($storyfiles[$i], $storyfiles[$i]);
1534 0 my $navpoint = $epub->add_navpoint(
1535 label => $ch_titles[$i],
1536 id => $id,
1537 content => $storyfiles[$i],
1538 play_order => $play_order,
1539 );
1540 }
1541  
1542 0 my $epub_file = $info->{basename} . '.epub';
1543 0 $epub->pack_zip($epub_file);
1544  
1545 # now unlink the storyfiles
1546 0 ▶ for (my $i=0; $i < @storyfiles; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @storyfiles) 0 0 0
1547 {
1548 0 unlink $storyfiles[$i];
1549 }
1550  
1551 0 return $epub_file;
1552 } # build_epub
1553  
1554 =head2 tidy_chars
1555  
1556 Remove nasty encodings.
1557
1558 $content = $self->tidy_chars($content);
1559  
1560 =cut
1561 sub tidy_chars {
1562 0 ▶ my $self = shift;
Subroutine tidy_chars: not called CC=2 SCAR=17.9
Pod documented
1563 0 my $string = shift;
1564  
1565 # numeric entities
1566 0 $string =~ s/&#13;//sg;
1567 0 $string =~ s/&#39;/'/sg;
1568 0 $string =~ s/&#34;/"/sg;
1569 0 $string =~ s/&#45;/-/sg;
1570 0 $string =~ s/&#160;/ /sg;
1571  
1572 #-------------------------------------------------------
1573 # from Catalyst::Plugin::Params::Demoronize
1574 0 zap_cp1252($string);
1575  
1576 0 my %replace_map = (
1577 '\302' => '',
1578 '\240' => ' ',
1579 );
1580  
1581 0 foreach my $replace (keys(%{replace_map})) {
1582 0 my $rr = $replace_map{$replace};
1583 0 $string =~ s/$replace/$rr/g;
1584 }
1585  
1586 #-------------------------------------------------------
1587 # from demoronizser
1588 # http://www.fourmilab.ch/webtools/demoroniser/
1589 #-------------------------------------------------------
1590  
1591 # Supply missing semicolon at end of numeric entity if
1592 # Billy's bozos left it out.
1593  
1594 0 $string =~ s/(&#[0-2]\d\d)\s/$1; /g;
1595  
1596 # Fix dimbulb obscure numeric rendering of &lt; &gt; &amp;
1597  
1598 0 $string =~ s/\&\#038;/&amp;/g;
1599 0 $string =~ s/\&\#39;/&lsquo;/g;
1600 0 $string =~ s/\&\#060;/&lt;/g;
1601 0 $string =~ s/\&\#062;/&gt;/g;
1602  
1603 # Translate Unicode numeric punctuation characters
1604 # into ISO equivalents
1605  
1606 0 $string =~ s/&#8208;/-/sg; # 0x2010 Hyphen
1607 0 $string =~ s/&#8209;/-/sg; # 0x2011 Non-breaking hyphen
1608 0 $string =~ s/&#8211;/-/sg; # 0x2013 En dash
1609 0 $string =~ s/&#8212;/--/sg; # 0x2014 Em dash
1610 0 $string =~ s/&#8213;/--/sg; # 0x2015 Horizontal bar/quotation dash
1611 0 $string =~ s/&#8214;/||/sg; # 0x2016 Double vertical line
1612 0 $string =~ s-&#8215;-_-sg; # 0x2017 Double low line
1613 0 $string =~ s/&#8216;/`/sg; # 0x2018 Left single quotation mark
1614 0 $string =~ s/&#8217;/'/sg; # 0x2019 Right single quotation mark
1615 0 $string =~ s/&#8218;/,/sg; # 0x201A Single low-9 quotation mark
1616 0 $string =~ s/&#8219;/`/sg; # 0x201B Single high-reversed-9 quotation mark
1617 0 $string =~ s/&#8220;/"/sg; # 0x201C Left double quotation mark
1618 0 $string =~ s/&#8221;/"/sg; # 0x201D Right double quotation mark
1619 0 $string =~ s/&#8222;/,,/sg; # 0x201E Double low-9 quotation mark
1620 0 $string =~ s/&#8223;/"/sg; # 0x201F Double high-reversed-9 quotation mark
1621 0 $string =~ s/&#8226;/*/sg; # 0x2022 Bullet
1622 0 $string =~ s/&#8227;/*/sg; # 0x2023 Triangular bullet
1623 0 $string =~ s/&#8228;/./sg; # 0x2024 One dot leader
1624 0 $string =~ s/&#8229;/../sg; # 0x2026 Two dot leader
1625 0 $string =~ s/&#8230;/.../sg; # 0x2026 Horizontal ellipsis
1626 0 $string =~ s/&#8231;/&#183;/sg; # 0x2027 Hyphenation point
1627 #-------------------------------------------------------
1628  
1629 # and somehow some of the entities go funny
1630 0 $string =~ s/\&\#133;/.../g;
1631 0 $string =~ s/\&nbsp;/ /g;
1632 0 $string =~ s/\&lsquo;/'/g;
1633 0 $string =~ s/\&rsquo;/'/g;
1634 0 $string =~ s/\&ldquo;/"/g;
1635 0 $string =~ s/\&rdquo;/"/g;
1636 0 $string =~ s/\&quot;/"/g;
1637 0 $string =~ s/\&ndash;/-/g;
1638 0 $string =~ s/\&hellip;/.../g;
1639  
1640 # replace double-breaks with <p>
1641 0 $string =~ s#<br\s*\/?>\s*<br\s*\/?>#\n<p>#sg;
1642  
1643 # remove other cruft
1644 0 $string =~ s#<wbr>##sg;
1645 0 $string =~ s#</wbr>##sg;
1646 0 $string =~ s#<wbr/>##sg;
1647 0 $string =~ s#<nobr>##sg;
1648  
1649 # Clean unwanted MS-Word HTML
1650 0 $string =~ s#<!--\[if gte mso \d*\]>.*?<!\[endif\]-->##sg;
1651 0 $string =~ s#<!--\[if !mso\]>.*?<!\[endif\]-->##sg;
1652 0 $string =~ s!<[/]?(font|span|xml|del|ins|[ovwxp]:\w+|st\d:\w+)[^>]*?>!!igs;
1653 0 $string =~ s!<([^>]*)(?:lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>!<$1$2>!isg;
1654 0 $string =~ s/\s*class="Banner[0-9]+"//g;
1655 0 $string =~ s/\s*class="Textbody"//g;
1656 0 $string =~ s/\s*class="MsoNormal"//g;
1657 0 $string =~ s/\s*class="MsoBodyText"//g;
1658  
1659 0 return $string;
1660 } # tidy_chars
1661  
1662 2 1; # End of WWW::FetchStory::Fetcher
1663 __END__
« blib/lib/WWW/FetchStory/Fetcher/FanfictionNet.pm ↑ Summary blib/lib/HTTP/Cookies/Wget.pm »