domain support for feeds, factor out sc-specific bits
[scpubgit/SCS.git] / lib / SCSite.pm
index 69925c2..a5266cf 100755 (executable)
@@ -26,7 +26,7 @@ sub default_config {
     pages_dir => 'share/pages',
     template_dir => 'share/templates',
     static_dir => 'share/static',
-    feed_id_prefix => 'http://shadow.cat',
+    generate_host => 'www.example.com',
   )
 }
 
@@ -37,33 +37,17 @@ sub _build_pages {
 
 sub _build_filters {
   my ($self) = @_;
-  require SCSite::SubListFilter;
-  require SCSite::SidebarFilter;
   +{
-    map +($_ => "SCSite::${_}Filter"->new_from_site($self)),
-      qw(SubList Sidebar)
+    map {
+      require "SCSite/${_}Filter.pm";
+      +($_ => "SCSite::${_}Filter"->new_from_site($self))
+    } @{$self->config->{filters}}
   }
 }
 
 sub _build__feed_configs {
-  my $f = +{
-    'blog' => {
-      title => 'All Shadowcat blogs',
-      entries => { min_depth => 2, max_depth => 3 },
-    },
-    'blog/matt-s-trout' => {
-      title => q{Matt S Trout (mst)'s blog},
-      entries => { at_depth => 1 },
-    },
-    'blog/mark-keating' => {
-      title => q{Mark Keating (mdk)'s blog},
-      entries => { min_depth => 1, max_depth => 2 },
-    },
-    'news' => {
-      title => 'Shadowcat News',
-      entries => { at_depth => 4 },
-    },
-  };
+  my ($self) = @_;
+  my $f = $self->config->{feeds}||{};
   $f->{$_}{base} ||= $_ for keys %$f;
   $f;
 }
@@ -73,7 +57,6 @@ sub _build__feed_generator {
   require SCSite::FeedGenerator;
   SCSite::FeedGenerator->new(
     pages => $self->pages,
-    id_prefix => $self->config->{feed_id_prefix},
   );
 }
 
@@ -81,7 +64,7 @@ sub dispatch_request {
   my $self = shift;
   sub (/feed/**/) {
     if (my $conf = $self->_feed_configs->{$_[1]}) {
-      $self->_feed_http_response(200 => $conf);
+      $self->_feed_http_response(200 => $conf => $_[PSGI_ENV]);
     }
   },
   sub (/) {
@@ -157,16 +140,16 @@ sub _build__layout_zoom {
 
 sub run_if_script {
   return $_[0]->to_psgi_app if caller(1);
-  my $class = shift;
-  my @config_keys = keys %{{$class->default_config}};
+  my $self = ref($_[0]) ? $_[0] : $_[0]->new;
+  my @config_keys = keys %{{$self->default_config}};
   require Getopt::Long;
   my %config = map +($_ => $ENV{"SCS_${\uc $_}"}), @config_keys;
   Getopt::Long::GetOptions(
     map +("$_=s" => \$config{$_}), @config_keys
   );
   delete $config{$_} for grep !defined($config{$_}), keys %config;
-  my $new = $class->new(config => \%config);
-  $new->run(@_)
+  @{$self->config}{keys %config} = values %config;
+  $self->run(@_)
 }
 
 around _run_cli => sub {
@@ -196,22 +179,23 @@ sub _run_cli_generate {
     if (@spec) { '^('.join('|',map quotemeta($_),@spec).')' }
     else { '.' }
   };
+  my $prefix = 'http://'.$self->config->{generate_host};
   foreach my $path ('', $self->pages->all_paths) {
     next unless "$path/" =~ /$check/;
-    print "Generating ${path}\n";
+    print "Generating page ${path}\n";
     my $dir = $out->catdir($path);
     $dir->mkpath;
     $dir->catfile('index.html')->print(
-      $self->run_test_request(GET => "$path/")->content
+      $self->run_test_request(GET => "${prefix}${path}/")->content
     );
   }
   foreach my $path (map "/feed/$_/", keys %{$self->_feed_configs}) {
     next unless "$path/" =~ /$check/;
-    print "Generating ${path}\n";
+    print "Generating feed ${path}\n";
     my $dir = $out->catdir($path);
     $dir->mkpath;
     $dir->catfile('index.atom')->print(
-      $self->run_test_request(GET => $path)->content
+      $self->run_test_request(GET => "${prefix}${path}")->content
     );
   }
 }