domain support for feeds, factor out sc-specific bits
[scpubgit/SCS.git] / lib / SCSite / FeedGenerator.pm
index e8e28c1..104b8cc 100644 (file)
@@ -1,48 +1,62 @@
 package SCSite::FeedGenerator;
 
+use URI;
 use Moo;
 no warnings 'once';
 
-has id_prefix => (is => 'ro', required => 1);
 has pages => (is => 'ro', required => 1);
 
 sub feed_http_response {
-  my ($self, $code, $feed_config) = @_;
+  my ($self, $code, $feed_config, $env) = @_;
   $self->_feed_response(
-    $code, $self->_config_to_data($feed_config)
+    $code, $self->_config_to_data($feed_config, $env)
   );
 }
 
 sub _config_to_data {
-  my ($self, $config) = @_;
+  my ($self, $config, $env) = @_;
+  my $url_scheme = $env->{'psgi.url_scheme'} || "http";
+  my $url_port = $env->{SERVER_PORT}||80;
+  my $base_url = URI->new(
+    $url_scheme
+    .'://'
+    .($env->{HTTP_HOST}
+      or (
+        ($env->{SERVER_NAME} || "")
+        .($url_scheme eq 'http' && $url_port == 80
+           ? ''
+           : ":${url_port}"
+         )
+      ))
+    .($env->{SCRIPT_NAME} || '/')
+  );
+  my $abs = sub { URI->new_abs($_[0], $base_url)->as_string };
   my $base_page = $self->pages->get({ path => $config->{base} });
   my @entry_pages = $base_page->children(%{$config->{entries}})
                               ->latest(10)->flatten;
   my $updated = (sort map $_->created, @entry_pages)[-1];
   +{
      %$config,
-     id => $self->_id_for($base_page->path),
-     web_url => $base_page->path.'/',
-     feed_url => "/feed/${\$config->{base}}/",
+     id => $abs->("feed/${\$config->{base}}/"),
+     web_url => $abs->($config->{base}.'/'),
+     feed_url => $abs->("feed/${\$config->{base}}/"),
      updated => join('T', split(' ',$updated)).'Z',
-     entries => [ map +{
-       title => $_->title,
-       summary_html => do {
-         use HTML::Tags;
-         join '', HTML::Tags::to_html_string(<p>, $_->description, </p>)
-       },
-       content_html => $_->body,
-       created => join('T', split(' ',$_->created)).'Z',
-       web_url => $_->path,
-     }, @entry_pages ],
+     entries => [ map {
+      my $page_url = $abs->(do { (my $p = $_->path) =~ s/^\///; "$p/" });
+      +{
+         title => $_->title,
+         summary_html => do {
+           use HTML::Tags;
+           join '', HTML::Tags::to_html_string(<p>, $_->description, </p>)
+         },
+         content_html => $self->_absolutify_html($_->body, $base_url, $page_url),
+         created => join('T', split(' ',$_->created)).'Z',
+         web_url => $page_url,
+       }
+    } @entry_pages ],
   }
 }
 
-sub _id_for {
-  my ($self, $for) = @_;
-  join '', $self->id_prefix, $for;
-}
-
 sub _feed_response {
   my ($self, $code, $data) = @_;
   [ $code,
@@ -88,7 +102,7 @@ sub _entry_data_to_tags {
     '    ', <title>, $data->{title}, </title>, "\n",
     '    ', <author>, <name>, "Shadowcat Staff", </name>, </author>, "\n",
     '    ', <link href="${web_url}" />, "\n",
-    '    ', <id>, $self->_id_for($data->{web_url}), </id>, "\n",
+    '    ', <id>, $web_url, </id>, "\n",
     '    ', <published>, $data->{created}, </published>, "\n",
     '    ', <updated>, ($data->{created}||$data->{updated}), </updated>, "\n",
     ($data->{summary_html}
@@ -99,4 +113,9 @@ sub _entry_data_to_tags {
   '  ', </entry>, "\n";
 }
 
+sub _absolutify_html {
+  my ($self, $html, $base_url, $page_url) = @_;
+  $html;
+}
+
 1;