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) {
57 return $self->_new_page($path, $self->_json->decode($cache->all));
62 my $extracted = $self->${\"_extract_from_${type}"}($raw);
63 $cache->print($self->_json->encode($extracted));
64 $self->_new_page($path, $extracted);
66 die "Error inflating ${path} as ${type}: $_\n\nData was: ${raw}";
71 my ($self, $mapper) = @_;
72 [ map $mapper->($_), $self->flatten ]
75 sub _depth_under_base {
76 my ($self, $path) = @_;
77 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
82 return unless (my $base = $self->base_dir)->exists;
84 my $slash = io->dir('/');
85 my $min = $self->min_depth;
87 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
90 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
94 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
95 ->all_files($self->max_depth - ($min-1))
99 # can't use ->all_dirs($min-1) since we only want the final level
100 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
107 my ($self, $max) = @_;
108 require SCSite::LatestPageSet;
109 SCSite::LatestPageSet->new(
116 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
119 sub _types_re { qw/\.(html|md)/ }
121 sub _extract_from_html {
122 my ($self, $html) = @_;
123 HTML::Zoom->from_html($html)
124 ->select('title')->collect_content({ into => \my @title })
125 ->select('meta[name=subtitle]')->collect({ into => \my @subtitle })
126 ->select('meta[name=description]')->collect({ into => \my @description })
127 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
128 ->select('meta[name=created]')->collect({ into => \my @created })
129 ->select('body')->collect_content({ into => \my @body })
132 title => $title[0]->{raw}||'',
133 subtitle => $subtitle[0]->{attrs}{content}||'',
134 description => $description[0]->{attrs}{content}||'',
135 keywords => $keywords[0]->{attrs}{content}||'',
136 created => $created[0]->{attrs}{content}||'',
137 body => HTML::Zoom->from_events(\@body)->to_html||'',
141 sub _extract_from_md {
142 my ($self, $md) = @_;
143 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));