1 package SCSite::PageSet;
3 use Text::MultiMarkdown 'markdown';
6 use Syntax::Keyword::Gather;
13 with 'SCSite::PageChildren';
20 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
21 has base_dir => (is => 'ro', required => 1);
22 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
23 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
25 has rel_path => (is => 'lazy');
30 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
33 sub _page_set_class { ref($_[0]) }
34 sub _top_dir { shift->top_dir }
35 sub _my_path { shift->base_dir }
38 my ($self, $spec) = @_;
39 $spec->{path} or die "path is required to get";
40 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
42 my @poss = io->dir($self->base_dir)->${\sub {
44 defined($dir) ? $io->catdir($dir) : $io
46 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
48 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
49 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
51 return undef unless @poss;
53 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
58 my ($self, $type, $path, $io) = @_;
59 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
60 my $cache = io($cache_name);
62 if ($cache->mtime >= $io->mtime) {
64 $self->_new_page($path, $self->_json->decode($cache->all));
66 die "Error inflating ${path} from cache: $_\n";
72 my $extracted = $self->${\"_extract_from_${type}"}($raw);
73 try { $cache->print($self->_json->encode($extracted)); };
74 $self->_new_page($path, $extracted);
76 die "Error inflating ${path} as ${type}: $_\n";
81 my ($self, $mapper) = @_;
82 [ map $mapper->($_), $self->flatten ]
85 sub _depth_under_base {
86 my ($self, $path) = @_;
87 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
92 my $slash = io->dir('/');
94 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
97 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
105 my $slash = io->dir('/');
107 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
108 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name,
114 return unless (my $base = $self->base_dir)->exists;
116 my $min = $self->min_depth;
118 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
119 ->all_files($self->max_depth - ($min-1))
123 # can't use ->all_dirs($min-1) since we only want the final level
124 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
131 my ($self, $max) = @_;
132 require SCSite::LatestPageSet;
133 SCSite::LatestPageSet->new(
140 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
143 sub _types_re { qw/\.(html|md)/ }
145 sub _extract_from_html {
146 my ($self, $html) = @_;
147 HTML::Zoom->from_html($html)
148 ->select('title')->collect_content({ into => \my @title })
149 ->select('meta[name=subtitle]')->collect({ into => \my @subtitle })
150 ->select('meta[name=description]')->collect({ into => \my @description })
151 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
152 ->select('meta[name=created]')->collect({ into => \my @created })
153 ->select('body')->collect_content({ into => \my @body })
156 title => $title[0]->{raw}||'',
157 subtitle => $subtitle[0]->{attrs}{content}||'',
158 description => $description[0]->{attrs}{content}||'',
159 keywords => $keywords[0]->{attrs}{content}||'',
160 created => $created[0]->{attrs}{content}||'',
161 body => HTML::Zoom->from_events(\@body)->to_html||'',
165 sub _extract_from_md {
166 my ($self, $md) = @_;
167 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));