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);
16 with 'App::SCS::Role::PageChildren';
23 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
24 has base_dir => (is => 'ro', required => 1);
25 has plugin_config => (is => 'ro', required => 1);
26 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
27 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
29 has rel_path => (is => 'lazy');
34 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
37 sub _page_set { $_[0] }
38 sub _page_set_class { ref($_[0]) }
39 sub _top_dir { shift->top_dir }
40 sub _my_path { shift->base_dir }
43 my ($self, $spec) = @_;
44 $spec->{path} or die "path is required to get";
45 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
47 my @poss = io->dir($self->base_dir)->${\sub {
49 defined($dir) ? $io->catdir($dir) : $io
51 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
53 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
54 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
56 return undef unless @poss;
58 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
62 sub _config_files_for {
63 my ($self, $path) = @_;
65 my @dir_parts = io->dir($path)->splitdir;
66 my @dirs = map io->dir('')->catdir(@dir_parts[1..$_]), 1..($#dir_parts - 1);
68 return grep +(-f $_->name and not $_->empty),
69 map $self->_top_dir->catfile("${_}.conf"), @dirs;
73 my ($self, $type, $path, $io) = @_;
74 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
75 my $cache = io($cache_name);
76 my @config_files = $self->_config_files_for($path);
77 my $max_stat = max map $_->mtime, $io, @config_files;
80 if ($cache->mtime >= $max_stat) {
82 $self->_new_page($path, $self->_json->decode($cache->all));
84 die "Error inflating ${path} from cache: $_\n";
91 my $extracted = $self->${\"_extract_from_${type}"}($raw);
92 my $jsony = JSONY->new;
97 map @{$jsony->load($_)}, (
98 $extracted->{plugins},
99 (map $_->all, reverse @config_files), # apply config deepest first
105 my $tmp_cache = io($cache_name . ".tmp");
106 $tmp_cache->print($self->_json->encode($setup));
107 $tmp_cache->rename($cache_name);
110 $self->_new_page($path, $setup);
112 die "Error inflating ${path} as ${type}: $_\n";
117 my ($self, $mapper) = @_;
118 [ map $mapper->($_), $self->flatten ]
121 sub _depth_under_base {
122 my ($self, $path) = @_;
123 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
128 my $slash = io->dir('/');
130 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
133 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
141 my $slash = io->dir('/');
143 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
144 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
150 return unless (my $base = $self->base_dir)->exists;
152 my $min = $self->min_depth;
154 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
155 ->all_files($self->max_depth - ($min-1))
159 # can't use ->all_dirs($min-1) since we only want the final level
160 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
167 my ($self, $max) = @_;
168 use_module('App::SCS::LatestPageSet')->new(
175 use_module('App::SCS::Page')->new(
176 path => $_[1], page_set => $_[0], %{$_[2]}
180 sub _types_re { qw/\.(html|md)/ }
182 sub _extract_from_html {
183 my ($self, $html) = @_;
185 HTML::Zoom->from_html($html)
186 ->select('title')->collect_content({ into => \my @title })
190 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
191 } $z, qw(subtitle description keywords created plugins)
195 title => $title[0]->{raw}||'',
196 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
201 sub _extract_from_md {
202 my ($self, $md) = @_;
203 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));