add pages to demo sidebar!
[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
34597fb2 11has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
95148a72 12has base_dir => (is => 'ro', required => 1);
ebd4c292 13has max_depth => (is => 'ro', default => quote_sub q{ 0 });
fc436d2e 14has min_depth => (is => 'ro', default => quote_sub q{ 0 });
95148a72 15
34597fb2 16has rel_path => (is => 'lazy');
17
18sub _build_rel_path {
19 my ($self) = @_;
20 io->dir('/')
21 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
22}
23
95148a72 24sub get {
25 my ($self, $spec) = @_;
26 $spec->{path} or die "path is required to get";
27 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
28 my $type;
5cc6d9e2 29 my @poss = io->dir($self->base_dir)->${\sub {
95148a72 30 my $io = shift;
31 defined($dir) ? $io->catdir($dir) : $io
ebd4c292 32 }}->filter(sub {
33 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
34 })
fc436d2e 35 ->${\sub { $_[0]->exists ? $_[0]->all_files : () }};
95148a72 36 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
37 if @poss > 1;
ebd4c292 38 return undef unless @poss;
34597fb2 39 $self->${\"_inflate_${type}"}(
40 $self->rel_path->catdir($spec->{path}), $poss[0]->all
41 );
ebd4c292 42}
43
44sub map {
45 my ($self, $mapper) = @_;
46 [ map $mapper->($_), $self->flatten ]
47}
48
fc436d2e 49sub _depth_under_base {
50 my ($self, $path) = @_;
51 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
52}
53
ebd4c292 54sub flatten {
55 my ($self) = @_;
fc436d2e 56 return unless (my $base = $self->base_dir)->exists;
ebd4c292 57 my %seen;
34597fb2 58 my $slash = io->dir('/');
fc436d2e 59 my $min = $self->min_depth;
60 my @dirs = map $min ? $_->all_dirs($min) : $_, $base;
ebd4c292 61 map {
62 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
63 $self->${\"_inflate_${type}"}(
fc436d2e 64 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
65 $_->all
ebd4c292 66 );
fc436d2e 67 } map {
68 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
69 ->all_files($self->max_depth - $min)
70 } map
71 $min
72 ? do {
73 # can't use ->all_dirs($min) since we only want the final level
74 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min; @x
75 }
76 : $_,
77 $self->base_dir;
ebd4c292 78}
79
80sub latest {
81 my ($self, $max) = @_;
82 require SCSite::LatestPageSet;
83 SCSite::LatestPageSet->new(
84 parent => $self,
85 max_entries => $max,
86 );
95148a72 87}
88
89sub _new_page {
ebd4c292 90 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
95148a72 91}
92
ebd4c292 93sub _types_re { qw/\.(html|md)/ }
94
95148a72 95sub _inflate_html {
ebd4c292 96 my ($self, $path, $html) = @_;
97 $self->_new_page($path, $self->_extract_from_html($html));
95148a72 98}
99
100sub _extract_from_html {
101 my ($self, $html) = @_;
102 HTML::Zoom->from_html($html)
103 ->select('title')->collect_content({ into => \my @title })
104 ->select('meta[name=description]')->collect({ into => \my @description })
105 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
ebd4c292 106 ->select('meta[name=created]')->collect({ into => \my @created })
95148a72 107 ->select('body')->collect_content({ into => \my @body })
108 ->run;
109 +{
ebd4c292 110 title => $title[0]->{raw}||'',
111 description => $description[0]->{attrs}{content}||'',
112 keywords => $keywords[0]->{attrs}{content}||'',
113 created => $created[0]->{attrs}{content}||'',
114 body => HTML::Zoom->from_events(\@body)->to_html||'',
95148a72 115 }
116}
117
118sub _inflate_md {
ebd4c292 119 my ($self, $path, $md) = @_;
120 $self->_new_page($path, $self->_extract_from_md($md));
95148a72 121}
122
123sub _extract_from_md {
124 my ($self, $md) = @_;
125 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
126}
127
1281;