basic porting work for SCSite
[scpubgit/App-SCS.git] / lib / App / SCS / Plugin / Feeds / Generator.pm
1 package App::SCS::Plugin::Feeds::Generator;
2
3 use URI;
4 use Moo;
5 no warnings 'once';
6
7 has pages => (is => 'ro', required => 1);
8
9 has mounted_at => (is => 'ro', required => 1);
10
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)
15   );
16 }
17
18 sub _config_to_data {
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(
23     $url_scheme
24     .'://'
25     .($env->{HTTP_HOST}
26       or (
27         ($env->{SERVER_NAME} || "")
28         .($url_scheme eq 'http' && $url_port == 80
29            ? ''
30            : ":${url_port}"
31          )
32       ))
33     .($env->{SCRIPT_NAME} || '/')
34   );
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;
41   +{
42      %$config,
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',
47      entries => [ map {
48       my $page_url = $abs->(do { (my $p = $_->path) =~ s/^\///; "$p/" });
49       +{
50          title => $_->title,
51          summary_html => do {
52            use HTML::Tags;
53            join '', HTML::Tags::to_html_string(<p>, $_->description, </p>)
54          },
55          content_html => $self->_content_html($_, $base_url, $page_url),
56          created => join('T', split(' ',$_->created)).'Z',
57          web_url => $page_url,
58        }
59     } @entry_pages ],
60   }
61 }
62
63 sub _feed_response {
64   my ($self, $code, $data) = @_;
65   [ $code,
66     [ 'Content-type' => 'application/atom+xml' ],
67     [ $self->_feed_string($data) ]
68   ]
69 }
70
71 sub _feed_string {
72   my ($self, $data) = @_;
73   XML::Tags::to_xml_string(
74     $self->_feed_data_to_tags($data)
75   );
76 }
77
78 sub _feed_data_to_tags {
79   my ($self, $data) = @_;
80   use XML::Tags qw(
81     feed title subtitle link id
82   );
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",
87     ($data->{subtitle}
88       ? ('  ', <subtitle type="text">, $data->{subtitle}, </subtitle>, "\n",)
89       : ()),
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}}),
95   </feed>);
96 }
97
98 sub _entry_data_to_tags {
99   my ($self, $data) = @_;
100   use XML::Tags qw(
101     entry title author name link id published updated summary content
102   );
103   my $web_url = $data->{web_url};
104   '  ', <entry>, "\n",
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")
113       : ()
114     ),
115     '    ', <content type="html">, \('<![CDATA['.$data->{content_html}.']]>'), </content>, "\n",
116   '  ', </entry>, "\n";
117 }
118
119 sub _content_html {
120   my ($self, $page, $base_url, $page_url) = @_;
121   my @ev;
122   HTML::Zoom->from_html($page->html)
123             ->collect(body => { into => \@ev })
124             ->run;
125   HTML::Zoom->from_events(\@ev)->to_html;
126 }
127
128 1;