start of SubList filter code
[scpubgit/SCS.git] / lib / SCSite / PageSet.pm
CommitLineData
95148a72 1package SCSite::PageSet;
2
95148a72 3use IO::All;
4use Text::MultiMarkdown 'markdown';
5use HTML::Zoom;
ebd4c292 6use Sub::Quote;
7use Syntax::Keyword::Gather;
8use SCSite::Page;
95148a72 9use Moo;
10
11has base_dir => (is => 'ro', required => 1);
ebd4c292 12has max_depth => (is => 'ro', default => quote_sub q{ 0 });
95148a72 13
14sub 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;
5cc6d9e2 19 my @poss = io->dir($self->base_dir)->${\sub {
95148a72 20 my $io = shift;
21 defined($dir) ? $io->catdir($dir) : $io
ebd4c292 22 }}->filter(sub {
23 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
24 })
95148a72 25 ->all_files;
26 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
27 if @poss > 1;
ebd4c292 28 return undef unless @poss;
29 $self->${\"_inflate_${type}"}($spec->{path}, $poss[0]->all);
30}
31
32sub map {
33 my ($self, $mapper) = @_;
34 [ map $mapper->($_), $self->flatten ]
35}
36
37sub 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
50sub latest {
51 my ($self, $max) = @_;
52 require SCSite::LatestPageSet;
53 SCSite::LatestPageSet->new(
54 parent => $self,
55 max_entries => $max,
56 );
95148a72 57}
58
59sub _new_page {
ebd4c292 60 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
95148a72 61}
62
ebd4c292 63sub _types_re { qw/\.(html|md)/ }
64
95148a72 65sub _inflate_html {
ebd4c292 66 my ($self, $path, $html) = @_;
67 $self->_new_page($path, $self->_extract_from_html($html));
95148a72 68}
69
70sub _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 })
ebd4c292 76 ->select('meta[name=created]')->collect({ into => \my @created })
95148a72 77 ->select('body')->collect_content({ into => \my @body })
78 ->run;
79 +{
ebd4c292 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||'',
95148a72 85 }
86}
87
88sub _inflate_md {
ebd4c292 89 my ($self, $path, $md) = @_;
90 $self->_new_page($path, $self->_extract_from_md($md));
95148a72 91}
92
93sub _extract_from_md {
94 my ($self, $md) = @_;
95 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
96}
97
981;