Commit | Line | Data |
632f0e07 |
1 | package App::SCS::PageSet; |
2 | |
3 | use Text::MultiMarkdown 'markdown'; |
4 | use HTML::Zoom; |
5 | use Sub::Quote; |
6 | use Syntax::Keyword::Gather; |
7 | use App::SCS::Page; |
8 | use IO::All; |
9 | use Try::Tiny; |
fd5c2ec2 |
10 | use List::Util qw(reduce max); |
0034f151 |
11 | use Module::Runtime qw(use_module); |
f4837d95 |
12 | use JSON::MaybeXS; |
632f0e07 |
13 | use Moo; |
fd5c2ec2 |
14 | use Hash::Merge qw(merge); |
15 | use Data::Pond qw(pond_read_datum pond_write_datum); |
16 | use JSONY; |
632f0e07 |
17 | |
18 | with 'App::SCS::Role::PageChildren'; |
19 | |
20 | { |
21 | my $j = JSON->new; |
22 | sub _json { $j } |
23 | } |
24 | |
25 | has top_dir => (is => 'ro', lazy => 1, builder => 'base_dir'); |
26 | has base_dir => (is => 'ro', required => 1); |
27 | has plugin_config => (is => 'ro', required => 1); |
28 | has max_depth => (is => 'ro', default => quote_sub q{ 0 }); |
29 | has min_depth => (is => 'ro', default => quote_sub q{ 1 }); |
30 | |
31 | has rel_path => (is => 'lazy'); |
32 | |
33 | sub _build_rel_path { |
34 | my ($self) = @_; |
35 | io->dir('/') |
36 | ->catdir(File::Spec->abs2rel($self->base_dir->name, $self->top_dir->name)) |
37 | } |
38 | |
f50b4a35 |
39 | sub _page_set { $_[0] } |
632f0e07 |
40 | sub _page_set_class { ref($_[0]) } |
41 | sub _top_dir { shift->top_dir } |
42 | sub _my_path { shift->base_dir } |
43 | |
44 | sub get { |
45 | my ($self, $spec) = @_; |
46 | $spec->{path} or die "path is required to get"; |
47 | my ($dir, $file) = $spec->{path} =~ m{^(?:(.*)/)?([^/]+)$}; |
48 | my $type; |
49 | my @poss = io->dir($self->base_dir)->${\sub { |
50 | my $io = shift; |
51 | defined($dir) ? $io->catdir($dir) : $io |
52 | }}->filter(sub { |
53 | $_->filename =~ /^\Q${file}\E${\$self->_types_re}$/ and $type = $1 |
54 | }) |
55 | ->${\sub { -e "$_[0]" ? $_[0]->all_files : () }}; |
56 | die "multiple files found for ${\$spec->{path}}:\n".join "\n", @poss |
57 | if @poss > 1; |
58 | return undef unless @poss; |
59 | $self->_inflate( |
60 | $type, $self->rel_path->catdir($spec->{path}), $poss[0] |
61 | ); |
62 | } |
63 | |
b9dd3724 |
64 | sub _config_files_for { |
fd5c2ec2 |
65 | my ($self, $path) = @_; |
66 | |
67 | my @files = (); |
68 | my @dirs = io->dir($path)->splitdir; |
69 | |
70 | |
71 | shift @dirs; |
72 | pop @dirs; |
73 | my $buildpath = io(''); |
74 | |
75 | foreach my $dir (@dirs) { |
76 | $buildpath = $buildpath->catdir("/$dir"); |
77 | #/home/.../share/pages/blog.conf etc |
78 | |
79 | my $file = $self->_top_dir->catfile("$buildpath.conf"); |
80 | |
81 | if (!$file->exists || !$file->file || $file->empty) { |
82 | next; |
83 | } |
84 | |
85 | push @files, $file; |
86 | } |
87 | |
88 | return \@files; |
89 | } |
90 | |
632f0e07 |
91 | sub _inflate { |
92 | my ($self, $type, $path, $io) = @_; |
93 | (my $cache_name = $io->name) =~ s/\/([^\/]+)$/\/.htcache.$1.json/; |
94 | my $cache = io($cache_name); |
b9dd3724 |
95 | my $config_files = $self->_config_files_for($path); |
2f5c23b3 |
96 | my $max_stat = max map $_->mtime, $io, @$config_files; |
fd5c2ec2 |
97 | |
632f0e07 |
98 | if (-f $cache_name) { |
2f5c23b3 |
99 | if ($cache->mtime >= $max_stat) { |
632f0e07 |
100 | return try { |
101 | $self->_new_page($path, $self->_json->decode($cache->all)); |
102 | } catch { |
103 | die "Error inflating ${path} from cache: $_\n"; |
104 | } |
105 | } |
106 | } |
107 | my $raw = $io->all; |
108 | try { |
c68d9e18 |
109 | |
632f0e07 |
110 | my $extracted = $self->${\"_extract_from_${type}"}($raw); |
fd5c2ec2 |
111 | my $jsony = JSONY->new; |
c68d9e18 |
112 | my $config = reduce { merge($a, $jsony->load($b->all)) } [], @$config_files; |
fd5c2ec2 |
113 | |
114 | $extracted->{plugins} = pond_read_datum('[' . $extracted->{plugins} . ']'); |
c68d9e18 |
115 | |
116 | my $setup = $extracted; |
117 | |
118 | $setup->{plugin_config} = merge($extracted->{plugins}, $config); |
fd5c2ec2 |
119 | |
120 | try { |
121 | my $tmp_cache = io($cache_name . ".tmp"); |
122 | $tmp_cache->print($self->_json->encode($setup)); |
123 | $tmp_cache->rename($cache_name); |
124 | }; |
125 | |
126 | $self->_new_page($path, $setup); |
632f0e07 |
127 | } catch { |
128 | die "Error inflating ${path} as ${type}: $_\n"; |
129 | } |
130 | } |
131 | |
132 | sub map { |
133 | my ($self, $mapper) = @_; |
134 | [ map $mapper->($_), $self->flatten ] |
135 | } |
136 | |
137 | sub _depth_under_base { |
138 | my ($self, $path) = @_; |
139 | File::Spec->splitdir(File::Spec->abs2rel($path, $self->base_dir->name)) |
140 | } |
141 | |
142 | sub flatten { |
143 | my ($self) = @_; |
144 | my $slash = io->dir('/'); |
145 | map { |
146 | my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/; |
147 | $self->_inflate( |
148 | $type, |
149 | $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name)), |
150 | $_ |
151 | ); |
152 | } $self->_all_files; |
153 | } |
154 | |
155 | sub all_paths { |
156 | my ($self) = @_; |
157 | my $slash = io->dir('/'); |
158 | map { |
159 | my ($path, $type) = $_->name =~ /^(.*)${\$self->_types_re}$/; |
160 | $slash->catdir(File::Spec->abs2rel($path, $self->top_dir->name))->name, |
161 | } $self->_all_files; |
162 | } |
163 | |
164 | sub _all_files { |
165 | my ($self) = @_; |
166 | return unless (my $base = $self->base_dir)->exists; |
167 | my %seen; |
168 | my $min = $self->min_depth; |
169 | map { |
170 | $_->filter(sub { $_->filename =~ /${\$self->_types_re}$/ }) |
171 | ->all_files($self->max_depth - ($min-1)) |
172 | } map |
173 | $min > 1 |
174 | ? do { |
175 | # can't use ->all_dirs($min-1) since we only want the final level |
176 | my @x = ($_); @x = map $_->all_dirs, @x for 1..$min-1; @x |
177 | } |
178 | : $_, |
179 | $base; |
180 | } |
181 | |
182 | sub latest { |
183 | my ($self, $max) = @_; |
0034f151 |
184 | use_module('App::SCS::LatestPageSet')->new( |
632f0e07 |
185 | parent => $self, |
186 | max_entries => $max, |
187 | ); |
188 | } |
189 | |
190 | sub _new_page { |
0034f151 |
191 | use_module('App::SCS::Page')->new( |
192 | path => $_[1], page_set => $_[0], %{$_[2]} |
193 | ); |
632f0e07 |
194 | } |
195 | |
196 | sub _types_re { qw/\.(html|md)/ } |
197 | |
198 | sub _extract_from_html { |
199 | my ($self, $html) = @_; |
200 | my %meta; |
201 | HTML::Zoom->from_html($html) |
202 | ->select('title')->collect_content({ into => \my @title }) |
203 | ->${\sub { |
204 | my $z = shift; |
205 | return reduce { |
206 | $a->collect("meta[name=${b}]", { into => ($meta{$b}=[]) }) |
207 | } $z, qw(subtitle description keywords created plugins) |
208 | }} |
209 | ->run; |
210 | +{ |
211 | title => $title[0]->{raw}||'', |
212 | (map +($_ => $meta{$_}[0]->{attrs}{content}||''), keys %meta), |
213 | html => $html, |
214 | } |
215 | } |
216 | |
217 | sub _extract_from_md { |
218 | my ($self, $md) = @_; |
632f0e07 |
219 | $self->_extract_from_html(markdown($md, { document_format => 'complete' })); |
220 | } |
221 | |
222 | 1; |