1 package App::SCS::Plugin::Feeds::Generator;
7 has pages => (is => 'ro', required => 1);
9 has mounted_at => (is => 'ro', required => 1);
11 sub feed_http_response {
12 my ($self, $code, $feed_config, $env) = @_;
13 $self->_feed_response(
14 $code, $self->_config_to_data($feed_config, $env)
19 my ($self, $config, $env) = @_;
20 my $url_scheme = $env->{'psgi.url_scheme'} || "http";
21 my $url_port = $env->{SERVER_PORT}||80;
22 my $base_url = URI->new(
27 ($env->{SERVER_NAME} || "")
28 .($url_scheme eq 'http' && $url_port == 80
33 .($env->{SCRIPT_NAME} || '/')
35 my $abs = sub { URI->new_abs($_[0], $base_url)->as_string };
36 my $base_page = $self->pages->get({ path => $config->{base} });
37 my @entry_pages = $base_page->children(%{$config->{entries}})
38 ->latest(10)->flatten;
39 my $updated = (sort map $_->created, @entry_pages)[-1];
40 my $base = $self->mounted_at;
43 id => $abs->("${base}/${\$config->{base}}/"),
44 web_url => $abs->($config->{base}.'/'),
45 feed_url => $abs->("${base}/${\$config->{base}}/"),
46 updated => join('T', split(' ',$updated)).'Z',
48 my $page_url = $abs->(do { (my $p = $_->path) =~ s/^\///; "$p/" });
53 join '', HTML::Tags::to_html_string(<p>, $_->description, </p>)
55 content_html => $self->_content_html($_, $base_url, $page_url),
56 created => join('T', split(' ',$_->created)).'Z',
64 my ($self, $code, $data) = @_;
66 [ 'Content-type' => 'application/atom+xml' ],
67 [ $self->_feed_string($data) ]
72 my ($self, $data) = @_;
73 XML::Tags::to_xml_string(
74 $self->_feed_data_to_tags($data)
78 sub _feed_data_to_tags {
79 my ($self, $data) = @_;
81 feed title subtitle link id
83 my ($web_url, $feed_url) = @{$data}{qw(web_url feed_url)};
84 (\'<?xml version="1.0" encoding="UTF-8"?>', "\n",
85 <feed xmlns="http://www.w3.org/2005/Atom">, "\n",
86 ' ', <title type="text">, $data->{title}, </title>, "\n",
88 ? (' ', <subtitle type="text">, $data->{subtitle}, </subtitle>, "\n",)
90 ' ', <link rel="alternate" type="text/html" href="${web_url}" />, "\n",
91 ' ', <link rel="self" type="application/atom+xml" href="${feed_url}" />, "\n",
92 ' ', <updated>, $data->{updated}, </updated>, "\n",
93 ' ', <id>, $data->{id}, </id>, "\n",
94 (map $self->_entry_data_to_tags($_), @{$data->{entries}}),
98 sub _entry_data_to_tags {
99 my ($self, $data) = @_;
101 entry title author name link id published updated summary content
103 my $web_url = $data->{web_url};
105 ' ', <title>, $data->{title}, </title>, "\n",
106 ' ', <author>, <name>, "Shadowcat Staff", </name>, </author>, "\n",
107 ' ', <link href="${web_url}" />, "\n",
108 ' ', <id>, $web_url, </id>, "\n",
109 ' ', <published>, $data->{created}, </published>, "\n",
110 ' ', <updated>, ($data->{created}||$data->{updated}), </updated>, "\n",
111 ($data->{summary_html}
112 ? (' ', <summary type="html">, \('<![CDATA['.$data->{summary_html}.']]>'), </summary>, "\n")
115 ' ', <content type="html">, \('<![CDATA['.$data->{content_html}.']]>'), </content>, "\n",
120 my ($self, $page, $base_url, $page_url) = @_;
122 HTML::Zoom->from_html($page->html)
123 ->collect(body => { into => \@ev })
125 HTML::Zoom->from_events(\@ev)->to_html;