1 package SCSite::PageSet;
3 { package SCSite::Page; use Moo;
4 has $_ => (is => 'ro') for qw(title description keywords body);
8 use Text::MultiMarkdown 'markdown';
12 has base_dir => (is => 'ro', required => 1);
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
22 }}->filter(sub { $_->filename =~ /\Q${file}\E\.(html|md)/ and $type = $1 })
24 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
26 $self->${\"_inflate_${type}"}($poss[0]->all);
30 SCSite::Page->new($_[1])
34 my ($self, $html) = @_;
35 $self->_new_page($self->_extract_from_html($html));
38 sub _extract_from_html {
39 my ($self, $html) = @_;
40 HTML::Zoom->from_html($html)
41 ->select('title')->collect_content({ into => \my @title })
42 ->select('meta[name=description]')->collect({ into => \my @description })
43 ->select('meta[name=keywords]')->collect({ into => \my @keywords })
44 ->select('body')->collect_content({ into => \my @body })
47 title => $title[0]->{raw},
48 description => $description[0]->{attrs}{content},
49 keywords => $keywords[0]->{attrs}{content},
50 body => HTML::Zoom->from_events(\@body)->to_html,
56 $self->_new_page($self->_extract_from_md($md));
59 sub _extract_from_md {
61 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));