1 package App::SCS::PageSet;
3 use Text::MultiMarkdown 'markdown';
6 use Syntax::Keyword::Gather;
10 use List::Util qw(reduce max);
11 use Module::Runtime qw(use_module);
14 use Hash::Merge qw(merge);
17 with 'App::SCS::Role::PageChildren';
24 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
25 has base_dir => (is => 'ro', required => 1);
26 has plugin_config => (is => 'ro', required => 1);
27 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
28 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
30 has rel_path => (is => 'lazy');
35 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
38 sub _page_set { $_[0] }
39 sub _page_set_class { ref($_[0]) }
40 sub _top_dir { shift->top_dir }
41 sub _my_path { shift->base_dir }
44 my ($self, $spec) = @_;
45 $spec->{path} or die "path is required to get";
46 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
48 my @poss = io->dir($self->base_dir)->${\sub {
50 defined($dir) ? $io->catdir($dir) : $io
52 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
54 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
55 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
57 return undef unless @poss;
59 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
63 sub _config_files_for {
64 my ($self, $path) = @_;
66 my @dir_parts = io->dir($path)->splitdir;
67 my @dirs = map io->dir('')->catdir(@dir_parts[1..$_]), 1..($#dir_parts - 1);
69 return grep +($_->is_file and $_->exists and not $_->empty),
70 map $self->_top_dir->catfile("${_}.conf"), @dirs;
74 my ($self, $type, $path, $io) = @_;
75 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
76 my $cache = io($cache_name);
77 my @config_files = $self->_config_files_for($path);
78 my $max_stat = max map $_->mtime, $io, @config_files;
81 if ($cache->mtime >= $max_stat) {
83 $self->_new_page($path, $self->_json->decode($cache->all));
85 die "Error inflating ${path} from cache: $_\n";
92 my $extracted = $self->${\"_extract_from_${type}"}($raw);
93 my $jsony = JSONY->new;
94 my $config = reduce { merge($a, $jsony->load($b->all)) } [], @config_files;
96 $extracted->{plugins} = $jsony->load($extracted->{plugins});
98 my $setup = $extracted;
100 $setup->{plugin_config} = merge($extracted->{plugins}, $config);
103 my $tmp_cache = io($cache_name . ".tmp");
104 $tmp_cache->print($self->_json->encode($setup));
105 $tmp_cache->rename($cache_name);
108 $self->_new_page($path, $setup);
110 die "Error inflating ${path} as ${type}: $_\n";
115 my ($self, $mapper) = @_;
116 [ map $mapper->($_), $self->flatten ]
119 sub _depth_under_base {
120 my ($self, $path) = @_;
121 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
126 my $slash = io->dir('/');
128 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
131 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
139 my $slash = io->dir('/');
141 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
142 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
148 return unless (my $base = $self->base_dir)->exists;
150 my $min = $self->min_depth;
152 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
153 ->all_files($self->max_depth - ($min-1))
157 # can't use ->all_dirs($min-1) since we only want the final level
158 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
165 my ($self, $max) = @_;
166 use_module('App::SCS::LatestPageSet')->new(
173 use_module('App::SCS::Page')->new(
174 path => $_[1], page_set => $_[0], %{$_[2]}
178 sub _types_re { qw/\.(html|md)/ }
180 sub _extract_from_html {
181 my ($self, $html) = @_;
183 HTML::Zoom->from_html($html)
184 ->select('title')->collect_content({ into => \my @title })
188 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
189 } $z, qw(subtitle description keywords created plugins)
193 title => $title[0]->{raw}||'',
194 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
199 sub _extract_from_md {
200 my ($self, $md) = @_;
201 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));