use App::SCS::Page;
use IO::All;
use Try::Tiny;
-use List::Util qw(reduce);
-use JSON;
+use List::Util qw(reduce max);
+use Module::Runtime qw(use_module);
+use JSON::MaybeXS;
use Moo;
+use Hash::Merge qw(merge);
+use Data::Pond qw(pond_read_datum pond_write_datum);
+use JSONY;
with 'App::SCS::Role::PageChildren';
->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
}
+sub _page_set { $_[0] }
sub _page_set_class { ref($_[0]) }
sub _top_dir { shift->top_dir }
sub _my_path { shift->base_dir }
);
}
+sub _config_files_for {
+ my ($self, $path) = @_;
+
+ my @files = ();
+ my @dirs = io->dir($path)->splitdir;
+
+ shift @dirs;
+ pop @dirs;
+ my $build_path = io('');
+
+ foreach my $dir (@dirs) {
+ $build_path = $build_path->catdir("/$dir");
+ #/home/.../share/pages/blog.conf etc
+
+ my $file = $self->_top_dir->catfile("$build_path.conf");
+
+ if (!$file->exists || !$file->file || $file->empty) {
+ next;
+ }
+
+ push @files, $file;
+ }
+
+ return \@files;
+}
+
sub _inflate {
my ($self, $type, $path, $io) = @_;
(my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
my $cache = io($cache_name);
+ my $config_files = $self->_config_files_for($path);
+ my $max_stat = max map $_->mtime, $io, @$config_files;
+
if (-f $cache_name) {
- if ($cache->mtime >= $io->mtime) {
+ if ($cache->mtime >= $max_stat) {
return try {
$self->_new_page($path, $self->_json->decode($cache->all));
} catch {
}
my $raw = $io->all;
try {
+
my $extracted = $self->${\"_extract_from_${type}"}($raw);
- try { $cache->print($self->_json->encode($extracted)); };
- $self->_new_page($path, $extracted);
+ my $jsony = JSONY->new;
+ my $config = reduce { merge($a, $jsony->load($b->all)) } [], @$config_files;
+
+ $extracted->{plugins} = pond_read_datum('[' . $extracted->{plugins} . ']');
+
+ my $setup = $extracted;
+
+ $setup->{plugin_config} = merge($extracted->{plugins}, $config);
+
+ try {
+ my $tmp_cache = io($cache_name . ".tmp");
+ $tmp_cache->print($self->_json->encode($setup));
+ $tmp_cache->rename($cache_name);
+ };
+
+ $self->_new_page($path, $setup);
} catch {
die "Error inflating ${path} as ${type}: $_\n";
}
sub latest {
my ($self, $max) = @_;
- require SCSite::LatestPageSet;
- SCSite::LatestPageSet->new(
+ use_module('App::SCS::LatestPageSet')->new(
parent => $self,
max_entries => $max,
);
}
sub _new_page {
- SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
+ use_module('App::SCS::Page')->new(
+ path => $_[1], page_set => $_[0], %{$_[2]}
+ );
}
sub _types_re { qw/\.(html|md)/ }
sub _extract_from_md {
my ($self, $md) = @_;
- #warn markdown($md, { document_format => 'complete' });
$self->_extract_from_html(markdown($md, { document_format => 'complete' }));
}