| 1 | # ABSTRACT: Atom feed generator | ||
| 2 | |||
| 3 | |||
| 4 | package HiD::Generator::AtomFeed; | ||
| 5 | $HiD::Generator::AtomFeed::VERSION = '1.5'; | ||
| 6 | use Moose; | ||
| 7 | with 'HiD::Generator'; | ||
| 8 | |||
| 9 | use DateTime; | ||
| 10 | use HiD::VirtualPage; | ||
| 11 | use XML::Atom::Entry; | ||
| 12 | use XML::Atom::Feed; | ||
| 13 | use XML::Atom::Link; | ||
| 14 | use XML::Atom::Person; | ||
| 15 | |||
| 16 | has 'destination' => ( | ||
| 17 | is => 'ro' , | ||
| 18 | isa => 'HiD_DirPath' , | ||
| 19 | ); | ||
| 20 | |||
| 21 | |||
| 22 | sub generate { | ||
| 23 | my( $self , $site ) = @_; | ||
| 24 | |||
| 25 | return unless | ||
| 26 | my $destination = $site->config->{atom_feed}; | ||
| 27 | |||
| 28 | my $post_limit = $site->config->{atom_feed_posts} // 20; | ||
| 29 | my $post_count = 1; | ||
| 30 | |||
| 31 | my $feed = $self->_new_feed($site); | ||
| 32 | |||
| 33 | POST: for my $post( @{ $site->posts }) { | ||
| 34 | $feed->add_entry($self->_new_entry($post)); | ||
| 35 | |||
| 36 | $post_count++; | ||
| 37 | last POST if $post_count > $post_limit; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $feed_page = HiD::VirtualPage->new({ | ||
| 41 | output_filename => $site->destination . $destination , | ||
| 42 | content => $feed->as_xml , | ||
| 43 | }); | ||
| 44 | |||
| 45 | $site->add_input( "ATOM FEED" => 'page' ); | ||
| 46 | $site->add_object( $feed_page ); | ||
| 47 | |||
| 48 | $site->INFO( "* Injected Atom feed"); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _new_entry { | ||
| 52 | my( $self , $post ) = @_; | ||
| 53 | |||
| 54 | my $author = XML::Atom::Person->new(); | ||
| 55 | $author->name($post->author); | ||
| 56 | |||
| 57 | my $link = XML::Atom::Link->new(); | ||
| 58 | $link->type('text/html'); | ||
| 59 | $link->rel('alternate'); | ||
| 60 | $link->href($post->url); | ||
| 61 | |||
| 62 | my $entry = XML::Atom::Entry->new(); | ||
| 63 | $entry->title($post->title); | ||
| 64 | $entry->author($author); | ||
| 65 | $entry->add_link($link); | ||
| 66 | $entry->content($post->converted_content); | ||
| 67 | |||
| 68 | return $entry; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _new_feed { | ||
| 72 | my( $self , $site ) = @_; | ||
| 73 | |||
| 74 | my $feed = XML::Atom::Feed->new(); | ||
| 75 | my $title = $site->config->{atom_feed_title} // $site->config->{title}; | ||
| 76 | $feed->title( $title ); | ||
| 77 | |||
| 78 | if ( my $base_url = $site->config->{atom_feed_base} ) { | ||
| 79 | my $base_link = XML::Atom::Link->new(); | ||
| 80 | $base_link->type('text/html'); | ||
| 81 | $base_link->rel('alternate'); | ||
| 82 | $base_link->href($base_url); | ||
| 83 | |||
| 84 | $feed->add_link( $base_link ); | ||
| 85 | } | ||
| 86 | |||
| 87 | if ( my $feed_url = $site->config->{atom_feed_link} ) { | ||
| 88 | my $feed_link = XML::Atom::Link->new(); | ||
| 89 | $feed_link->type('application/atom+xml'); | ||
| 90 | $feed_link->rel('self'); | ||
| 91 | $feed_link->href( $feed_url ); | ||
| 92 | |||
| 93 | $feed->add_link( $feed_link ); | ||
| 94 | |||
| 95 | $feed->id( $feed_link ); | ||
| 96 | } | ||
| 97 | |||
| 98 | $feed->updated(DateTime->now()); | ||
| 99 | |||
| 100 | return $feed; | ||
| 101 | } | ||
| 102 | |||
| 103 | __PACKAGE__->meta->make_immutable; | ||
| 104 | 1; | ||
| 105 | |||
| 106 | __END__ | ||
| 107 | |||
| 108 | =pod | ||
| 109 | |||
| 110 | =encoding utf-8 | ||
| 111 | |||
| 112 | =head1 NAME | ||
| 113 | |||
| 114 | HiD::Generator::AtomFeed - Atom feed generator | ||
| 115 | |||
| 116 | =head1 DESCRIPTION | ||
| 117 | |||
| 118 | This Generator produces an Atom feed of your posts. | ||
| 119 | |||
| 120 | Enable it by setting the 'atom_feed' key in your config to the path where the | ||
| 121 | feed should be generated. | ||
| 122 | |||
| 123 | The 'atom_feed_base' and 'atom_feed_link' keys, if they exist, will be used to | ||
| 124 | add the 'rel=altertate' base link and the 'rel=self' feed link to the atom | ||
| 125 | feed, respectively. | ||
| 126 | |||
| 127 | =head1 METHODS | ||
| 128 | |||
| 129 | =head2 generate | ||
| 130 | |||
| 131 | =head1 VERSION | ||
| 132 | |||
| 133 | version 1.5 | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | John SJ Anderson <genehack@genehack.org> | ||
| 138 | |||
| 139 | =head1 COPYRIGHT AND LICENSE | ||
| 140 | |||
| 141 | This software is copyright (c) 2014 by John SJ Anderson. | ||
| 142 | |||
| 143 | This is free software; you can redistribute it and/or modify it under | ||
| 144 | the same terms as the Perl 5 programming language system itself. | ||
| 145 | |||
| 146 | =cut |