start of SubList filter code
[scpubgit/SCS.git] / lib / SCSite / PageSet.pm
index 559038b..f9db60d 100644 (file)
@@ -1,15 +1,15 @@
 package SCSite::PageSet;
 
-{ package SCSite::Page; use Moo;
-  has $_ => (is => 'ro') for qw(title description keywords body);
-}
-
 use IO::All;
 use Text::MultiMarkdown 'markdown';
 use HTML::Zoom;
+use Sub::Quote;
+use Syntax::Keyword::Gather;
+use SCSite::Page;
 use Moo;
 
 has base_dir => (is => 'ro', required => 1);
+has max_depth => (is => 'ro', default => quote_sub q{ 0 });
 
 sub get {
   my ($self, $spec) = @_;
@@ -19,20 +19,52 @@ sub get {
   my @poss = io->dir($self->base_dir)->${\sub {
     my $io = shift;
     defined($dir) ? $io->catdir($dir) : $io
-  }}->filter(sub { $_->filename =~ /\Q${file}\E\.(html|md)/ and $type = $1 })
+  }}->filter(sub {
+        $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
+      })
     ->all_files;
   die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
     if @poss > 1;
-  $self->${\"_inflate_${type}"}($poss[0]->all);
+  return undef unless @poss;
+  $self->${\"_inflate_${type}"}($spec->{path}, $poss[0]->all);
+}
+
+sub map {
+  my ($self, $mapper) = @_;
+  [ map $mapper->($_), $self->flatten ]
+}
+
+sub flatten {
+  my ($self) = @_;
+  my %seen;
+  map {
+    my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
+    $self->${\"_inflate_${type}"}(
+      File::Spec->abs2rel($path, $self->base_dir->name), $_->all
+    );
+  } io->dir($self->base_dir)
+      ->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
+      ->all_files($self->max_depth)
+}
+
+sub latest {
+  my ($self, $max) = @_;
+  require SCSite::LatestPageSet;
+  SCSite::LatestPageSet->new(
+    parent => $self,
+    max_entries => $max,
+  );
 }
 
 sub _new_page {
-  SCSite::Page->new($_[1])
+  SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
 }
 
+sub _types_re { qw/\.(html|md)/ }
+
 sub _inflate_html {
-  my ($self, $html) = @_;
-  $self->_new_page($self->_extract_from_html($html));
+  my ($self, $path, $html) = @_;
+  $self->_new_page($path, $self->_extract_from_html($html));
 }
 
 sub _extract_from_html {
@@ -41,19 +73,21 @@ sub _extract_from_html {
     ->select('title')->collect_content({ into => \my @title })
     ->select('meta[name=description]')->collect({ into => \my @description })
     ->select('meta[name=keywords]')->collect({ into => \my @keywords })
+    ->select('meta[name=created]')->collect({ into => \my @created })
     ->select('body')->collect_content({ into => \my @body })
     ->run;
   +{
-    title => $title[0]->{raw},
-    description => $description[0]->{attrs}{content},
-    keywords => $keywords[0]->{attrs}{content},
-    body => HTML::Zoom->from_events(\@body)->to_html,
+    title => $title[0]->{raw}||'',
+    description => $description[0]->{attrs}{content}||'',
+    keywords => $keywords[0]->{attrs}{content}||'',
+    created => $created[0]->{attrs}{content}||'',
+    body => HTML::Zoom->from_events(\@body)->to_html||'',
   }
 }
 
 sub _inflate_md {
-  my ($self, $md) = @_;
-  $self->_new_page($self->_extract_from_md($md));
+  my ($self, $path, $md) = @_;
+  $self->_new_page($path, $self->_extract_from_md($md));
 }
 
 sub _extract_from_md {