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