blib/lib/WebFetch/Input/SiteNews.pm

filter: Statementstmt 94.1% 208 / 221 Branchbran 70.0% 77 / 110 Conditioncond 55.5% 20 / 36 MC/DCmcdc 29.1% 7 / 24 Subroutinesub 95.6% 22 / 23 Podpod 11.1% 1 / 9 totaltotal 79.2% 335 / 423 CC 70 SCAR 25.2
CC
70
Cov
83%
parse_input
19.0
parse_input_inner
18.2
fetch
17.1
CRAP 12.5
« blib/lib/WebFetch/Data/Record.pm ↑ Summary blib/lib/WebFetch/Data/Store.pm »
1 # WebFetch::Input::SiteNews
2 # ABSTRACT: download and save SiteNews headlines from a local file
3 #
4 # Copyright (c) 1998-2022 Ian Kluft. This program is free software; you can
5 # redistribute it and/or modify it under the terms of the GNU General Public
6 # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html
7  
8 # pragmas to silence some warnings from Perl::Critic
9 ## no critic (Modules::RequireExplicitPackage)
10 # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 ## use critic (Modules::RequireExplicitPackage)
15  
16 package WebFetch::Input::SiteNews;
17 3 $WebFetch::Input::SiteNews::VERSION = '0.15.8';
18 3 ▶use base "WebFetch";
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # set defaults
27 3 my ( $cat_priorities, $now );
28 3 Readonly::Array my @Options => ( "short_path|short=s", "long_path|long=s", );
29 3 Readonly::Scalar my $Usage => "--short short-output-file --long long-output-file";
30  
31 # configuration parameters
32 3 Readonly::Scalar my $num_links => 5;
33 3 Readonly::Array my @dt_keys => qw(locale time_zone);
34  
35 # functions for access to internal data for testing
36 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
37 sub _config_params
38 {
39 4 ▶ return { Options => \@Options, Usage => $Usage, num_links => $num_links };
Subroutine _config_params: called CC=1 SCAR=0.0
40 }
41 0 ▶sub _cat_priorities { return $cat_priorities; }
Subroutine _cat_priorities: not called CC=1 SCAR=6.9
42 ## critic (Subroutines::ProhibitUnusedPrivateSubroutines)
43  
44 # no user-servicable parts beyond this point
45  
46 # register capabilities with WebFetch
47 3 __PACKAGE__->module_register( _config_params(), "cmdline", "input:sitenews", "output:sitenews_long" );
48  
49 # constants for state names
50 299 ▶sub initial_state { return 0; }
Subroutine initial_state: called CC=1 SCAR=0.0
Pod undocumented
51 184 ▶sub attr_state { return 1; }
Subroutine attr_state: called CC=1 SCAR=0.0
Pod undocumented
52 234 ▶sub text_state { return 2; }
Subroutine text_state: called CC=1 SCAR=0.0
Pod undocumented
53  
54 # fetch() is the WebFetch API call point to run this module
55 sub fetch
56 {
57 2 ▶ my ($self) = @_;
Subroutine fetch: called CC=13 SCAR=28.4
Pod documented
58  
59 # set parameters for WebFetch routines
60 2 ▶ if ( not defined $self->{num_links} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"num_links"}) 2 0 2
61 2 $self->set_param( "num_links", WebFetch->config("num_links") );
62 }
63 2 $self->set_param( "para", 1 );
64  
65 # set up Webfetch Embedding API data
66 2 $self->{actions} = {};
67 2 $self->data->add_fields( "date", "title", "priority", "expired", "position", "label", "url", "category", "text" );
68  
69 # defined which fields match to which "well-known field names"
70 2 $self->data->add_wk_names(
71 "title" => "title",
72 "url" => "url",
73 "date" => "date",
74 "summary" => "text",
75 "category" => "category"
76 );
77  
78 # process the links
79  
80 # get local time for various date comparisons
81 2 my %dt_opts;
82 2 ▶ if ( exists $self->{datetime_settings} and reftype( $self->{datetime_settings} ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH") 0 2 2
Condition33%1/3
exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH"
exists $self->{"datetime_settings"} builtin::reftype($self->{"datetime_settings"}) eq "HASH"
Truth table33%1/3
A and B
A= exists $self->{"datetime_settings"}B= builtin::reftype($self->{"datetime_settings"}) eq "HASH"
ABresult
0X0
100
111
83 0 %dt_opts = %{ $self->{datetime_settings} }; # get locale and time_zone settings if available
84 }
85 2 ▶ if ( exists $self->{testing_faketime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'testing_faketime'}) { } 2 0 2
86  
87 # use a pre-specified timestamp for testing purposes so news elements are same age as expected result
88 2 $now = DateTime::Format::ISO8601->parse_datetime( $self->{testing_faketime}, %dt_opts );
89 2 ▶ if ( exists $dt_opts{locale} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dt_opts{"locale"}) 0 2 2
90 0 $now->set_locale( $dt_opts{locale} );
91 }
92 2 ▶ if ( exists $dt_opts{time_zone} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dt_opts{"time_zone"}) 0 2 2
93 0 $now->set_time_zone( $dt_opts{time_zone} );
94 }
95 } else {
96 0 $now = DateTime->now(%dt_opts);
97 }
98  
99 # parse data file(s)
100 2 my @sources;
101 2 ▶ if ( ( exists $self->{sources} ) and ( ref $self->{sources} eq "ARRAY" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"sources"} and ref $self->{"sources"} eq "ARRAY") 0 2 2
Condition33%1/3
exists $self->{"sources"} and ref $self->{"sources"} eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
exists $self->{"sources"} and ref $self->{"sources"} eq "ARRAY"
exists $self->{"sources"} ref $self->{"sources"} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $self->{"sources"}B= ref $self->{"sources"} eq "ARRAY"
ABresult
0X0
100
111
102 0 @sources = @{ $self->{sources} };
103 }
104 2 ▶ if ( exists $self->{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"source"}) 2 0 2
105 2 push @sources, $self->{source};
106 }
107 2 foreach my $source (@sources) {
108 2 $self->parse_input($source);
109 }
110  
111 # set parameters for the short news format
112 2 ▶ if ( defined $self->{short_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"short_path"}) 2 0 2
113  
114 # create the HTML actions list
115 2 $self->{actions}{html} = [];
116  
117 # create the HTML-generation parameters
118 2 my $params = {};
119 2 $params = {};
120 $params->{sort_func} = sub {
121 48 ▶ my ( $a, $b ) = @_;
Subroutine __ANON__: called CC=6 SCAR=19.2
122  
123 # sort/compare news entries for the short display
124 # sorting priority:
125 # expiration status first (expired items last)
126 # priority second (category/age combo)
127 # label third (chronological order)
128  
129 # check expirations first
130 48 my $exp_fnum = $self->fname2fnum("expired");
131 48 ▶ ( $a->[$exp_fnum] and not $b->[$exp_fnum] ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$a->[$exp_fnum] and not $b->[$exp_fnum] and return 1 0 48 48
Condition33%1/3
$a->[$exp_fnum] and not $b->[$exp_fnum]
!ll&&!rl&&r
4800
MC/DC0%0/2
$a->[$exp_fnum] and not $b->[$exp_fnum]
$a->[$exp_fnum] not $b->[$exp_fnum]
Truth table33%1/3
A and not(B)
A= $a->[$exp_fnum]B= not $b->[$exp_fnum]
ABresult
0X0
100
111
132 48 ▶ ( not $a->[$exp_fnum] and $b->[$exp_fnum] ) and return -1;
Branch: 1 branch
BranchTrueFalseTotal
not $a->[$exp_fnum] and $b->[$exp_fnum] and return -1 0 48 48
Condition33%1/3
not $a->[$exp_fnum] and $b->[$exp_fnum]
!ll&&!rl&&r
0480
MC/DC0%0/2
not $a->[$exp_fnum] and $b->[$exp_fnum]
not $a->[$exp_fnum] $b->[$exp_fnum]
Truth table33%1/3
not(A) and B
A= not $a->[$exp_fnum]B= $b->[$exp_fnum]
ABresult
0X0
100
111
133  
134 # compare priority - posting category w/ age penalty
135 48 my $pri_fnum = $self->fname2fnum("priority");
136 48 ▶ if ( $a->[$pri_fnum] != $b->[$pri_fnum] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->[$pri_fnum] != $b->[$pri_fnum]) 18 30 48
137 18 return $a->[$pri_fnum] <=> $b->[$pri_fnum];
138 }
139  
140 # otherwise sort by label (chronological order)
141 30 my $lbl_fnum = $self->fname2fnum("label");
142 30 return $a->[$lbl_fnum] cmp $b->[$lbl_fnum];
143 2 };
144 $params->{filter_func} = sub {
145  
146 # filter: skip expired items
147 33 ▶ my $exp_fnum = $self->fname2fnum("expired");
Subroutine __ANON__: called CC=1 SCAR=0.0
148 33 return not $_[$exp_fnum];
149 2 };
150 $params->{format_func} = sub {
151  
152 # generate HTML text
153 10 ▶ my $txt_fnum = $self->fname2fnum("text");
Subroutine __ANON__: called CC=1 SCAR=0.0
154 10 my $pri_fnum = $self->fname2fnum("priority");
155 10 return $_[$txt_fnum] . "\n<!--- priority " . $_[$pri_fnum] . " --->";
156 2 };
157  
158 # put parameters for fmt_handler_html() on the html list
159 2 push @{ $self->{actions}{html} }, [ $self->{short_path}, $params ];
160 }
161  
162 # set parameters for the long news format
163 2 ▶ if ( defined $self->{long_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"long_path"}) 2 0 2
164  
165 # create the SiteNews-specific action list
166 # It will use WebFetch::Input::SiteNews::fmt_handler_sitenews_long()
167 # which is defined in this file
168 2 $self->{actions}{sitenews_long} = [];
169  
170 # put parameters for fmt_handler_sitenews_long() on the list
171 2 push @{ $self->{actions}{sitenews_long} }, [ $self->{long_path} ];
172 }
173 2 return;
174 }
175  
176 # inner portion of input parsing - called by parse_input()
177 # read SiteNews text file and parse it into news items to return to caller
178 sub parse_input_inner
179 {
180 2 ▶ my ( $self, $news_data_fd ) = @_;
Subroutine parse_input_inner: called CC=18 SCAR=29.0
Pod undocumented
181 2 my @news_items;
182 2 my $position = 0;
183 2 my $state = initial_state; # before first entry
184 2 my ($current);
185 2 $cat_priorities = {}; # priorities for sorting
186 2 ▶ while (<$news_data_fd>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $news_data_fd)) 233 2 235
187 233 chomp;
188 233 ▶ /^\s*\#/x and next; # skip comments
Branch: 1 branch
BranchTrueFalseTotal
/^\s*\#/x and next 5 228 233
189 228 ▶ /^\s*$/x and next; # skip blank lines
Branch: 1 branch
BranchTrueFalseTotal
/^\s*$/x and next 36 192 228
190  
191 192 ▶ if (/^[^\s]/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[^\s]/x) { } 107 85 192
192  
193 # found attribute line
194 107 ▶ if ( $state == initial_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state == initial_state()) 10 97 107
195 10 ▶ if (/^categories:\s*(.*)/x) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^categories:\s*(.*)/x) { } 2 8 10
elsif (/^(\w+):\s*(.*)/x) { } 6 2 8
196 2 my @cats = split( ' ', $1 );
197 2 my ($i);
198 2 $cat_priorities->{"default"} = 999;
199 2 ▶ for ( $i = 0 ; $i <= $#cats ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#cats) 8 2 10
200 8 $cat_priorities->{ $cats[$i] } = $i + 1;
201 }
202 2 next;
203 } elsif (/^(\w+):\s*(.*)/x) {
204 6 $self->set_param( $1, $2 );
205 }
206 }
207 105 ▶ if ( $state == initial_state or $state == text_state ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state == initial_state() or $state == text_state()) { } 39 66 105
elsif ($state == attr_state()) { } 66 0 66
Condition100%3/3
$state == initial_state() or $state == text_state()
l!l&&r!l&&!r
83166
MC/DC100%2/2
$state == initial_state() or $state == text_state()
$state == initial_state() $state == text_state()
Truth table100%3/3
A or B
A= $state == initial_state()B= $state == text_state()
ABresult
1X1
011
000
208  
209 # found first attribute of a new entry
210 39 ▶ if (/^([^=]+)=(.*)/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^=]+)=(.*)/x) 33 6 39
211 33 $current = {};
212 33 $current->{position} = $position++;
213 33 $current->{$1} = $2;
214 33 push( @news_items, $current );
215 33 $state = attr_state;
216 }
217 } elsif ( $state == attr_state ) {
218  
219 # found a followup attribute
220 66 ▶ if (/^([^=]+)=(.*)/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^=]+)=(.*)/x) 66 0 66
221 66 $current->{$1} = $2;
222 }
223 }
224 } else {
225  
226 # found text line
227 85 ▶ if ( $state == initial_state ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state == initial_state()) { } 0 85 85
elsif ($state == attr_state() or $state == text_state()) { } 85 0 85
228  
229 # cannot accept text before any attributes
230 0 next;
231 ▶ } elsif ( $state == attr_state or $state == text_state ) {
Condition66%2/3
$state == attr_state() or $state == text_state()
l!l&&r!l&&!r
33520
MC/DC0%0/2
$state == attr_state() or $state == text_state()
$state == attr_state() $state == text_state()
Truth table66%2/3
A or B
A= $state == attr_state()B= $state == text_state()
ABresult
1X1
011
000
232 85 ▶ if ( defined $current->{text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current->{'text'}) { } 52 33 85
233 52 $current->{text} .= "\n$_";
234 } else {
235 33 $current->{text} = $_;
236 }
237 85 $state = text_state;
238 }
239 }
240 }
241 2 return @news_items;
242 }
243  
244 # parse input file
245 sub parse_input
246 {
247 2 ▶ my ( $self, $input ) = @_;
Subroutine parse_input: called CC=17 SCAR=29.5
Pod undocumented
248  
249 # parse data file
250 2 my $news_data;
251 2 ▶ if ( not open( $news_data, "<", $input ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $news_data, "<", $input) 0 2 2
252 0 croak "$0: failed to open $input: $!\n";
253 }
254 2 my @news_items = $self->parse_input_inner($news_data);
255 2 close $news_data;
256  
257 # translate parsed news into the WebFetch Embedding API data table
258 2 my ( %label_hash, $pos );
259 2 $pos = 0;
260 2 foreach my $item (@news_items) {
261  
262 # collect fields for the data record
263 33 ▶ my $title = ( defined $item->{title} ) ? $item->{title} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{'title'} ? : 33 0 33
264 33 ▶ my $posted = ( defined $item->{posted} ) ? $item->{posted} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{'posted'} ? : 33 0 33
265 33 ▶ my $category = ( defined $item->{category} ) ? $item->{category} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{'category'} ? : 33 0 33
266 33 ▶ my $text = ( defined $item->{text} ) ? $item->{text} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{'text'} ? : 33 0 33
267 my $url_prefix =
268 33 ▶ ( defined $self->{url_prefix} ) ? $self->{url_prefix} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'url_prefix'} ? : 33 0 33
269  
270 # timestamp processing using optional locale and time_zone from WebFetch object's datetime_settings hash
271 # This is usually set by SiteNews file's global settings at the top
272 33 my ( %dt_opts, $time_str, $anchor_time );
273 33 ▶ if ( exists $self->{datetime_settings} and reftype $self->{datetime_settings} eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH") 33 0 33
Condition33%1/3
exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH"
!ll&&!rl&&r
0033
MC/DC0%0/2
exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH"
exists $self->{"datetime_settings"} builtin::reftype($self->{"datetime_settings"}) eq "HASH"
Truth table33%1/3
A and B
A= exists $self->{"datetime_settings"}B= builtin::reftype($self->{"datetime_settings"}) eq "HASH"
ABresult
0X0
100
111
274 33 %dt_opts = %{ $self->{datetime_settings} }; # get locale and time_zone settings if available
275 }
276 33 ▶ if ($posted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posted) 33 0 33
277 33 my $date_ref = WebFetch::parse_date( \%dt_opts, $posted );
278 33 ▶ if ( defined $date_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date_ref) 8 25 33
279 8 ▶ my $dt = ( ref $date_ref eq "DateTime" ) ? $date_ref : DateTime->new( @$date_ref, %dt_opts );
Branch: 1 branch
BranchTrueFalseTotal
ref $date_ref eq 'DateTime' ? : 7 1 8
280 8 $time_str = WebFetch::gen_timestamp( \%dt_opts, $dt );
281 8 $anchor_time = WebFetch::anchor_timestr( \%dt_opts, $dt );
282 }
283 }
284 33 ▶ if ( not defined $time_str or not defined $anchor_time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time_str and defined $anchor_time) 25 8 33
Condition66%2/3
defined $time_str and defined $anchor_time
!ll&&!rl&&r
2508
MC/DC50%1/2
defined $time_str and defined $anchor_time
defined $time_str defined $anchor_time
Truth table66%2/3
A and B
A= defined $time_strB= defined $anchor_time
ABresult
0X0
100
111
285 25 $time_str = "undated";
286 25 $anchor_time = "0000-undated";
287 }
288  
289 # generate an intra-page link label
290 33 my ( $label, $count );
291 33 $count = 0;
292 33 ▶ while ( ( $label = $anchor_time . "-" . sprintf( "%03d", $count ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($label = $anchor_time . '-' . sprintf("%03d", $count) and defined $label_hash{$label}) 300 33 333
Condition66%2/3
$label = $anchor_time . '-' . sprintf("%03d", $count) and defined $label_hash{$label}
!ll&&!rl&&r
033300
MC/DC50%1/2
$label = $anchor_time . '-' . sprintf("%03d", $count) and defined $label_hash{$label}
$label = $anchor_time . '-' . sprintf("%03d", $count) defined $label_hash{$label}
Truth table66%2/3
A and B
A= $label = $anchor_time . '-' . sprintf("%03d", $count)B= defined $label_hash{$label}
ABresult
0X0
100
111
293 and defined $label_hash{$label} )
294 {
295 300 $count++;
296 }
297 33 $label_hash{$label} = 1;
298  
299 # generate data record for output
300 33 $self->data->add_record(
301 $time_str, $title, $self->priority($item),
302 expired($item), $pos, $label, $url_prefix . "#" . $label,
303 $category, $text
304 );
305 33 $pos++;
306 }
307 2 return;
308 }
309  
310 # format handler function specific to this module's long-news output format
311 sub fmt_handler_sitenews_long
312 {
313 # TODO move sort block to separate function and remove the no-critic exemption
314 ## no critic ( BuiltinFunctions::RequireSimpleSortBlock )
315 2 ▶ my ($self) = @_;
Subroutine fmt_handler_sitenews_long: called CC=8 SCAR=20.8
Pod undocumented
316  
317 # sort events for long display
318 my @long_news = sort {
319  
320 # sort news entries for long display
321 # sorting priority:
322 # date first
323 # category/priority second
324 # reverse file order last
325  
326 # sort by date
327 37 my $lbl_fnum = $self->fname2fnum("label");
328 37 my ( $a_date, $b_date ) = ( $a->[$lbl_fnum], $b->[$lbl_fnum] );
329 37 $a_date =~ s/-.*//x;
330 37 $b_date =~ s/-.*//x;
331 37 ▶ if ( $a_date ne $b_date ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_date ne $b_date) 7 30 37
332 7 return $b_date cmp $a_date;
333 }
334  
335 # sort by priority (within same date)
336 30 my $pri_fnum = $self->fname2fnum("priority");
337 30 ▶ if ( $a->[$pri_fnum] != $b->[$pri_fnum] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebFetch::Input::SiteNews::a->[$pri_fnum] != $WebFetch::Input::SiteNews::b->[$pri_fnum]) 1 29 30
338 1 return $a->[$pri_fnum] <=> $b->[$pri_fnum];
339 }
340  
341 # sort by chronological order (within same date and priority)
342 29 return $a->[$lbl_fnum] cmp $b->[$lbl_fnum];
343 2 } @{ $self->{data}{records} };
344  
345 # process the links for the long list
346 2 my ( @long_text, $prev, $url_prefix, $i );
347 $url_prefix =
348 ( defined $self->{url_prefix} )
349 ? $self->{url_prefix}
350 2 ▶ : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'url_prefix'} ? : 2 0 2
351 2 $prev = undef;
352 2 push @long_text, "<dl>";
353 2 my $lbl_fnum = $self->fname2fnum("label");
354 2 my $date_fnum = $self->fname2fnum("date");
355 2 my $title_fnum = $self->fname2fnum("title");
356 2 my $txt_fnum = $self->fname2fnum("text");
357 2 my $exp_fnum = $self->fname2fnum("expired");
358 2 my $pri_fnum = $self->fname2fnum("priority");
359  
360 2 ▶ for ( $i = 0 ; $i <= $#long_news ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#long_news) 33 2 35
361 33 my $news = $long_news[$i];
362 33 ▶ if ( ( !defined $prev->[$date_fnum] )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prev->[$date_fnum] or $prev->[$date_fnum] ne $news->[$date_fnum]) 9 24 33
Condition100%3/3
not defined $prev->[$date_fnum] or $prev->[$date_fnum] ne $news->[$date_fnum]
l!l&&r!l&&!r
2724
MC/DC100%2/2
not defined $prev->[$date_fnum] or $prev->[$date_fnum] ne $news->[$date_fnum]
not defined $prev->[$date_fnum] $prev->[$date_fnum] ne $news->[$date_fnum]
Truth table100%3/3
not(A) or B
A= not defined $prev->[$date_fnum]B= $prev->[$date_fnum] ne $news->[$date_fnum]
ABresult
1X1
011
000
363 or $prev->[$date_fnum] ne $news->[$date_fnum] )
364 {
365 9 push @long_text, "<dt>" . $news->[$date_fnum];
366 9 push @long_text, "<dd>";
367 }
368 33 ▶ push @long_text,
Branch: 1 branch
BranchTrueFalseTotal
$news->[$exp_fnum] ? : 0 33 33
369 "<a name=\""
370 . $news->[$lbl_fnum] . "\">"
371 . $news->[$title_fnum]
372 . "</a>\n"
373 . $news->[$txt_fnum]
374 . "<!--- priority: "
375 . $news->[$pri_fnum]
376 . ( $news->[$exp_fnum] ? " expired" : "" ) . " --->";
377 33 push @long_text, "<p>";
378 33 $prev = $news;
379 }
380 2 push @long_text, "</dl>";
381  
382 # store it for later save to disk
383 2 $self->html_savable( $self->{long_path}, join( "\n", @long_text ) . "\n" );
384 2 return;
385 }
386  
387 #
388 # utility functions
389 #
390  
391 # function to detect if a news entry is expired
392 sub expired
393 {
394 33 ▶ my ($entry) = @_;
Subroutine expired: called CC=3 SCAR=16.5
Pod undocumented
395 33 ▶ return 0 if not exists $entry->{expires};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{"expires"} 33 0 33
396 0 ▶ return 0 if not defined $entry->{expires};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"expires"} 0 0 0
397 0 return $entry->{expires} < $now;
398 }
399  
400 # function to compute a priority value which decays with age
401 sub priority
402 {
403 33 ▶ my ( $self, $entry ) = @_;
Subroutine priority: called CC=11 SCAR=25.2
Pod undocumented
404  
405 33 ▶ return 999 if not exists $entry->{posted};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{"posted"} 0 33 33
406 33 ▶ return 999 if not defined $entry->{posted};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"posted"} 0 33 33
407  
408 33 my $date_ref = WebFetch::parse_date( $entry->{posted} );
409 33 ▶ return 999 if not defined $date_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date_ref 25 8 33
410  
411 8 my %dt_opts;
412 8 ▶ if ( exists $self->{datetime_settings} and reftype( $self->{datetime_settings} ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH") 8 0 8
Condition33%1/3
exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH"
!ll&&!rl&&r
008
MC/DC0%0/2
exists $self->{"datetime_settings"} and builtin::reftype($self->{"datetime_settings"}) eq "HASH"
exists $self->{"datetime_settings"} builtin::reftype($self->{"datetime_settings"}) eq "HASH"
Truth table33%1/3
A and B
A= exists $self->{"datetime_settings"}B= builtin::reftype($self->{"datetime_settings"}) eq "HASH"
ABresult
0X0
100
111
413 8 %dt_opts = %{ $self->{datetime_settings} }; # get locale and time_zone settings if available
414 }
415 8 ▶ my $dt = ( ref $date_ref eq "DateTime" ) ? $date_ref : DateTime->new( @$date_ref, %dt_opts );
Branch: 1 branch
BranchTrueFalseTotal
ref $date_ref eq 'DateTime' ? : 7 1 8
416 8 my $age = ( $now->subtract_datetime($dt) )->delta_days();
417 8 my $bonus = 0;
418  
419 8 ▶ if ( $age <= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age <= 2) 0 8 8
420 0 $bonus -= 2 - $age;
421 }
422 8 ▶ if ( ( defined $entry->{category} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry->{'category'} and defined $$cat_priorities{$entry->{'category'}}) { } 7 1 8
Condition66%2/3
defined $entry->{"category"} and defined $$cat_priorities{$entry->{"category"}}
!ll&&!rl&&r
017
MC/DC50%1/2
defined $entry->{"category"} and defined $$cat_priorities{$entry->{"category"}}
defined $entry->{"category"} defined $$cat_priorities{$entry->{"category"}}
Truth table66%2/3
A and B
A= defined $entry->{"category"}B= defined $$cat_priorities{$entry->{"category"}}
ABresult
0X0
100
111
423 and ( defined $cat_priorities->{ $entry->{category} } ) )
424 {
425 my $cat_pri =
426 ( exists $cat_priorities->{ $entry->{category} } )
427 ? $cat_priorities->{ $entry->{category} }
428 7 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $$cat_priorities{$entry->{'category'}} ? : 7 0 7
429 7 return $cat_pri + $age * 0.025 + $bonus;
430 } else {
431 1 return $cat_priorities->{"default"} + $age * 0.025 + $bonus;
432 }
433 }
434  
435 3 1;
436  
437 =pod
438  
439 =encoding UTF-8
440  
441 =head1 NAME
442  
443 WebFetch::Input::SiteNews - download and save SiteNews headlines from a local file
444  
445 =head1 VERSION
446  
447 version 0.15.8
448  
449 =head1 SYNOPSIS
450  
451 In perl scripts:
452  
453 C<use WebFetch::Input::SiteNews;>
454  
455 From the command line:
456  
457 C<perl -w -MWebFetch::Input::SiteNews -e "&fetch_main" -- --dir directory
458 --source news-file --short short-form-output-file
459 --long long-form-output-file>
460  
461 =head1 DESCRIPTION
462  
463 This module gets the current headlines from a site-local file.
464  
465 The I<--source> parameter specifies a file name which contains news to be
466 posted. See L<"FILE FORMAT"> below for details on contents to put in the
467 file. I<--source> may be specified more than once, allowing a single news
468 output to come from more than one input. For example, one file could be
469 manually maintained in CVS or RCS and another could be entered from a
470 web form.
471  
472 After this runs, the file C<site_news.html> will be created or replaced.
473 If there already was a C<site_news.html> file, it will be moved to
474 C<Osite_news.html>.
475  
476 =head1 FILE FORMAT
477  
478 The WebFetch::Input::SiteNews data format is used to set up news for the
479 local web site and allow other sites to import it via WebFetch.
480 The file is plain text containing comments and the news items.
481  
482 There are three forms of outputs generated from these news files.
483  
484 The I<"short news" output> is a small number (5 by default)
485 of HTML text and links used for display in a small news window.
486 This list takes into account expiration dates and
487 priorities to pick which news entries are displayed and in what order.
488  
489 The I<"long news" output> lists all the news entries chronologically.
490 It does not take expiration or priority into account.
491 It is intended for a comprehensive site news list.
492  
493 The I<export modes> make news items available in formats other web sites
494 can retrieve to post news about your site. They are chronological
495 listings that omit expired items.
496 They do not take priorities into account.
497  
498 =over 4
499  
500 =item global parameters
501  
502 Lines coming before the first news item can set global parameters.
503  
504 =over 4
505  
506 =item categories
507  
508 A line before the first news item beginning with "categories:" contains a
509 whitespace-delimited list of news category names in order from highest
510 to lowest priority.
511 These priority names are used by the news item attributes and
512 then for sorting "short news" list items.
513  
514 =back
515  
516 =item data lines
517  
518 Non-blank non-indented non-comment lines are I<data lines>.
519 Each data line contains a I<name=value> pair.
520 Each group of consecutive data lines is followed by an arbitrary number
521 of indented lines which contain HTML text for the news entry.
522  
523 The recognized attributes are as follows:
524  
525 =over 4
526  
527 =item category
528  
529 used for prioritization, values are set by the categories global parameter
530 (required)
531  
532 =item posted
533  
534 date posted, format is a numerical date YYYYMMDD (required)
535  
536 =item expires
537  
538 expiration date, format is a numerical date YYYYMMDD (optional)
539  
540 =item title
541  
542 shorter title for use in news exports to other sites,
543 otherwise the whole news text will be used (optional)
544  
545 =back
546  
547 =item text lines
548  
549 Intended lines are HTML text for the news item.
550  
551 =item comments
552  
553 Comments are lines beginning with "#".
554 They are ignored so they can be used for human-readable information.
555  
556 =back
557  
558 Note that the "short news" list has some modifications to
559 priorities based on the age of the news item,
560 so that the short list will favor newer items when
561 they're the same priority.
562 There is a sorting "priority bonus" for items less than a
563 day old, which increases their priority by two priority levels.
564 Day-old news items get a bonus of one priority level.
565 All news items also "decay" in priority slightly every day,
566 dropping a whole priority level every 40 days.
567  
568 =head1 SEE ALSO
569  
570 L<WebFetch>
571 L<https://github.com/ikluft/WebFetch>
572  
573 =head1 BUGS AND LIMITATIONS
574  
575 Please report bugs via GitHub at L<https://github.com/ikluft/WebFetch/issues>
576  
577 Patches and enhancements may be submitted via a pull request at L<https://github.com/ikluft/WebFetch/pulls>
578  
579 =head1 AUTHOR
580  
581 Ian Kluft <https://github.com/ikluft>
582  
583 =head1 COPYRIGHT AND LICENSE
584  
585 This software is Copyright (c) 1998-2023 by Ian Kluft.
586  
587 This is free software, licensed under:
588  
589 The GNU General Public License, Version 3, June 2007
590  
591 =cut
592  
593 __END__
594 # POD docs follow
595  
« blib/lib/WebFetch/Data/Record.pm ↑ Summary blib/lib/WebFetch/Data/Store.pm »