1 package App::SCS::Page;
4 use Time::Local qw(timelocal);
5 use Data::Pond qw(pond_read_datum pond_write_datum);
6 use List::Util qw(reduce);
7 use Module::Runtime qw(use_module);
10 with 'App::SCS::Role::PageChildren';
12 has $_ => (is => 'ro') for qw(
13 title subtitle description keywords plugins html created path
16 has plugin_config => (is => 'lazy');
18 sub _build_plugin_config {
21 ? pond_read_datum('[ '.$self->plugins.' ]')
25 sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} }
27 sub with_plugin_config {
28 my ($self, $with_name, $with_config) = @_;
29 my @orig = @{$self->plugin_config};
31 while (my ($name, $config) = splice @orig, 0, 2) {
34 ? ($name, { %$config, %$with_config })
38 return $self->with(plugins => pond_write_datum(\@new));
41 has _page_plugins => (is => 'lazy');
43 sub _build__page_plugins {
45 my $plugin_config = $self->plugin_config;
46 my ($plugin_map, $defaults) = @{$self->_page_set->plugin_config}
47 {qw(plugin_map defaults)};
48 my @spec = (@$defaults, @$plugin_config);
50 while (my ($name, $config) = splice @spec, 0, 2) {
51 my $info = $plugin_map->{$name};
53 use_module($info->{class})->new(
54 ($info->{config}||sub{})->(), %$config, page => $self
62 ? scalar localtime timelocal
63 map +(@{$_}[0..3], $_->[4]-1, $_->[5]-1900),
64 [ reverse split '\D+', $_[0]->created ]
68 has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
70 sub _page_set_class { ref($_[0]->_page_set) }
71 sub _top_dir { $_[0]->_page_set->top_dir }
72 sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
76 return sub { $self->to_psgi_response(@_) };
79 sub to_psgi_response {
80 my ($self, $env) = @_;
82 if (my $cb = $env->{'App::SCS::Command::Generate.extra_pages'}) {
83 $cb->($_->extra_pages) for @{$self->page_plugins};
86 $self->_psgi_response;
89 has _psgi_response => (is => 'lazy');
91 sub _build__psgi_response {
95 200, [ 'Content-type' => 'text/html' ], $self->_content_zoom->to_fh
99 $b->filter_psgi_response($a)
100 } $psgi_res, @{$self->_page_plugins};
106 $b->filter_content_zoom($a)
107 } $self->_html_zoom, @{$self->_page_plugins};
113 $b->filter_html_zoom($a)
114 } HTML::Zoom->from_html($self->html), @{$self->_page_plugins};
121 return ref($self)->new(%$self, @_);