blib/lib/XML/Atom/Feed/JavaScript.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 9 | 31 | 29.0 |
branch | 0 | 4 | 0.0 |
condition | 0 | 3 | 0.0 |
subroutine | 3 | 5 | 60.0 |
pod | n/a | ||
total | 12 | 43 | 27.9 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package XML::Atom::Feed::JavaScript; | ||||||
2 | |||||||
3 | 3 | 3 | 2607 | use strict; | |||
3 | 7 | ||||||
3 | 116 | ||||||
4 | 3 | 3 | 17 | use warnings; | |||
3 | 6 | ||||||
3 | 113 | ||||||
5 | 3 | 3 | 24 | use base qw( XML::Atom::Feed ); | |||
3 | 4 | ||||||
3 | 2880 | ||||||
6 | |||||||
7 | our $VERSION = 0.5; | ||||||
8 | |||||||
9 | =head1 NAME | ||||||
10 | |||||||
11 | XML::Atom::Feed::JavaScript - Atom syndication with JavaScript | ||||||
12 | |||||||
13 | =head1 SYNOPSIS | ||||||
14 | |||||||
15 | ## get an Atom feed from the network | ||||||
16 | |||||||
17 | use XML::Atom::Client | ||||||
18 | use XML::Atom::Feed::JavaScript; | ||||||
19 | |||||||
20 | my $client = XML::Atom::Client->new(); | ||||||
21 | my $feed = $client->getFeed( 'http://example.com/atom.xml' ); | ||||||
22 | print $feed->asJavascript(); | ||||||
23 | |||||||
24 | ## or get an atom feed from disk | ||||||
25 | |||||||
26 | use XML::Atom::Feed::JavaScript; | ||||||
27 | |||||||
28 | my $feed = XML::Atom::Feed->new( Stream => 'atom.xml' ); | ||||||
29 | print $feed->asJavascript(); | ||||||
30 | |||||||
31 | =head1 DESCRIPTION | ||||||
32 | |||||||
33 | XML::Atom::Feed::JavaScript exports an additional function into the XML::Atom | ||||||
34 | package for outputting Atom feeds as javascript. | ||||||
35 | |||||||
36 | =head1 FUNCTIONS | ||||||
37 | |||||||
38 | =head2 asJavascript() | ||||||
39 | |||||||
40 | Returns a XML::Atom::Feed object as a string of JavaScript code. If | ||||||
41 | you want to limit the amount of entries you can pass in an integer argument: | ||||||
42 | |||||||
43 | ## limit to first 10 entries | ||||||
44 | my $javascript = $feed->asJavascript( 10 ); | ||||||
45 | |||||||
46 | =cut | ||||||
47 | |||||||
48 | sub XML::Atom::Feed::asJavascript { | ||||||
49 | 0 | 0 | 0 | my ( $feed, $max ) = @_ or die q( can't get feed ); | |||
50 | |||||||
51 | 0 | my @entries = $feed->entries(); | |||||
52 | 0 | my $items = scalar @entries; | |||||
53 | |||||||
54 | 0 | 0 | 0 | if ( not $max or $max > $items ) { $max = $items; } | |||
0 | |||||||
55 | |||||||
56 | ## open javascript section | ||||||
57 | 0 | my $output = _jsPrint( ' ' );
|
|||||
58 | 0 | $output .= _jsPrint( ' ' .
|
|||||
59 | $feed->title() . '' ); | ||||||
60 | |||||||
61 | ## open our list | ||||||
62 | 0 | $output .= _jsPrint( '
|
|||||
63 | |||||||
64 | ## generate content for each item | ||||||
65 | 0 | foreach my $item ( @entries[ 0..$max - 1 ] ) { | |||||
66 | 0 | my $link = $item->link->href(); | |||||
67 | 0 | my $title = $item->title(); | |||||
68 | 0 | my $desc = $item->content->body(); | |||||
69 | 0 | my $data = <<"JAVASCRIPT_TEXT"; | |||||
70 | |
||||||
71 | |||||||
72 | $title | ||||||
73 | |||||||
74 | $desc | ||||||
75 | |||||||
76 | JAVASCRIPT_TEXT | ||||||
77 | 0 | $output .= _jsPrint( $data ); | |||||
78 | } | ||||||
79 | |||||||
80 | ## close our item list, and return | ||||||
81 | 0 | $output .= _jsPrint( '' ); | |||||
82 | 0 | $output .= _jsPrint( '' ); | |||||
83 | 0 | return $output; | |||||
84 | } | ||||||
85 | |||||||
86 | |||||||
87 | sub _jsPrint { | ||||||
88 | 0 | 0 | my $string = shift; | ||||
89 | 0 | $string =~ s/"/\\"/g; | |||||
90 | 0 | $string =~ s/'/\\'/g; | |||||
91 | 0 | $string =~ s/\n//g; | |||||
92 | 0 | return( "document.write('$string');\n" ); | |||||
93 | } | ||||||
94 | |||||||
95 | =head1 AUTHORS | ||||||
96 | |||||||
97 | =over 4 | ||||||
98 | |||||||
99 | =item David Jacobs |
||||||
100 | |||||||
101 | =item Ed Summers |
||||||
102 | |||||||
103 | =item Brian Cassidy |
||||||
104 | |||||||
105 | =back | ||||||
106 | |||||||
107 | =cut | ||||||
108 | |||||||
109 | 1; |