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