Commit | Line | Data |
632f0e07 |
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); |
0034f151 |
7 | use Module::Runtime qw(use_module); |
18968192 |
8 | use HTML::Zoom; |
632f0e07 |
9 | use Moo; |
10 | |
f50b4a35 |
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 | |
632f0e07 |
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) = @_; |
fd5c2ec2 |
27 | |
28 | return $self->plugins; |
632f0e07 |
29 | } |
30 | |
31 | sub has_plugin_config { exists $_[0]->plugin_config->{$_[1]} } |
32 | |
33 | sub with_plugin_config { |
34 | my ($self, $with_name, $with_config) = @_; |
35 | my @orig = @{$self->plugin_config}; |
36 | my @new; |
37 | while (my ($name, $config) = splice @orig, 0, 2) { |
38 | push @new, ( |
39 | $name eq $with_name |
40 | ? ($name, { %$config, %$with_config }) |
41 | : ($name, $config) |
42 | ); |
43 | } |
44 | return $self->with(plugins => pond_write_datum(\@new)); |
45 | } |
46 | |
e2e7175f |
47 | has _raw_page_plugins => (is => 'lazy', builder => sub { |
632f0e07 |
48 | my ($self) = @_; |
49 | my $plugin_config = $self->plugin_config; |
0034f151 |
50 | my ($plugin_map, $defaults) = @{$self->_page_set->plugin_config} |
51 | {qw(plugin_map defaults)}; |
632f0e07 |
52 | my @spec = (@$defaults, @$plugin_config); |
53 | my @plugins; |
54 | while (my ($name, $config) = splice @spec, 0, 2) { |
55 | my $info = $plugin_map->{$name}; |
56 | push @plugins, |
57 | use_module($info->{class})->new( |
e2e7175f |
58 | ($info->{config}||sub{})->(), %$config, page => $self, |
59 | plugin_map => $plugin_map, # some things will need this |
632f0e07 |
60 | ); |
61 | } |
62 | return \@plugins; |
e2e7175f |
63 | }); |
64 | |
65 | has _page_plugins => (is => 'lazy', builder => sub { |
66 | my ($self) = @_; |
67 | my $raw = $self->_raw_page_plugins; |
68 | reduce { $b->filter_plugins($a) } $raw, @$raw; |
69 | }); |
632f0e07 |
70 | |
71 | sub published_at { |
72 | $_[0]->created |
73 | ? scalar localtime timelocal |
74 | map +(@{$_}[0..3], $_->[4]-1, $_->[5]-1900), |
75 | [ reverse split '\D+', $_[0]->created ] |
76 | : '' |
77 | } |
78 | |
632f0e07 |
79 | sub to_app { |
80 | my ($self) = @_; |
81 | return sub { $self->to_psgi_response(@_) }; |
82 | } |
83 | |
84 | sub to_psgi_response { |
85 | my ($self, $env) = @_; |
86 | |
87 | if (my $cb = $env->{'App::SCS::Command::Generate.extra_pages'}) { |
88 | $cb->($_->extra_pages) for @{$self->page_plugins}; |
89 | } |
90 | |
91 | $self->_psgi_response; |
92 | } |
93 | |
94 | has _psgi_response => (is => 'lazy'); |
95 | |
96 | sub _build__psgi_response { |
97 | my ($self) = @_; |
98 | |
632f0e07 |
99 | my $psgi_res = [ |
0034f151 |
100 | 200, [ 'Content-type' => 'text/html' ], $self->_content_zoom->to_fh |
632f0e07 |
101 | ]; |
102 | |
103 | return reduce { |
104 | $b->filter_psgi_response($a) |
0034f151 |
105 | } $psgi_res, @{$self->_page_plugins}; |
106 | } |
107 | |
108 | sub _content_zoom { |
109 | my ($self) = @_; |
110 | return reduce { |
111 | $b->filter_content_zoom($a) |
112 | } $self->_html_zoom, @{$self->_page_plugins}; |
113 | } |
114 | |
115 | sub _html_zoom { |
116 | my ($self) = @_; |
117 | return reduce { |
118 | $b->filter_html_zoom($a) |
119 | } HTML::Zoom->from_html($self->html), @{$self->_page_plugins}; |
632f0e07 |
120 | } |
121 | |
18968192 |
122 | sub body { |
123 | my ($self) = @_; |
124 | HTML::Zoom->from_html($self->html) |
125 | ->collect(body => { into => \my @ev }) |
126 | ->run; |
127 | HTML::Zoom->from_events(\@ev)->to_html; |
128 | } |
129 | |
632f0e07 |
130 | no Moo; |
131 | |
132 | sub with { |
133 | my $self = shift; |
134 | return ref($self)->new(%$self, @_); |
135 | } |
136 | |
137 | 1; |