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';
);
}
-sub _get_config_files {
+sub _config_files_for {
my ($self, $path) = @_;
- my @files = ();
- my @dirs = io->dir($path)->splitdir;
+ my @dir_parts = io->dir($path)->splitdir;
+ my @dirs = map io->dir('')->catdir(@dir_parts[1..$_]), 1..($#dir_parts - 1);
-
- shift @dirs;
- pop @dirs;
- my $buildpath = io('');
-
- foreach my $dir (@dirs) {
- $buildpath = $buildpath->catdir("/$dir");
- #/home/.../share/pages/blog.conf etc
-
- my $file = $self->_top_dir->catfile("$buildpath.conf");
-
- if (!$file->exists || !$file->file || $file->empty) {
- next;
- }
-
- push @files, $file;
- }
-
- return \@files;
+ return grep +(-f $_->name and not $_->empty),
+ map $self->_top_dir->catfile("${_}.conf"), @dirs;
}
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->_get_config_files($path);
- my $maxstat = 0;
-
- if (scalar @$config_files) {
- my $maxfile = reduce { $a->mtime > $b->mtime ? $a : $b } @$config_files;
- $maxstat = $maxfile->mtime;
- }
+ my @config_files = $self->_config_files_for($path);
+ my $max_stat = max map $_->mtime, $io, @config_files;
if (-f $cache_name) {
- if ($cache->mtime >= max($io->mtime, $maxstat)) {
+ if ($cache->mtime >= $max_stat) {
return try {
$self->_new_page($path, $self->_json->decode($cache->all));
} catch {
my $extracted = $self->${\"_extract_from_${type}"}($raw);
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);
+ my $setup = {
+ %$extracted,
+ plugin_config => [
+ map @{$jsony->load($_)}, (
+ $extracted->{plugins},
+ (map $_->all, reverse @config_files), # apply config deepest first
+ )
+ ]
+ };
try {
my $tmp_cache = io($cache_name . ".tmp");
sub _extract_from_md {
my ($self, $md) = @_;
- $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
+
+ my $html = markdown($md, { document_format => 'complete' });
+
+ $html =~ s#<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">##g;
+ $self->_extract_from_html($html);
}
1;