blib/lib/HTML/Latemp/News.pm

filter: Statementstmt 11.4% 12 / 105 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 18.5% 5 / 27 Podpod n/a totaltotal 9.7% 17 / 174 CC 23 SCAR 17.5
CC
23
Cov
8%
initialize
12.0
author
6.0
category
6.0
CRAP 5.8
↑ Summary Build »
1 package HTML::Latemp::News;
2 $HTML::Latemp::News::VERSION = '0.1.12';
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 package HTML::Latemp::News::Base;
10 $HTML::Latemp::News::Base::VERSION = '0.1.12';
11 2 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new
14 {
15 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
16 0 my $self = {};
17 0 bless $self, $class;
18 0 $self->initialize(@_);
19 0 return $self;
20 }
21  
22 package HTML::Latemp::News::Item;
23 $HTML::Latemp::News::Item::VERSION = '0.1.12';
24 our @ISA = (qw(HTML::Latemp::News::Base));
25  
26 sub author
27 {
28 0 ▶ my $self = shift;
Subroutine author: not called CC=2 SCAR=17.9
29  
30 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
31 {
32 0 $self->{author} = shift;
33 }
34  
35 0 return $self->{author};
36 }
37  
38 sub category
39 {
40 0 ▶ my $self = shift;
Subroutine category: not called CC=2 SCAR=17.9
41  
42 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
43 {
44 0 $self->{category} = shift;
45 }
46  
47 0 return $self->{category};
48 }
49  
50 sub date
51 {
52 0 ▶ my $self = shift;
Subroutine date: not called CC=2 SCAR=17.9
53  
54 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
55 {
56 0 $self->{date} = shift;
57 }
58  
59 0 return $self->{date};
60 }
61  
62 sub description
63 {
64 0 ▶ my $self = shift;
Subroutine description: not called CC=2 SCAR=17.9
65  
66 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
67 {
68 0 $self->{description} = shift;
69 }
70  
71 0 return $self->{description};
72 }
73  
74 sub id
75 {
76 0 ▶ my $self = shift;
Subroutine id: not called CC=2 SCAR=17.9
77  
78 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
79 {
80 0 $self->{id} = shift;
81 }
82  
83 0 return $self->{id};
84 }
85  
86 sub index
87 {
88 0 ▶ my $self = shift;
Subroutine index: not called CC=2 SCAR=17.9
89  
90 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
91 {
92 0 $self->{index} = shift;
93 }
94  
95 0 return $self->{index};
96 }
97  
98 sub text
99 {
100 0 ▶ my $self = shift;
Subroutine text: not called CC=2 SCAR=17.9
101  
102 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
103 {
104 0 $self->{text} = shift;
105 }
106  
107 0 return $self->{text};
108 }
109  
110 sub title
111 {
112 0 ▶ my $self = shift;
Subroutine title: not called CC=2 SCAR=17.9
113  
114 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
115 {
116 0 $self->{title} = shift;
117 }
118  
119 0 return $self->{title};
120 }
121  
122 sub initialize
123 {
124 0 ▶ my $self = shift;
Subroutine initialize: not called CC=3 SCAR=24.8
125  
126 0 my (%args) = (@_);
127  
128 0 foreach my $k ( keys(%args) )
129 {
130 0 ▶ if ( !$self->can($k) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($k)) 0 0 0
131 {
132 0 die "Unknown property for HTML::Latemp::News::Item - \"$k\"!";
133 }
134 0 $self->can($k)->( $self, $args{$k} );
135 }
136 }
137  
138 package HTML::Latemp::News;
139  
140 our @ISA = (qw(HTML::Latemp::News::Base));
141  
142 sub copyright
143 {
144 0 ▶ my $self = shift;
Subroutine copyright: not called CC=2 SCAR=17.9
145  
146 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
147 {
148 0 $self->{copyright} = shift;
149 }
150  
151 0 return $self->{copyright};
152 }
153  
154 sub description
155 {
156 0 ▶ my $self = shift;
Subroutine description: not called CC=2 SCAR=17.9
157  
158 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
159 {
160 0 $self->{description} = shift;
161 }
162  
163 0 return $self->{description};
164 }
165  
166 sub docs
167 {
168 0 ▶ my $self = shift;
Subroutine docs: not called CC=2 SCAR=17.9
169  
170 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
171 {
172 0 $self->{docs} = shift;
173 }
174  
175 0 return $self->{docs};
176 }
177  
178 sub generator
179 {
180 0 ▶ my $self = shift;
Subroutine generator: not called CC=2 SCAR=17.9
181  
182 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
183 {
184 0 $self->{generator} = shift;
185 }
186  
187 0 return $self->{generator};
188 }
189  
190 sub items
191 {
192 0 ▶ my $self = shift;
Subroutine items: not called CC=2 SCAR=17.9
193  
194 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
195 {
196 0 $self->{items} = shift;
197 }
198  
199 0 return $self->{items};
200 }
201  
202 sub language
203 {
204 0 ▶ my $self = shift;
Subroutine language: not called CC=2 SCAR=17.9
205  
206 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
207 {
208 0 $self->{language} = shift;
209 }
210  
211 0 return $self->{language};
212 }
213  
214 sub link
215 {
216 0 ▶ my $self = shift;
Subroutine link: not called CC=2 SCAR=17.9
217  
218 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
219 {
220 0 $self->{link} = shift;
221 }
222  
223 0 return $self->{link};
224 }
225  
226 sub managing_editor
227 {
228 0 ▶ my $self = shift;
Subroutine managing_editor: not called CC=2 SCAR=17.9
229  
230 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
231 {
232 0 $self->{managing_editor} = shift;
233 }
234  
235 0 return $self->{managing_editor};
236 }
237  
238 sub rating
239 {
240 0 ▶ my $self = shift;
Subroutine rating: not called CC=2 SCAR=17.9
241  
242 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
243 {
244 0 $self->{rating} = shift;
245 }
246  
247 0 return $self->{rating};
248 }
249  
250 sub title
251 {
252 0 ▶ my $self = shift;
Subroutine title: not called CC=2 SCAR=17.9
253  
254 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
255 {
256 0 $self->{title} = shift;
257 }
258  
259 0 return $self->{title};
260 }
261  
262 sub ttl
263 {
264 0 ▶ my $self = shift;
Subroutine ttl: not called CC=2 SCAR=17.9
265  
266 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
267 {
268 0 $self->{ttl} = shift;
269 }
270  
271 0 return $self->{ttl};
272 }
273  
274 sub webmaster
275 {
276 0 ▶ my $self = shift;
Subroutine webmaster: not called CC=2 SCAR=17.9
277  
278 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
279 {
280 0 $self->{webmaster} = shift;
281 }
282  
283 0 return $self->{webmaster};
284 }
285 2 ▶use XML::RSS;
Subroutine BEGIN: called CC=1 SCAR=2.6
286  
287 sub input_items
288 {
289 my $self = shift;
290  
291 my $items = shift;
292  
293 return [ map { $self->input_single_item( $_, $items->[$_] ) }
294 ( 0 .. $#$items ) ];
295 }
296  
297 sub input_single_item
298 {
299 my $self = shift;
300 my ( $index, $inputted_item ) = (@_);
301  
302 return HTML::Latemp::News::Item->new( %$inputted_item, 'index' => $index, );
303 }
304  
305 sub initialize
306 {
307 my $self = shift;
308  
309 my %args = (@_);
310  
311 my $items = $args{'news_items'};
312  
313 $self->items( $self->input_items($items) );
314  
315 $self->title( $args{'title'} );
316 $self->link( $args{'link'} );
317 $self->language( $args{'language'} );
318 $self->rating( $args{'rating'}
319 || '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))'
320 );
321 $self->copyright( $args{'copyright'} || "" );
322 $self->docs( $args{'docs'} || "http://blogs.law.harvard.edu/tech/rss" );
323 $self->ttl( $args{'ttl'} || "360" );
324 $self->generator( $args{'generator'} || "Perl and XML::RSS" );
325 $self->webmaster( $args{'webmaster'} );
326 $self->managing_editor( $args{'managing_editor'} || $self->webmaster() );
327 $self->description( $args{'description'} );
328  
329 return 0;
330 }
331  
332  
333 sub add_item_to_rss_feed
334 {
335 my $self = shift;
336 my %args = (@_);
337  
338 my $item = $args{'item'};
339 my $rss_feed = $args{'feed'};
340  
341 my $item_url = $self->get_item_url($item);
342  
343 $rss_feed->add_item(
344 'title' => $item->title(),
345 'link' => $item_url,
346 'permaLink' => $item_url,
347 'enclosure' => { 'url' => $item_url, },
348 'description' => $item->description(),
349 'author' => $item->author(),
350 'pubDate' => $item->date(),
351 'category' => $item->category(),
352 );
353 }
354  
355 sub get_item_url
356 {
357 my $self = shift;
358 my $item = shift;
359 return $self->link() . $self->get_item_rel_url($item);
360 }
361  
362 sub get_item_rel_url
363 {
364 my $self = shift;
365 my $item = shift;
366 return "news/" . $item->id() . "/";
367 }
368  
369 sub get_items_to_include
370 {
371 my $self = shift;
372 my $args = shift;
373  
374 my $num_items_to_include = $args->{'num_items'} || 10;
375  
376 my $items = $self->items();
377  
378 if ( @$items < $num_items_to_include )
379 {
380 $num_items_to_include = scalar(@$items);
381 }
382  
383 return [ @$items[ ( -$num_items_to_include ) .. (-1) ] ];
384 }
385  
386 sub generate_rss_feed
387 {
388 my $self = shift;
389  
390 my %args = (@_);
391  
392 my $rss_feed = XML::RSS->new( 'version' => "2.0" );
393 $rss_feed->channel(
394 'title' => $self->title(),
395 'link' => $self->link(),
396 'language' => $self->language(),
397 'description' => $self->description(),
398 'rating' => $self->rating(),
399 'copyright' => $self->copyright(),
400 'pubDate' => ( scalar( localtime() ) ),
401 'lastBuildDate' => ( scalar( localtime() ) ),
402 'docs' => $self->docs(),
403 'ttl' => $self->ttl(),
404 'generator' => $self->generator(),
405 'managingEditor' => $self->managing_editor(),
406 'webMaster' => $self->webmaster(),
407 );
408  
409 foreach my $single_item ( @{ $self->get_items_to_include( \%args ) } )
410 {
411 $self->add_item_to_rss_feed(
412 'item' => $single_item,
413 'feed' => $rss_feed,
414 );
415 }
416  
417 my $filename = $args{'output_filename'} || "rss.xml";
418  
419 $rss_feed->save($filename);
420 }
421  
422  
423 sub get_navmenu_items
424 {
425 my $self = shift;
426 my %args = (@_);
427  
428 my @ret;
429  
430 foreach my $single_item (
431 reverse( @{ $self->get_items_to_include( \%args ) } ) )
432 {
433 push @ret,
434 {
435 'text' => $single_item->title(),
436 'url' => $self->get_item_rel_url($single_item),
437 };
438 }
439 return \@ret;
440 }
441  
442  
443 sub format_news_page_item
444 {
445 my $self = shift;
446 my (%args) = (@_);
447  
448 my $item = $args{'item'};
449 my $base_url = $args{'base_url'};
450  
451 return
452 "<h3><a href=\"$base_url"
453 . $item->id() . "/\">"
454 . CGI::escapeHTML( $item->title() )
455 . "</a></h3>\n" . "<p>\n"
456 . $item->description()
457 . "\n</p>\n";
458 }
459  
460 sub get_news_page_entries
461 {
462 my $self = shift;
463 my %args = (@_);
464  
465 my $html = "";
466  
467 my $base_url = exists( $args{'base_url'} ) ? $args{'base_url'} : "";
468  
469 foreach my $single_item (
470 reverse( @{ $self->get_items_to_include( \%args ) } ) )
471 {
472 $html .= $self->format_news_page_item(
473 'item' => $single_item,
474 'base_url' => $base_url,
475 );
476 }
477 return $html;
478 }
479  
480  
481 sub get_news_box_contents
482 {
483 my $self = shift;
484 my (%args) = (@_);
485  
486 my $html = "";
487 foreach my $item ( reverse( @{ $self->get_items_to_include( \%args ) } ) )
488 {
489 $html .=
490 "<li><a href=\""
491 . $self->get_item_rel_url($item) . "\">"
492 . CGI::escapeHTML( $item->title() )
493 . "</a></li>\n";
494 }
495 return $html;
496 }
497  
498 sub get_news_box
499 {
500 my $self = shift;
501  
502 my $html = "";
503  
504 $html .= qq{<div class="news">\n};
505 $html .= qq{<h3>News</h3>\n};
506 $html .= qq{<ul>\n};
507 $html .= $self->get_news_box_contents(@_);
508 $html .= qq{<li><a href="./news/">More&hellip;</a></li>};
509 $html .= qq{</ul>\n};
510 $html .= qq{</div>\n};
511 return $html;
512 }
513  
514  
515 1;
516  
517 __END__
518  
519 =pod
520  
521 =encoding UTF-8
522  
523 =head1 NAME
524  
525 HTML::Latemp::News - News Maintenance Module for Latemp (and possibly other
526 web frameworks)
527  
528 =head1 VERSION
529  
530 version 0.1.12
531  
532 =head1 SYNOPSIS
533  
534 #!/usr/bin/perl
535  
536 use strict;
537 use warnings;
538  
539 use MyManageNews;
540  
541 my @news_items =
542 (
543 .
544 .
545 .
546 {
547 'title' => "Changes of 18-April-2005",
548 'id' => "changes-2005-04-18",
549 'description' => q{Around 18 April, 2005, Jane's Site has seen a
550 lot of changes. Click the link for details on them.},
551 'date' => "2005-04-18",
552 'author' => "Jane Smith",
553 'category' => "Jane's Site",
554 },
555 .
556 .
557 .
558 );
559  
560 my $news_manager =
561 HTML::Latemp::News->new(
562 'news_items' => \@news_items,
563 'title' => "Better SCM News",
564 'link' => "http://janes-site.tld/",
565 'language' => "en-US",
566 'copyright' => "Copyright by Jane Smith, (c) 2005",
567 'webmaster' => "Jane Smith <jane@janes-site.tld>",
568 'managing_editor' => "Jane Smith <jane@janes-site.tld>",
569 'description' => "News of Jane's Site - a personal site of " .
570 "Jane Smith",
571 );
572  
573 $news_manager->generate_rss_feed(
574 'output_filename' => "dest/rss.xml"
575 );
576  
577 1;
578  
579 =head1 DESCRIPTION
580  
581 This is a module that maintains news item for a web-site. It can generate
582 an RSS feed, as well as a news page, and an HTML newsbox, all from the same
583 data.
584  
585 =head1 VERSION
586  
587 version 0.1.12
588  
589 =head1 FUNCTION
590  
591 =head2 HTML::Latemp::News->new(...)
592  
593 This is the constructor for the news manager. It accepts the following named
594 parameters:
595  
596 =over 8
597  
598 =item 'news_items'
599  
600 This is a reference to a list of news_items. See below.
601  
602 =item 'title'
603  
604 The title of the RSS feed.
605  
606 =item 'link'
607  
608 The link to the homepage of the site.
609  
610 =item 'language'
611  
612 The language of the text.
613  
614 =item 'copyright'
615  
616 The copyright notice of the text.
617  
618 =item 'webmaster'
619  
620 The Webmaster.
621  
622 =item 'managing_editor'
623  
624 The managing editor.
625  
626 =item 'description'
627  
628 A description of the news feed as will be put in the RSS feed.
629  
630 =back
631  
632 =head3 Format of the news_items
633  
634 The news_items is a reference to an array, of which each element is a hash
635 reference. The hash may contain the following keys:
636  
637 =over 8
638  
639 =item 'title'
640  
641 The title of the item.
642  
643 =item 'id'
644  
645 The ID of the item. This will also be used to calculate URLs.
646  
647 =item 'description'
648  
649 A text description explaining what the item is all about.
650  
651 =item 'author'
652  
653 The author of the item.
654  
655 =item 'date'
656  
657 A string representing the daet.
658  
659 =item 'category'
660  
661 The cateogry of the item.
662  
663 =back
664  
665 =head2 $news_manager->generate_rss_feed('output_filename' => "rss.xml")
666  
667 This generates an RSS feed. It accepts two named arguments.
668 C<'output_filename'> is the name of the RSS file to write to. C<'num_items'>
669 is the number of items to include, which defaults to 10.
670  
671 =head2 $news_manager->get_navmenu_items('num_items' => 5)
672  
673 This generates navigation menu items for input to the navigation menu of
674 L<HTML::Widgets::NavMenu>. It accepts a named argument C<'num_items'> which
675 defaults to 10.
676  
677 =head2 $news_manager->get_news_page_entries('num_items' => 5, 'base_url' => "news/")
678  
679 This generates HTML for the news page. 'base_url' points to a URL to be
680 appended to each item's ID.
681  
682 =head2 $news_manager->get_news_box('num_items' => 5)
683  
684 This generates an HTML news box with the recent headlines.
685  
686 =head1 AUTHOR
687  
688 Shlomi Fish, L<http://www.shlomifish.org/> .
689  
690 =head1 BUGS
691  
692 Please report any bugs or feature requests to
693 C<bug-html-latemp-news@rt.cpan.org>, or through the web interface at
694 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Latemp-News>.
695 I will be notified, and then you'll automatically be notified of progress on
696 your bug as I make changes.
697  
698 =head1 SEE ALSO
699  
700 L<XML::RSS>, L<HTML::Widgets::NavMenu>.
701  
702 =head1 ACKNOWLEDGEMENTS
703  
704 =head1 COPYRIGHT & LICENSE
705  
706 Copyright 2005 Shlomi Fish, All Rights Reserved.
707  
708 This program is free software; you can redistribute it and/or modify it
709 under the terms of the MIT X11 license.
710  
711 =head1 AUTHOR
712  
713 Shlomi Fish <shlomif@cpan.org>
714  
715 =head1 COPYRIGHT AND LICENSE
716  
717 This software is Copyright (c) 2005 by Shlomi Fish.
718  
719 This is free software, licensed under:
720  
721 The MIT (X11) License
722  
723 =head1 BUGS
724  
725 Please report any bugs or feature requests on the bugtracker website
726 L<https://github.com/shlomif/html-latemp-news/issues>
727  
728 When submitting a bug or request, please include a test-file or a
729 patch to an existing test-file that illustrates the bug or desired
730 feature.
731  
732 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
733  
734 =head1 SUPPORT
735  
736 =head2 Perldoc
737  
738 You can find documentation for this module with the perldoc command.
739  
740 perldoc HTML::Latemp::News
741  
742 =head2 Websites
743  
744 The following websites have more information about this module, and may be of help to you. As always,
745 in addition to those websites please use your favorite search engine to discover more resources.
746  
747 =over 4
748  
749 =item *
750  
751 MetaCPAN
752  
753 A modern, open-source CPAN search engine, useful to view POD in HTML format.
754  
755 L<https://metacpan.org/release/HTML-Latemp-News>
756  
757 =item *
758  
759 Search CPAN
760  
761 The default CPAN search engine, useful to view POD in HTML format.
762  
763 L<http://search.cpan.org/dist/HTML-Latemp-News>
764  
765 =item *
766  
767 RT: CPAN's Bug Tracker
768  
769 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
770  
771 L<https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-Latemp-News>
772  
773 =item *
774  
775 AnnoCPAN
776  
777 The AnnoCPAN is a website that allows community annotations of Perl module documentation.
778  
779 L<http://annocpan.org/dist/HTML-Latemp-News>
780  
781 =item *
782  
783 CPAN Ratings
784  
785 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
786  
787 L<http://cpanratings.perl.org/d/HTML-Latemp-News>
788  
789 =item *
790  
791 CPANTS
792  
793 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
794  
795 L<http://cpants.cpanauthors.org/dist/HTML-Latemp-News>
796  
797 =item *
798  
799 CPAN Testers
800  
801 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
802  
803 L<http://www.cpantesters.org/distro/H/HTML-Latemp-News>
804  
805 =item *
806  
807 CPAN Testers Matrix
808  
809 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
810  
811 L<http://matrix.cpantesters.org/?dist=HTML-Latemp-News>
812  
813 =item *
814  
815 CPAN Testers Dependencies
816  
817 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
818  
819 L<http://deps.cpantesters.org/?module=HTML::Latemp::News>
820  
821 =back
822  
823 =head2 Bugs / Feature Requests
824  
825 Please report any bugs or feature requests by email to C<bug-html-latemp-news at rt.cpan.org>, or through
826 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=HTML-Latemp-News>. You will be automatically notified of any
827 progress on the request by the system.
828  
829 =head2 Source Code
830  
831 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
832 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
833 from your repository :)
834  
835 L<https://github.com/shlomif/html-latemp-news>
836  
837 git clone git://github.com/shlomif/html-latemp-news.git
838  
839 =cut
↑ Summary Build »