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