1 package SCSite::PageSet;
4 use Text::MultiMarkdown 'markdown';
7 use Syntax::Keyword::Gather;
12 has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir');
13 has base_dir => (is => 'ro', required => 1);
14 has max_depth => (is => 'ro', default => quote_sub q{ 0 });
15 has min_depth => (is => 'ro', default => quote_sub q{ 1 });
17 has rel_path => (is => 'lazy');
22 ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name))
26 my ($self, $spec) = @_;
27 $spec->{path} or die "path is required to get";
28 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
30 my @poss = io->dir($self->base_dir)->${\sub {
32 defined($dir) ? $io->catdir($dir) : $io
34 $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1
36 ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }};
37 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
39 return undef unless @poss;
41 $type, $self->rel_path->catdir($spec->{path}), $poss[0]->all
46 my ($self, $type, $path, $data) = @_;
47 try { $self->${\"_inflate_${type}"}($path, $data) }
49 die "Error inflating ${path} as ${type}: $_\n\nData was: ${data}";
54 my ($self, $mapper) = @_;
55 [ map $mapper->($_), $self->flatten ]
58 sub _depth_under_base {
59 my ($self, $path) = @_;
60 File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name))
65 return unless (my $base = $self->base_dir)->exists;
67 my $slash = io->dir('/');
68 my $min = $self->min_depth;
70 my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/;
73 $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)),
77 $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ })
78 ->all_files($self->max_depth - ($min-1))
82 # can't use ->all_dirs($min-1) since we only want the final level
83 my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x
90 my ($self, $max) = @_;
91 require SCSite::LatestPageSet;
92 SCSite::LatestPageSet->new(
99 SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} })
102 sub _types_re { qw/\.(html|md)/ }
105 my ($self, $path, $html) = @_;
106 $self->_new_page($path, $self->_extract_from_html($html));
109 sub _extract_from_html {
110 my ($self, $html) = @_;
111 HTML::Zoom->from_html($html)
112 ->select('title')->collect_content({ into => \my @title })
113 ->select('meta[name=subtitle]')->collect({ into => \my @subtitle })
114 ->select('meta[name=description]')->collect({ into => \my @description })
115 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
116 ->select('meta[name=created]')->collect({ into => \my @created })
117 ->select('body')->collect_content({ into => \my @body })
120 title => $title[0]->{raw}||'',
121 subtitle => $subtitle[0]->{attrs}{content}||'',
122 description => $description[0]->{attrs}{content}||'',
123 keywords => $keywords[0]->{attrs}{content}||'',
124 created => $created[0]->{attrs}{content}||'',
125 body => HTML::Zoom->from_events(\@body)->to_html||'',
130 my ($self, $path, $md) = @_;
131 $self->_new_page($path, $self->_extract_from_md($md));
134 sub _extract_from_md {
135 my ($self, $md) = @_;
136 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));