add plugins filtering plugins and implement RemovePlugin PagePlugin
[scpubgit/App-SCS.git] / lib / App / SCS / Page.pm
CommitLineData
632f0e07 1package App::SCS::Page;
2
3use IO::All;
4use Time::Local qw(timelocal);
5use Data::Pond qw(pond_read_datum pond_write_datum);
6use List::Util qw(reduce);
0034f151 7use Module::Runtime qw(use_module);
18968192 8use HTML::Zoom;
632f0e07 9use Moo;
10
f50b4a35 11has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
12
13sub _page_set_class { ref($_[0]->_page_set) }
14sub _top_dir { $_[0]->_page_set->top_dir }
15sub _my_path { io->dir($_[0]->_top_dir)->catdir($_[0]->path) }
16
632f0e07 17with 'App::SCS::Role::PageChildren';
18
19has $_ => (is => 'ro') for qw(
20 title subtitle description keywords plugins html created path
21);
22
23has plugin_config => (is => 'lazy');
24
25sub _build_plugin_config {
26 my ($self) = @_;
27 $self->plugins
28 ? pond_read_datum('[ '.$self->plugins.' ]')
29 : []
30}
31
32sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} }
33
34sub with_plugin_config {
35 my ($self, $with_name, $with_config) = @_;
36 my @orig = @{$self->plugin_config};
37 my @new;
38 while (my ($name, $config) = splice @orig, 0, 2) {
39 push @new, (
40 $name eq $with_name
41 ? ($name, { %$config, %$with_config })
42 : ($name, $config)
43 );
44 }
45 return $self->with(plugins => pond_write_datum(\@new));
46}
47
e2e7175f 48has _raw_page_plugins => (is => 'lazy', builder => sub {
632f0e07 49 my ($self) = @_;
50 my $plugin_config = $self->plugin_config;
0034f151 51 my ($plugin_map, $defaults) = @{$self->_page_set->plugin_config}
52 {qw(plugin_map defaults)};
632f0e07 53 my @spec = (@$defaults, @$plugin_config);
54 my @plugins;
55 while (my ($name, $config) = splice @spec, 0, 2) {
56 my $info = $plugin_map->{$name};
57 push @plugins,
58 use_module($info->{class})->new(
e2e7175f 59 ($info->{config}||sub{})->(), %$config, page => $self,
60 plugin_map => $plugin_map, # some things will need this
632f0e07 61 );
62 }
63 return \@plugins;
e2e7175f 64});
65
66has _page_plugins => (is => 'lazy', builder => sub {
67 my ($self) = @_;
68 my $raw = $self->_raw_page_plugins;
69 reduce { $b->filter_plugins($a) } $raw, @$raw;
70});
632f0e07 71
72sub published_at {
73 $_[0]->created
74 ? scalar localtime timelocal
75 map +(@{$_}[0..3], $_->[4]-1, $_->[5]-1900),
76 [ reverse split '\D+', $_[0]->created ]
77 : ''
78}
79
632f0e07 80sub to_app {
81 my ($self) = @_;
82 return sub { $self->to_psgi_response(@_) };
83}
84
85sub to_psgi_response {
86 my ($self, $env) = @_;
87
88 if (my $cb = $env->{'App::SCS::Command::Generate.extra_pages'}) {
89 $cb->($_->extra_pages) for @{$self->page_plugins};
90 }
91
92 $self->_psgi_response;
93}
94
95has _psgi_response => (is => 'lazy');
96
97sub _build__psgi_response {
98 my ($self) = @_;
99
632f0e07 100 my $psgi_res = [
0034f151 101 200, [ 'Content-type' => 'text/html' ], $self->_content_zoom->to_fh
632f0e07 102 ];
103
104 return reduce {
105 $b->filter_psgi_response($a)
0034f151 106 } $psgi_res, @{$self->_page_plugins};
107}
108
109sub _content_zoom {
110 my ($self) = @_;
111 return reduce {
112 $b->filter_content_zoom($a)
113 } $self->_html_zoom, @{$self->_page_plugins};
114}
115
116sub _html_zoom {
117 my ($self) = @_;
118 return reduce {
119 $b->filter_html_zoom($a)
120 } HTML::Zoom->from_html($self->html), @{$self->_page_plugins};
632f0e07 121}
122
18968192 123sub body {
124 my ($self) = @_;
125 HTML::Zoom->from_html($self->html)
126 ->collect(body => { into => \my @ev })
127 ->run;
128 HTML::Zoom->from_events(\@ev)->to_html;
129}
130
632f0e07 131no Moo;
132
133sub with {
134 my $self = shift;
135 return ref($self)->new(%$self, @_);
136}
137
1381;