proposed cleanup of _config_files_dor
[scpubgit/App-SCS.git] / lib / App / SCS / PageSet.pm
CommitLineData
632f0e07 1package App::SCS::PageSet;
2
3use Text::MultiMarkdown 'markdown';
4use HTML::Zoom;
5use Sub::Quote;
6use Syntax::Keyword::Gather;
7use App::SCS::Page;
8use IO::All;
9use Try::Tiny;
fd5c2ec2 10use List::Util qw(reduce max);
0034f151 11use Module::Runtime qw(use_module);
f4837d95 12use JSON::MaybeXS;
632f0e07 13use Moo;
fd5c2ec2 14use Hash::Merge qw(merge);
15use Data::Pond qw(pond_read_datum pond_write_datum);
16use JSONY;
632f0e07 17
18with 'App::SCS::Role::PageChildren';
19
20{
21 my $j = JSON->new;
22 sub _json { $j }
23}
24
25has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
26has base_dir => (is => 'ro', required => 1);
27has plugin_config => (is => 'ro', required => 1);
28has max_depth => (is => 'ro', default => quote_sub q{ 0 });
29has min_depth => (is => 'ro', default => quote_sub q{ 1 });
30
31has rel_path => (is => 'lazy');
32
33sub _build_rel_path {
34 my ($self) = @_;
35 io->dir('/')
36 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
37}
38
f50b4a35 39sub _page_set { $_[0] }
632f0e07 40sub _page_set_class { ref($_[0]) }
41sub _top_dir { shift->top_dir }
42sub _my_path { shift->base_dir }
43
44sub get {
45 my ($self, $spec) = @_;
46 $spec->{path} or die "path is required to get";
47 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
48 my $type;
49 my @poss = io->dir($self->base_dir)->${\sub {
50 my $io = shift;
51 defined($dir) ? $io->catdir($dir) : $io
52 }}->filter(sub {
53 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
54 })
55 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
56 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
57 if @poss > 1;
58 return undef unless @poss;
59 $self->_inflate(
60 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
61 );
62}
63
b9dd3724 64sub _config_files_for {
fd5c2ec2 65 my ($self, $path) = @_;
66
67 my @files = ();
da974328 68 my @dir_parts = io->dir($path)->splitdir;
fd5c2ec2 69
da974328 70 my @dirs = map io->dir('')->catdir(@dir_parts[1..$_]), 1..($#dir_parts - 1);
fd5c2ec2 71
da974328 72 return grep +($_->file and not $_->empty),
73 map $self->_top_dir->catfile("${_}.conf"), @dirs;
fd5c2ec2 74}
75
632f0e07 76sub _inflate {
77 my ($self, $type, $path, $io) = @_;
78 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
79 my $cache = io($cache_name);
b9dd3724 80 my $config_files = $self->_config_files_for($path);
2f5c23b3 81 my $max_stat = max map $_->mtime, $io, @$config_files;
fd5c2ec2 82
632f0e07 83 if (-f $cache_name) {
2f5c23b3 84 if ($cache->mtime >= $max_stat) {
632f0e07 85 return try {
86 $self->_new_page($path, $self->_json->decode($cache->all));
87 } catch {
88 die "Error inflating ${path} from cache: $_\n";
89 }
90 }
91 }
92 my $raw = $io->all;
93 try {
c68d9e18 94
632f0e07 95 my $extracted = $self->${\"_extract_from_${type}"}($raw);
fd5c2ec2 96 my $jsony = JSONY->new;
c68d9e18 97 my $config = reduce { merge($a, $jsony->load($b->all)) } [], @$config_files;
fd5c2ec2 98
99 $extracted->{plugins} = pond_read_datum('[' . $extracted->{plugins} . ']');
c68d9e18 100
101 my $setup = $extracted;
102
103 $setup->{plugin_config} = merge($extracted->{plugins}, $config);
fd5c2ec2 104
105 try {
106 my $tmp_cache = io($cache_name . ".tmp");
107 $tmp_cache->print($self->_json->encode($setup));
108 $tmp_cache->rename($cache_name);
109 };
110
111 $self->_new_page($path, $setup);
632f0e07 112 } catch {
113 die "Error inflating ${path} as ${type}: $_\n";
114 }
115}
116
117sub map {
118 my ($self, $mapper) = @_;
119 [ map $mapper->($_), $self->flatten ]
120}
121
122sub _depth_under_base {
123 my ($self, $path) = @_;
124 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
125}
126
127sub flatten {
128 my ($self) = @_;
129 my $slash = io->dir('/');
130 map {
131 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
132 $self->_inflate(
133 $type,
134 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
135 $_
136 );
137 } $self->_all_files;
138}
139
140sub all_paths {
141 my ($self) = @_;
142 my $slash = io->dir('/');
143 map {
144 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
145 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
146 } $self->_all_files;
147}
148
149sub _all_files {
150 my ($self) = @_;
151 return unless (my $base = $self->base_dir)->exists;
152 my %seen;
153 my $min = $self->min_depth;
154 map {
155 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
156 ->all_files($self->max_depth - ($min-1))
157 } map
158 $min > 1
159 ? do {
160 # can't use ->all_dirs($min-1) since we only want the final level
161 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
162 }
163 : $_,
164 $base;
165}
166
167sub latest {
168 my ($self, $max) = @_;
0034f151 169 use_module('App::SCS::LatestPageSet')->new(
632f0e07 170 parent => $self,
171 max_entries => $max,
172 );
173}
174
175sub _new_page {
0034f151 176 use_module('App::SCS::Page')->new(
177 path => $_[1], page_set => $_[0], %{$_[2]}
178 );
632f0e07 179}
180
181sub _types_re { qw/\.(html|md)/ }
182
183sub _extract_from_html {
184 my ($self, $html) = @_;
185 my %meta;
186 HTML::Zoom->from_html($html)
187 ->select('title')->collect_content({ into => \my @title })
188 ->${\sub {
189 my $z = shift;
190 return reduce {
191 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
192 } $z, qw(subtitle description keywords created plugins)
193 }}
194 ->run;
195 +{
196 title => $title[0]->{raw}||'',
197 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
198 html => $html,
199 }
200}
201
202sub _extract_from_md {
203 my ($self, $md) = @_;
632f0e07 204 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
205}
206
2071;