basic page rendering
[scpubgit/SCS.git] / lib / SCSite / PageSet.pm
CommitLineData
95148a72 1package SCSite::PageSet;
2
3{ package SCSite::Page; use Moo;
4 has $_ => (is => 'ro') for qw(title description keywords body);
5}
6
7use IO::All;
8use Text::MultiMarkdown 'markdown';
9use HTML::Zoom;
10use Moo;
11
12has base_dir => (is => 'ro', required => 1);
13
14sub get {
15 my ($self, $spec) = @_;
16 $spec->{path} or die "path is required to get";
17 my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$};
18 my $type;
5cc6d9e2 19 my @poss = io->dir($self->base_dir)->${\sub {
95148a72 20 my $io = shift;
21 defined($dir) ? $io->catdir($dir) : $io
22 }}->filter(sub { $_->filename =~ /\Q${file}\E\.(html|md)/ and $type = $1 })
23 ->all_files;
24 die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss
25 if @poss > 1;
26 $self->${\"_inflate_${type}"}($poss[0]->all);
27}
28
29sub _new_page {
30 SCSite::Page->new($_[1])
31}
32
33sub _inflate_html {
34 my ($self, $html) = @_;
35 $self->_new_page($self->_extract_from_html($html));
36}
37
38sub _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 })
45 ->run;
46 +{
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,
51 }
52}
53
54sub _inflate_md {
55 my ($self, $md) = @_;
56 $self->_new_page($self->_extract_from_md($md));
57}
58
59sub _extract_from_md {
60 my ($self, $md) = @_;
61 $self->_extract_from_html(markdown($md, { document_format => 'complete' }));
62}
63
641;