1 package SCSite::PageSet;
4 use Text::MultiMarkdown 'markdown';
7 use Syntax::Keyword::Gather;
11 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
12 has base_dir => (is => 'ro', required => 1);
13 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
15 has rel_path => (is => 'lazy');
20 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
24 my ($self, $spec) = @_;
25 $spec->{path} or die "path is required to get";
26 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
28 my @poss = io->dir($self->base_dir)->${\sub {
30 defined($dir) ? $io->catdir($dir) : $io
32 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
35 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
37 return undef unless @poss;
38 $self->${\"_inflate_${type}"}(
39 $self->rel_path->catdir($spec->{path}), $poss[0]->all
44 my ($self, $mapper) = @_;
45 [ map $mapper->($_), $self->flatten ]
51 my $slash = io->dir('/');
53 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
54 $self->${\"_inflate_${type}"}(
55 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)), $_->all
57 } io->dir($self->base_dir)
58 ->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
59 ->all_files($self->max_depth)
63 my ($self, $max) = @_;
64 require SCSite::LatestPageSet;
65 SCSite::LatestPageSet->new(
72 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
75 sub _types_re { qw/\.(html|md)/ }
78 my ($self, $path, $html) = @_;
79 $self->_new_page($path, $self->_extract_from_html($html));
82 sub _extract_from_html {
83 my ($self, $html) = @_;
84 HTML::Zoom->from_html($html)
85 ->select('title')->collect_content({ into => \my @title })
86 ->select('meta[name=description]')->collect({ into => \my @description })
87 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
88 ->select('meta[name=created]')->collect({ into => \my @created })
89 ->select('body')->collect_content({ into => \my @body })
92 title => $title[0]->{raw}||'',
93 description => $description[0]->{attrs}{content}||'',
94 keywords => $keywords[0]->{attrs}{content}||'',
95 created => $created[0]->{attrs}{content}||'',
96 body => HTML::Zoom->from_events(\@body)->to_html||'',
101 my ($self, $path, $md) = @_;
102 $self->_new_page($path, $self->_extract_from_md($md));
105 sub _extract_from_md {
106 my ($self, $md) = @_;
107 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));