1 package App::SCS::Page;
4 use Time::Local qw(timelocal);
5 use List::Util qw(reduce);
6 use Module::Runtime qw(use_module);
10 has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
12 sub _page_set_class { ref($_[0]->_page_set) }
13 sub _top_dir { $_[0]->_page_set->top_dir }
14 sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
16 with 'App::SCS::Role::PageChildren';
18 has $_ => (is => 'ro') for qw(
19 title subtitle description keywords html created path
22 has plugin_config => (is => 'rw', 'required' => 1);
24 sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} }
26 sub with_plugin_config {
27 my ($self, $with_name, $with_config) = @_;
28 my @orig = @{$self->plugin_config};
30 while (my ($name, $config) = splice @orig, 0, 2) {
33 ? ($name, { %$config, %$with_config })
37 return $self->with(plugin_config => \@new);
40 has _raw_page_plugins => (is => 'lazy', builder => sub {
42 my $plugin_config = $self->plugin_config;
43 my ($plugin_map, $defaults) = @{$self->_page_set->plugin_config}
44 {qw(plugin_map defaults)};
45 my @spec = (@$defaults, @$plugin_config);
47 while (my ($name, $config) = splice @spec, 0, 2) {
48 my $info = $plugin_map->{$name};
50 use_module($info->{class})->new(
51 ($info->{config}||sub{})->(), %$config, page => $self,
52 plugin_map => $plugin_map, # some things will need this
58 has _page_plugins => (is => 'lazy', builder => sub {
60 my $raw = $self->_raw_page_plugins;
61 reduce { $b->filter_plugins($a) } $raw, @$raw;
66 ? scalar localtime timelocal
67 map +(@{$_}[0..3], $_->[4]-1, $_->[5]-1900),
68 [ reverse split '\D+', $_[0]->created ]
74 return sub { $self->to_psgi_response(@_) };
77 sub to_psgi_response {
78 my ($self, $env) = @_;
80 if (my $cb = $env->{'App::SCS::Command::Generate.extra_pages'}) {
81 $cb->($_->extra_pages) for @{$self->page_plugins};
84 $self->_psgi_response;
87 has _psgi_response => (is => 'lazy');
89 sub _build__psgi_response {
93 200, [ 'Content-type' => 'text/html' ], $self->_content_zoom->to_fh
97 $b->filter_psgi_response($a)
98 } $psgi_res, @{$self->_page_plugins};
104 $b->filter_content_zoom($a)
105 } $self->_html_zoom, @{$self->_page_plugins};
111 $b->filter_html_zoom($a)
112 } HTML::Zoom->from_html($self->html), @{$self->_page_plugins};
117 HTML::Zoom->from_html($self->html)
118 ->collect(body => { into => \my @ev })
120 HTML::Zoom->from_events(\@ev)->to_html;
127 return ref($self)->new(%$self, @_);