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 }); |
221c4151 |
14 | has min_depth => (is => 'ro', default => quote_sub q{ 1 }); |
95148a72 |
15 | |
34597fb2 |
16 | has rel_path => (is => 'lazy'); |
17 | |
18 | sub _build_rel_path { |
19 | my ($self) = @_; |
20 | io->dir('/') |
21 | ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name)) |
22 | } |
23 | |
95148a72 |
24 | sub get { |
25 | my ($self, $spec) = @_; |
26 | $spec->{path} or die "path is required to get"; |
27 | my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$}; |
28 | my $type; |
5cc6d9e2 |
29 | my @poss = io->dir($self->base_dir)->${\sub { |
95148a72 |
30 | my $io = shift; |
31 | defined($dir) ? $io->catdir($dir) : $io |
ebd4c292 |
32 | }}->filter(sub { |
33 | $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1 |
34 | }) |
221c4151 |
35 | ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }}; |
95148a72 |
36 | die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss |
37 | if @poss > 1; |
ebd4c292 |
38 | return undef unless @poss; |
34597fb2 |
39 | $self->${\"_inflate_${type}"}( |
40 | $self->rel_path->catdir($spec->{path}), $poss[0]->all |
41 | ); |
ebd4c292 |
42 | } |
43 | |
44 | sub map { |
45 | my ($self, $mapper) = @_; |
46 | [ map $mapper->($_), $self->flatten ] |
47 | } |
48 | |
fc436d2e |
49 | sub _depth_under_base { |
50 | my ($self, $path) = @_; |
51 | File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name)) |
52 | } |
53 | |
ebd4c292 |
54 | sub flatten { |
55 | my ($self) = @_; |
fc436d2e |
56 | return unless (my $base = $self->base_dir)->exists; |
ebd4c292 |
57 | my %seen; |
34597fb2 |
58 | my $slash = io->dir('/'); |
fc436d2e |
59 | my $min = $self->min_depth; |
ebd4c292 |
60 | map { |
61 | my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/; |
62 | $self->${\"_inflate_${type}"}( |
fc436d2e |
63 | $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)), |
64 | $_->all |
ebd4c292 |
65 | ); |
fc436d2e |
66 | } map { |
67 | $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ }) |
221c4151 |
68 | ->all_files($self->max_depth - ($min-1)) |
fc436d2e |
69 | } map |
221c4151 |
70 | $min > 1 |
fc436d2e |
71 | ? do { |
221c4151 |
72 | # can't use ->all_dirs($min-1) since we only want the final level |
73 | my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x |
fc436d2e |
74 | } |
75 | : $_, |
221c4151 |
76 | $base; |
ebd4c292 |
77 | } |
78 | |
79 | sub latest { |
80 | my ($self, $max) = @_; |
81 | require SCSite::LatestPageSet; |
82 | SCSite::LatestPageSet->new( |
83 | parent => $self, |
84 | max_entries => $max, |
85 | ); |
95148a72 |
86 | } |
87 | |
88 | sub _new_page { |
ebd4c292 |
89 | SCSite::Page->new({ path => $_[1], page_set => $_[0], %{$_[2]} }) |
95148a72 |
90 | } |
91 | |
ebd4c292 |
92 | sub _types_re { qw/\.(html|md)/ } |
93 | |
95148a72 |
94 | sub _inflate_html { |
ebd4c292 |
95 | my ($self, $path, $html) = @_; |
96 | $self->_new_page($path, $self->_extract_from_html($html)); |
95148a72 |
97 | } |
98 | |
99 | sub _extract_from_html { |
100 | my ($self, $html) = @_; |
101 | HTML::Zoom->from_html($html) |
102 | ->select('title')->collect_content({ into => \my @title }) |
103 | ->select('meta[name=description]')->collect({ into => \my @description }) |
104 | ->select('meta[name=keywords]')->collect({ into => \my @keywords }) |
ebd4c292 |
105 | ->select('meta[name=created]')->collect({ into => \my @created }) |
95148a72 |
106 | ->select('body')->collect_content({ into => \my @body }) |
107 | ->run; |
108 | +{ |
ebd4c292 |
109 | title => $title[0]->{raw}||'', |
110 | description => $description[0]->{attrs}{content}||'', |
111 | keywords => $keywords[0]->{attrs}{content}||'', |
112 | created => $created[0]->{attrs}{content}||'', |
113 | body => HTML::Zoom->from_events(\@body)->to_html||'', |
95148a72 |
114 | } |
115 | } |
116 | |
117 | sub _inflate_md { |
ebd4c292 |
118 | my ($self, $path, $md) = @_; |
119 | $self->_new_page($path, $self->_extract_from_md($md)); |
95148a72 |
120 | } |
121 | |
122 | sub _extract_from_md { |
123 | my ($self, $md) = @_; |
124 | $self->_extract_from_html(markdown($md, { document_format => 'complete' })); |
125 | } |
126 | |
127 | 1; |