Config only from the previous dir on
[scpubgit/App-SCS.git] / lib / App / SCS / Page.pm
1 package App::SCS::Page;
2
3 use IO::All;
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);
8 use HTML::Zoom;
9 use Moo;
10
11 has "_$_" => (is => 'ro', init_arg => $_) for qw(page_set);
12
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) }
16
17 with 'App::SCS::Role::PageChildren';
18
19 has $_ => (is => 'ro') for qw(
20   title subtitle description keywords plugins html created path
21 );
22
23 has plugin_config => (is => 'lazy');
24
25 sub _build_plugin_config {
26   my ($self) = @_;
27
28   my $pluginref = $self->plugins
29     ? pond_read_datum('[ '.$self->plugins.' ]')
30     : [];
31
32   my @dirs = io->dir($self->path)->splitdir;
33   pop @dirs;
34   my $path = '';
35
36   foreach my $dir (@dirs) {
37       $path .= "/$dir";
38       #/home/.../share/pages/blog/config.pond etc
39
40       my $file = $self->_top_dir . "$path/config.pond";
41
42       next if !-f $file;
43
44       next if io($file)->empty;
45
46       my $content = io($file)->slurp;
47
48       my $config = pond_read_datum($content);
49       push @$pluginref, @{$config->{plugins}};
50   }
51
52
53   return $pluginref;
54 }
55
56 sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} }
57
58 sub with_plugin_config {
59   my ($self, $with_name, $with_config) = @_;
60   my @orig = @{$self->plugin_config};
61   my @new;
62   while (my ($name, $config) = splice @orig, 0, 2) {
63     push @new, (
64       $name eq $with_name
65         ? ($name, { %$config, %$with_config })
66         : ($name, $config)
67     );
68   }
69   return $self->with(plugins => pond_write_datum(\@new));
70 }
71
72 has _raw_page_plugins => (is => 'lazy', builder => sub {
73   my ($self) = @_;
74   my $plugin_config = $self->plugin_config;
75   my ($plugin_map, $defaults) = @{$self->_page_set->plugin_config}
76                                  {qw(plugin_map defaults)};
77   my @spec = (@$defaults, @$plugin_config);
78   my @plugins;
79   while (my ($name, $config) = splice @spec, 0, 2) {
80     my $info = $plugin_map->{$name};
81     push @plugins,
82       use_module($info->{class})->new(
83         ($info->{config}||sub{})->(), %$config, page => $self,
84         plugin_map => $plugin_map, # some things will need this
85       );
86   }
87   return \@plugins;
88 });
89
90 has _page_plugins => (is => 'lazy', builder => sub {
91   my ($self) = @_;
92   my $raw = $self->_raw_page_plugins;
93   reduce { $b->filter_plugins($a) } $raw, @$raw;
94 });
95
96 sub published_at {
97   $_[0]->created
98     ? scalar localtime timelocal
99         map +(@{$_}[0..3], $_->[4]-1, $_->[5]-1900),
100           [ reverse split '\D+', $_[0]->created ]
101     : ''
102 }
103
104 sub to_app {
105   my ($self) = @_;
106   return sub { $self->to_psgi_response(@_) };
107 }
108
109 sub to_psgi_response {
110   my ($self, $env) = @_;
111
112   if (my $cb = $env->{'App::SCS::Command::Generate.extra_pages'}) {
113     $cb->($_->extra_pages) for @{$self->page_plugins};
114   }
115
116   $self->_psgi_response;
117 }
118
119 has _psgi_response => (is => 'lazy');
120
121 sub _build__psgi_response {
122   my ($self) = @_;
123
124   my $psgi_res = [
125     200, [ 'Content-type' => 'text/html' ], $self->_content_zoom->to_fh
126   ];
127
128   return reduce {
129     $b->filter_psgi_response($a)
130   } $psgi_res, @{$self->_page_plugins};
131 }
132
133 sub _content_zoom {
134   my ($self) = @_;
135   return reduce {
136     $b->filter_content_zoom($a)
137   } $self->_html_zoom, @{$self->_page_plugins};
138 }
139
140 sub _html_zoom {
141   my ($self) = @_;
142   return reduce {
143     $b->filter_html_zoom($a)
144   } HTML::Zoom->from_html($self->html), @{$self->_page_plugins};
145 }
146
147 sub body {
148   my ($self) = @_;
149   HTML::Zoom->from_html($self->html)
150             ->collect(body => { into => \my @ev })
151             ->run;
152   HTML::Zoom->from_events(\@ev)->to_html;
153 }
154
155 no Moo;
156
157 sub with {
158   my $self = shift;
159   return ref($self)->new(%$self, @_);
160 }
161
162 1;