1 package SCSite::PageSet;
4 use Text::MultiMarkdown 'markdown';
7 use Syntax::Keyword::Gather;
11 has base_dir => (is => 'ro', required => 1);
12 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
15 my ($self, $spec) = @_;
16 $spec->{path} or die "path is required to get";
17 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
19 my @poss = io->dir($self->base_dir)->${\sub {
21 defined($dir) ? $io->catdir($dir) : $io
23 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
26 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
28 return undef unless @poss;
29 $self->${\"_inflate_${type}"}($spec->{path}, $poss[0]->all);
33 my ($self, $mapper) = @_;
34 [ map $mapper->($_), $self->flatten ]
41 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
42 $self->${\"_inflate_${type}"}(
43 File::Spec->abs2rel($path, $self->base_dir->name), $_->all
45 } io->dir($self->base_dir)
46 ->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
47 ->all_files($self->max_depth)
51 my ($self, $max) = @_;
52 require SCSite::LatestPageSet;
53 SCSite::LatestPageSet->new(
60 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
63 sub _types_re { qw/\.(html|md)/ }
66 my ($self, $path, $html) = @_;
67 $self->_new_page($path, $self->_extract_from_html($html));
70 sub _extract_from_html {
71 my ($self, $html) = @_;
72 HTML::Zoom->from_html($html)
73 ->select('title')->collect_content({ into => \my @title })
74 ->select('meta[name=description]')->collect({ into => \my @description })
75 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
76 ->select('meta[name=created]')->collect({ into => \my @created })
77 ->select('body')->collect_content({ into => \my @body })
80 title => $title[0]->{raw}||'',
81 description => $description[0]->{attrs}{content}||'',
82 keywords => $keywords[0]->{attrs}{content}||'',
83 created => $created[0]->{attrs}{content}||'',
84 body => HTML::Zoom->from_events(\@body)->to_html||'',
89 my ($self, $path, $md) = @_;
90 $self->_new_page($path, $self->_extract_from_md($md));
93 sub _extract_from_md {
95 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));