Commit | Line | Data |
95148a72 |
1 | package SCSite::PageSet; |
2 | |
95148a72 |
3 | use IO::All; |
4 | use Text::MultiMarkdown 'markdown'; |
5 | use HTML::Zoom; |
ebd4c292 |
6 | use Sub::Quote; |
7 | use Syntax::Keyword::Gather; |
8 | use SCSite::Page; |
95148a72 |
9 | use Moo; |
10 | |
34597fb2 |
11 | has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir'); |
95148a72 |
12 | has base_dir => (is => 'ro', required => 1); |
ebd4c292 |
13 | has max_depth => (is => 'ro', default => quote_sub q{ 0 }); |
95148a72 |
14 | |
34597fb2 |
15 | has rel_path => (is => 'lazy'); |
16 | |
17 | sub _build_rel_path { |
18 | my ($self) = @_; |
19 | io->dir('/') |
20 | ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name)) |
21 | } |
22 | |
95148a72 |
23 | sub get { |
24 | my ($self, $spec) = @_; |
25 | $spec->{path} or die "path is required to get"; |
26 | my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$}; |
27 | my $type; |
5cc6d9e2 |
28 | my @poss = io->dir($self->base_dir)->${\sub { |
95148a72 |
29 | my $io = shift; |
30 | defined($dir) ? $io->catdir($dir) : $io |
ebd4c292 |
31 | }}->filter(sub { |
32 | $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1 |
33 | }) |
95148a72 |
34 | ->all_files; |
35 | die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss |
36 | if @poss > 1; |
ebd4c292 |
37 | return undef unless @poss; |
34597fb2 |
38 | $self->${\"_inflate_${type}"}( |
39 | $self->rel_path->catdir($spec->{path}), $poss[0]->all |
40 | ); |
ebd4c292 |
41 | } |
42 | |
43 | sub map { |
44 | my ($self, $mapper) = @_; |
45 | [ map $mapper->($_), $self->flatten ] |
46 | } |
47 | |
48 | sub flatten { |
49 | my ($self) = @_; |
50 | my %seen; |
34597fb2 |
51 | my $slash = io->dir('/'); |
ebd4c292 |
52 | map { |
53 | my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/; |
54 | $self->${\"_inflate_${type}"}( |
34597fb2 |
55 | $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)), $_->all |
ebd4c292 |
56 | ); |
57 | } io->dir($self->base_dir) |
58 | ->filter(sub { $_->filename =~ /${\$self->_types_re}$/ }) |
59 | ->all_files($self->max_depth) |
60 | } |
61 | |
62 | sub latest { |
63 | my ($self, $max) = @_; |
64 | require SCSite::LatestPageSet; |
65 | SCSite::LatestPageSet->new( |
66 | parent => $self, |
67 | max_entries => $max, |
68 | ); |
95148a72 |
69 | } |
70 | |
71 | sub _new_page { |
ebd4c292 |
72 | SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} }) |
95148a72 |
73 | } |
74 | |
ebd4c292 |
75 | sub _types_re { qw/\.(html|md)/ } |
76 | |
95148a72 |
77 | sub _inflate_html { |
ebd4c292 |
78 | my ($self, $path, $html) = @_; |
79 | $self->_new_page($path, $self->_extract_from_html($html)); |
95148a72 |
80 | } |
81 | |
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 }) |
ebd4c292 |
88 | ->select('meta[name=created]')->collect({ into => \my @created }) |
95148a72 |
89 | ->select('body')->collect_content({ into => \my @body }) |
90 | ->run; |
91 | +{ |
ebd4c292 |
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||'', |
95148a72 |
97 | } |
98 | } |
99 | |
100 | sub _inflate_md { |
ebd4c292 |
101 | my ($self, $path, $md) = @_; |
102 | $self->_new_page($path, $self->_extract_from_md($md)); |
95148a72 |
103 | } |
104 | |
105 | sub _extract_from_md { |
106 | my ($self, $md) = @_; |
107 | $self->_extract_from_html(markdown($md, { document_format => 'complete' })); |
108 | } |
109 | |
110 | 1; |