1 package App::SCS::PageSet;
3 use Text::MultiMarkdown 'markdown';
6 use Syntax::Keyword::Gather;
10 use List::Util qw(reduce);
11 use Module::Runtime qw(use_module);
15 with 'App::SCS::Role::PageChildren';
22 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
23 has base_dir => (is => 'ro', required => 1);
24 has plugin_config => (is => 'ro', required => 1);
25 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
26 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
28 has rel_path => (is => 'lazy');
33 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
36 sub _page_set { $_[0] }
37 sub _page_set_class { ref($_[0]) }
38 sub _top_dir { shift->top_dir }
39 sub _my_path { shift->base_dir }
42 my ($self, $spec) = @_;
43 $spec->{path} or die "path is required to get";
44 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
46 my @poss = io->dir($self->base_dir)->${\sub {
48 defined($dir) ? $io->catdir($dir) : $io
50 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
52 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
53 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
55 return undef unless @poss;
57 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
62 my ($self, $type, $path, $io) = @_;
63 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
64 my $cache = io($cache_name);
66 if ($cache->mtime >= $io->mtime) {
68 $self->_new_page($path, $self->_json->decode($cache->all));
70 die "Error inflating ${path} from cache: $_\n";
76 my $extracted = $self->${\"_extract_from_${type}"}($raw);
77 try { $cache->print($self->_json->encode($extracted)); };
78 $self->_new_page($path, $extracted);
80 die "Error inflating ${path} as ${type}: $_\n";
85 my ($self, $mapper) = @_;
86 [ map $mapper->($_), $self->flatten ]
89 sub _depth_under_base {
90 my ($self, $path) = @_;
91 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
96 my $slash = io->dir('/');
98 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
101 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
109 my $slash = io->dir('/');
111 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
112 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
118 return unless (my $base = $self->base_dir)->exists;
120 my $min = $self->min_depth;
122 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
123 ->all_files($self->max_depth - ($min-1))
127 # can't use ->all_dirs($min-1) since we only want the final level
128 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
135 my ($self, $max) = @_;
136 use_module('App::SCS::LatestPageSet')->new(
143 use_module('App::SCS::Page')->new(
144 path => $_[1], page_set => $_[0], %{$_[2]}
148 sub _types_re { qw/\.(html|md)/ }
150 sub _extract_from_html {
151 my ($self, $html) = @_;
153 HTML::Zoom->from_html($html)
154 ->select('title')->collect_content({ into => \my @title })
158 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
159 } $z, qw(subtitle description keywords created plugins)
163 title => $title[0]->{raw}||'',
164 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
169 sub _extract_from_md {
170 my ($self, $md) = @_;
171 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));