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