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);
11 has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
13 sub _page_set_class { ref($_[0]->_page_set) }
14 sub _top_dir { $_[0]->_page_set->top_dir }
15 sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
17 with 'App::SCS::Role::PageChildren';
19 has $_ => (is => 'ro') for qw(
20 title subtitle description keywords plugins html created path
23 has plugin_config => (is => 'lazy');
25 sub _build_plugin_config {
28 my $pluginref = $self->plugins
29 ? pond_read_datum('[ '.$self->plugins.' ]')
32 my @dirs = io->dir($self->path)->splitdir;
35 foreach my $dir (@dirs) {
37 #/home/.../share/pages/blog/config.pond etc
39 my $file = $self->_top_dir . "$path/config.pond";
43 next if io($file)->empty;
45 my $content = io($file)->slurp;
47 my $config = pond_read_datum($content);
48 push @$pluginref, @{$config->{plugins}};
55 sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} }
57 sub with_plugin_config {
58 my ($self, $with_name, $with_config) = @_;
59 my @orig = @{$self->plugin_config};
61 while (my ($name, $config) = splice @orig, 0, 2) {
64 ? ($name, { %$config, %$with_config })
68 return $self->with(plugins => pond_write_datum(\@new));
71 has _raw_page_plugins => (is => 'lazy', builder => sub {
73 my $plugin_config = $self->plugin_config;
74 my ($plugin_map, $defaults) = @{$self->_page_set->plugin_config}
75 {qw(plugin_map defaults)};
76 my @spec = (@$defaults, @$plugin_config);
78 while (my ($name, $config) = splice @spec, 0, 2) {
79 my $info = $plugin_map->{$name};
81 use_module($info->{class})->new(
82 ($info->{config}||sub{})->(), %$config, page => $self,
83 plugin_map => $plugin_map, # some things will need this
89 has _page_plugins => (is => 'lazy', builder => sub {
91 my $raw = $self->_raw_page_plugins;
92 reduce { $b->filter_plugins($a) } $raw, @$raw;
97 ? scalar localtime timelocal
98 map +(@{$_}[0..3], $_->[4]-1, $_->[5]-1900),
99 [ reverse split '\D+', $_[0]->created ]
105 return sub { $self->to_psgi_response(@_) };
108 sub to_psgi_response {
109 my ($self, $env) = @_;
111 if (my $cb = $env->{'App::SCS::Command::Generate.extra_pages'}) {
112 $cb->($_->extra_pages) for @{$self->page_plugins};
115 $self->_psgi_response;
118 has _psgi_response => (is => 'lazy');
120 sub _build__psgi_response {
124 200, [ 'Content-type' => 'text/html' ], $self->_content_zoom->to_fh
128 $b->filter_psgi_response($a)
129 } $psgi_res, @{$self->_page_plugins};
135 $b->filter_content_zoom($a)
136 } $self->_html_zoom, @{$self->_page_plugins};
142 $b->filter_html_zoom($a)
143 } HTML::Zoom->from_html($self->html), @{$self->_page_plugins};
148 HTML::Zoom->from_html($self->html)
149 ->collect(body => { into => \my @ev })
151 HTML::Zoom->from_events(\@ev)->to_html;
158 return ref($self)->new(%$self, @_);