domain support for feeds, factor out sc-specific bits
[scpubgit/SCS.git] / lib / SCSite.pm
old mode 100644 (file)
new mode 100755 (executable)
index 7f6577b..a5266cf
@@ -1,5 +1,9 @@
+#!/usr/bin/env perl
+
 package SCSite;
 
+our $VERSION = '0.001001'; # 0.1.1
+
 use IO::All;
 use SCSite::PageSet;
 use Web::Simple;
@@ -22,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',
   )
 }
 
@@ -33,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;
 }
@@ -69,7 +57,6 @@ sub _build__feed_generator {
   require SCSite::FeedGenerator;
   SCSite::FeedGenerator->new(
     pages => $self->pages,
-    id_prefix => $self->config->{feed_id_prefix},
   );
 }
 
@@ -77,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 (/) {
@@ -153,26 +140,65 @@ 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;
+  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 {
   my ($orig, $self) = (shift, shift);
-  if (@_ >= 2 and $_[0] eq 'dev' and $_[1] eq 'server') {
+  if (@_ >= 2 and $_[0] eq 'dev') {
     require SCSite::DevMode;
     Moo::Role->apply_roles_to_object($self, 'SCSite::DevMode');
-    return $self->_run_dev_server(@_[2..$#_]);
+    if ($self->can("_run_dev_$_[1]")) {
+      return $self->${\"_run_dev_$_[1]"}(@_[2..$#_]);
+    } else {
+      die "No such dev mode $_[1]";
+    }
+  }
+  if (@_ >= 1 and my $code = $self->can("_run_cli_$_[0]")) {
+    shift;
+    return $self->$code(@_);
   }
   return $self->$orig(@_);
 };
 
+sub _run_cli_generate {
+  my ($self, $to, @spec) = @_;
+  die "generate requires a directory to generate to"
+    unless $to and -d $to;
+  my $out = io($to);
+  my $check = do {
+    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 page ${path}\n";
+    my $dir = $out->catdir($path);
+    $dir->mkpath;
+    $dir->catfile('index.html')->print(
+      $self->run_test_request(GET => "${prefix}${path}/")->content
+    );
+  }
+  foreach my $path (map "/feed/$_/", keys %{$self->_feed_configs}) {
+    next unless "$path/" =~ /$check/;
+    print "Generating feed ${path}\n";
+    my $dir = $out->catdir($path);
+    $dir->mkpath;
+    $dir->catfile('index.atom')->print(
+      $self->run_test_request(GET => "${prefix}${path}")->content
+    );
+  }
+}
+   
+
 __PACKAGE__->run_if_script;