start of SubList filter code
[scpubgit/SCS.git] / lib / SCSite / PageSet.pm
1 package SCSite::PageSet;
2
3 use IO::All;
4 use Text::MultiMarkdown 'markdown';
5 use HTML::Zoom;
6 use Sub::Quote;
7 use Syntax::Keyword::Gather;
8 use SCSite::Page;
9 use Moo;
10
11 has base_dir => (is => 'ro', required => 1);
12 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
13
14 sub get {
15   my ($self, $spec) = @_;
16   $spec->{path} or die "path is required to get";
17   my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
18   my $type;
19   my @poss = io->dir($self->base_dir)->${\sub {
20     my $io = shift;
21     defined($dir) ? $io->catdir($dir) : $io
22   }}->filter(sub {
23         $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
24       })
25     ->all_files;
26   die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
27     if @poss > 1;
28   return undef unless @poss;
29   $self->${\"_inflate_${type}"}($spec->{path}, $poss[0]->all);
30 }
31
32 sub map {
33   my ($self, $mapper) = @_;
34   [ map $mapper->($_), $self->flatten ]
35 }
36
37 sub flatten {
38   my ($self) = @_;
39   my %seen;
40   map {
41     my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
42     $self->${\"_inflate_${type}"}(
43       File::Spec->abs2rel($path, $self->base_dir->name), $_->all
44     );
45   } io->dir($self->base_dir)
46       ->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
47       ->all_files($self->max_depth)
48 }
49
50 sub latest {
51   my ($self, $max) = @_;
52   require SCSite::LatestPageSet;
53   SCSite::LatestPageSet->new(
54     parent => $self,
55     max_entries => $max,
56   );
57 }
58
59 sub _new_page {
60   SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
61 }
62
63 sub _types_re { qw/\.(html|md)/ }
64
65 sub _inflate_html {
66   my ($self, $path, $html) = @_;
67   $self->_new_page($path, $self->_extract_from_html($html));
68 }
69
70 sub _extract_from_html {
71   my ($self, $html) = @_;
72   HTML::Zoom->from_html($html)
73     ->select('title')->collect_content({ into => \my @title })
74     ->select('meta[name=description]')->collect({ into => \my @description })
75     ->select('meta[name=keywords]')->collect({ into => \my @keywords })
76     ->select('meta[name=created]')->collect({ into => \my @created })
77     ->select('body')->collect_content({ into => \my @body })
78     ->run;
79   +{
80     title => $title[0]->{raw}||'',
81     description => $description[0]->{attrs}{content}||'',
82     keywords => $keywords[0]->{attrs}{content}||'',
83     created => $created[0]->{attrs}{content}||'',
84     body => HTML::Zoom->from_events(\@body)->to_html||'',
85   }
86 }
87
88 sub _inflate_md {
89   my ($self, $path, $md) = @_;
90   $self->_new_page($path, $self->_extract_from_md($md));
91 }
92
93 sub _extract_from_md {
94   my ($self, $md) = @_;
95   $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
96 }
97
98 1;