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_class { ref($_[0]) }
37 sub _top_dir { shift->top_dir }
38 sub _my_path { shift->base_dir }
41 my ($self, $spec) = @_;
42 $spec->{path} or die "path is required to get";
43 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
45 my @poss = io->dir($self->base_dir)->${\sub {
47 defined($dir) ? $io->catdir($dir) : $io
49 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
51 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
52 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
54 return undef unless @poss;
56 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
61 my ($self, $type, $path, $io) = @_;
62 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
63 my $cache = io($cache_name);
65 if ($cache->mtime >= $io->mtime) {
67 $self->_new_page($path, $self->_json->decode($cache->all));
69 die "Error inflating ${path} from cache: $_\n";
75 my $extracted = $self->${\"_extract_from_${type}"}($raw);
76 try { $cache->print($self->_json->encode($extracted)); };
77 $self->_new_page($path, $extracted);
79 die "Error inflating ${path} as ${type}: $_\n";
84 my ($self, $mapper) = @_;
85 [ map $mapper->($_), $self->flatten ]
88 sub _depth_under_base {
89 my ($self, $path) = @_;
90 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
95 my $slash = io->dir('/');
97 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
100 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
108 my $slash = io->dir('/');
110 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
111 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
117 return unless (my $base = $self->base_dir)->exists;
119 my $min = $self->min_depth;
121 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
122 ->all_files($self->max_depth - ($min-1))
126 # can't use ->all_dirs($min-1) since we only want the final level
127 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
134 my ($self, $max) = @_;
135 use_module('App::SCS::LatestPageSet')->new(
142 use_module('App::SCS::Page')->new(
143 path => $_[1], page_set => $_[0], %{$_[2]}
147 sub _types_re { qw/\.(html|md)/ }
149 sub _extract_from_html {
150 my ($self, $html) = @_;
152 HTML::Zoom->from_html($html)
153 ->select('title')->collect_content({ into => \my @title })
157 $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) })
158 } $z, qw(subtitle description keywords created plugins)
162 title => $title[0]->{raw}||'',
163 (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta),
168 sub _extract_from_md {
169 my ($self, $md) = @_;
170 #warn markdown($md, { document_format => 'complete' });
171 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));