1 package SCSite::PageSet;
3 use Text::MultiMarkdown 'markdown';
6 use Syntax::Keyword::Gather;
18 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
19 has base_dir => (is => 'ro', required => 1);
20 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
21 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
23 has rel_path => (is => 'lazy');
28 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
32 my ($self, $spec) = @_;
33 $spec->{path} or die "path is required to get";
34 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
36 my @poss = io->dir($self->base_dir)->${\sub {
38 defined($dir) ? $io->catdir($dir) : $io
40 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
42 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
43 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
45 return undef unless @poss;
47 $type, $self->rel_path->catdir($spec->{path}), $poss[0]
52 my ($self, $type, $path, $io) = @_;
53 (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/;
54 my $cache = io($cache_name);
56 if ($cache->mtime >= $io->mtime) {
58 $self->_new_page($path, $self->_json->decode($cache->all));
60 die "Error inflating ${path} from cache: $_\n";
66 my $extracted = $self->${\"_extract_from_${type}"}($raw);
67 try { $cache->print($self->_json->encode($extracted)); };
68 $self->_new_page($path, $extracted);
70 die "Error inflating ${path} as ${type}: $_\n";
75 my ($self, $mapper) = @_;
76 [ map $mapper->($_), $self->flatten ]
79 sub _depth_under_base {
80 my ($self, $path) = @_;
81 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
86 return unless (my $base = $self->base_dir)->exists;
88 my $slash = io->dir('/');
89 my $min = $self->min_depth;
91 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
94 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
98 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
99 ->all_files($self->max_depth - ($min-1))
103 # can't use ->all_dirs($min-1) since we only want the final level
104 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
111 my ($self, $max) = @_;
112 require SCSite::LatestPageSet;
113 SCSite::LatestPageSet->new(
120 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
123 sub _types_re { qw/\.(html|md)/ }
125 sub _extract_from_html {
126 my ($self, $html) = @_;
127 HTML::Zoom->from_html($html)
128 ->select('title')->collect_content({ into => \my @title })
129 ->select('meta[name=subtitle]')->collect({ into => \my @subtitle })
130 ->select('meta[name=description]')->collect({ into => \my @description })
131 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
132 ->select('meta[name=created]')->collect({ into => \my @created })
133 ->select('body')->collect_content({ into => \my @body })
136 title => $title[0]->{raw}||'',
137 subtitle => $subtitle[0]->{attrs}{content}||'',
138 description => $description[0]->{attrs}{content}||'',
139 keywords => $keywords[0]->{attrs}{content}||'',
140 created => $created[0]->{attrs}{content}||'',
141 body => HTML::Zoom->from_events(\@body)->to_html||'',
145 sub _extract_from_md {
146 my ($self, $md) = @_;
147 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));