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);
15 use Data::Pond qw(pond_read_datum pond_write_datum);
18 with 'App::SCS::Role::PageChildren';
25 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
26 has base_dir => (is => 'ro', required => 1);
27 has plugin_config => (is => 'ro', required => 1);
28 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
29 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
31 has rel_path => (is => 'lazy');
36 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
39 sub _page_set { $_[0] }
40 sub _page_set_class { ref($_[0]) }
41 sub _top_dir { shift->top_dir }
42 sub _my_path { shift->base_dir }
45 my ($self, $spec) = @_;
46 $spec->{path} or die "path is required to get";
47 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
49 my @poss = io->dir($self->base_dir)->${\sub {
51 defined($dir) ? $io->catdir($dir) : $io
53 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
55 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
56 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
58 return undef unless @poss;
60 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
64 sub _config_files_for {
65 my ($self, $path) = @_;
68 my @dirs = io->dir($path)->splitdir;
73 my $buildpath = io('');
75 foreach my $dir (@dirs) {
76 $buildpath = $buildpath->catdir("/$dir");
77 #/home/.../share/pages/blog.conf etc
79 my $file = $self->_top_dir->catfile("$buildpath.conf");
81 if (!$file->exists || !$file->file || $file->empty) {
92 my ($self, $type, $path, $io) = @_;
93 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
94 my $cache = io($cache_name);
95 my $config_files = $self->_config_files_for($path);
98 if (scalar @$config_files) {
99 my $maxfile = reduce { $a->mtime > $b->mtime ? $a : $b } @$config_files;
100 $maxstat = $maxfile->mtime;
103 if (-f $cache_name) {
104 if ($cache->mtime >= max($io->mtime, $maxstat)) {
106 $self->_new_page($path, $self->_json->decode($cache->all));
108 die "Error inflating ${path} from cache: $_\n";
115 my $extracted = $self->${\"_extract_from_${type}"}($raw);
116 my $jsony = JSONY->new;
117 my $config = reduce { merge($a, $jsony->load($b->all)) } [], @$config_files;
119 $extracted->{plugins} = pond_read_datum('[' . $extracted->{plugins} . ']');
121 my $setup = $extracted;
123 $setup->{plugin_config} = merge($extracted->{plugins}, $config);
126 my $tmp_cache = io($cache_name . ".tmp");
127 $tmp_cache->print($self->_json->encode($setup));
128 $tmp_cache->rename($cache_name);
131 $self->_new_page($path, $setup);
133 die "Error inflating ${path} as ${type}: $_\n";
138 my ($self, $mapper) = @_;
139 [ map $mapper->($_), $self->flatten ]
142 sub _depth_under_base {
143 my ($self, $path) = @_;
144 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
149 my $slash = io->dir('/');
151 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
154 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
162 my $slash = io->dir('/');
164 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
165 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
171 return unless (my $base = $self->base_dir)->exists;
173 my $min = $self->min_depth;
175 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
176 ->all_files($self->max_depth - ($min-1))
180 # can't use ->all_dirs($min-1) since we only want the final level
181 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
188 my ($self, $max) = @_;
189 use_module('App::SCS::LatestPageSet')->new(
196 use_module('App::SCS::Page')->new(
197 path => $_[1], page_set => $_[0], %{$_[2]}
201 sub _types_re { qw/\.(html|md)/ }
203 sub _extract_from_html {
204 my ($self, $html) = @_;
206 HTML::Zoom->from_html($html)
207 ->select('title')->collect_content({ into => \my @title })
211 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
212 } $z, qw(subtitle description keywords created plugins)
216 title => $title[0]->{raw}||'',
217 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
222 sub _extract_from_md {
223 my ($self, $md) = @_;
224 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));