9 has pages => (is => 'lazy');
11 has filters => (is => 'lazy');
13 has _layout_zoom => (is => 'lazy');
15 has _feed_configs => (is => 'lazy');
17 has _feed_generator => (
19 handles => { _feed_http_response => 'feed_http_response' },
24 pages_dir => 'share/pages',
25 template_dir => 'share/templates',
26 static_dir => 'share/static',
27 feed_id_prefix => 'http://shadow.cat',
33 SCSite::PageSet->new(base_dir => io->dir($self->config->{pages_dir}))
38 require SCSite::SubListFilter;
39 require SCSite::SidebarFilter;
41 map +($_ => "SCSite::${_}Filter"->new_from_site($self)),
46 sub _build__feed_configs {
49 title => 'All Shadowcat blogs',
50 entries => { min_depth => 2, max_depth => 3 },
52 'blog/matt-s-trout' => {
53 title => q{Matt S Trout (mst)'s blog},
54 entries => { at_depth => 1 },
56 'blog/mark-keating' => {
57 title => q{Mark Keating (mdk)'s blog},
58 entries => { min_depth => 1, max_depth => 2 },
61 title => 'Shadowcat News',
62 entries => { at_depth => 4 },
65 $f->{$_}{base} ||= $_ for keys %$f;
69 sub _build__feed_generator {
71 require SCSite::FeedGenerator;
72 SCSite::FeedGenerator->new(
73 pages => $self->pages,
74 id_prefix => $self->config->{feed_id_prefix},
78 sub dispatch_request {
81 if (my $conf = $self->_feed_configs->{$_[1]}) {
82 $self->_feed_http_response(200 => $conf);
86 $self->_page_http_response(200 => $self->_find_page('index'));
89 [ 302, [ 'Location' => "/$_[1]/" ], [] ]
92 my ($code, $page) = map {
93 $_ ? (200, $_) : (404, $self->_error_page(404))
94 } $self->_find_page($_[1]);
95 $self->_page_http_response($code => $page);
100 my ($self, $code) = @_;
101 $self->_find_page("error_${code}");
105 my ($self, $path) = @_;
106 $self->pages->get({ path => $path });
110 my ($self, $code, $type, $content) = @_;
111 [ $code, [ 'Content-type' => $type ], [ $content ] ];
114 sub _page_http_response {
115 my ($self, $code, $page) = @_;
116 [ $code, [ 'Content-type' => 'text/html' ], $self->_render_page($page) ];
120 my ($self, $page) = @_;
121 my $zoom = $self->_layout_zoom;
122 my %filters = %{$self->filters};
123 $zoom->select('.page.title')->replace_content($page->title)
124 ->select('.page.subtitle')->${\sub {
126 ? $_[0]->replace_content($page->subtitle)
129 ->select('.page.published_at')->replace_content($page->published_at)
130 ->select('meta[name=description]')
131 ->set_attribute(content => $page->description)
132 ->select('meta[name=keywords]')
133 ->set_attribute(content => $page->keywords)
134 ->select('meta[name=created]')
135 ->set_attribute(content => $page->created)
136 ->select('.page.body')->replace_content(\$page->body)
138 foreach my $fname (sort keys %filters) {
139 my $cb = $filters{$fname}->callback_for($page);
140 $_ = $_->select(".${fname}")->collect({
141 filter => $cb, passthrough => 1
149 sub _build__layout_zoom {
151 HTML::Zoom->from_file(
152 io->dir($self->config->{template_dir})->catfile('layout.html')
157 return $_[0]->to_psgi_app if caller(1);
159 my @config_keys = keys %{{$class->default_config}};
160 require Getopt::Long;
161 my %config = map +($_ => $ENV{"SCS_${\uc $_}"}), @config_keys;
162 Getopt::Long::GetOptions(
163 map +("$_=s" => \$config{$_}), @config_keys
165 delete $config{$_} for grep !defined($config{$_}), keys %config;
166 my $new = $class->new(config => \%config);
170 around _run_cli => sub {
171 my ($orig, $self) = (shift, shift);
172 if (@_ >= 2 and $_[0] eq 'dev') {
173 require SCSite::DevMode;
174 Moo::Role->apply_roles_to_object($self, 'SCSite::DevMode');
175 if ($self->can("_run_dev_$_[1]")) {
176 return $self->${\"_run_dev_$_[1]"}(@_[2..$#_]);
178 die "No such dev mode $_[1]";
181 if (@_ >= 1 and my $code = $self->can("_run_cli_$_[0]")) {
183 return $self->$code(@_);
185 return $self->$orig(@_);
188 sub _run_cli_generate {
189 my ($self, $to, @spec) = @_;
190 die "generate requires a directory to generate to"
191 unless $to and -d $to;
194 if (@spec) { '^('.join('|',map quotemeta($_),@spec).')' }
197 foreach my $path ('', $self->pages->all_paths) {
198 next unless "$path/" =~ /$check/;
199 print "Generating ${path}\n";
200 my $dir = $out->catdir($path);
202 $dir->catfile('index.html')->print(
203 $self->run_test_request(GET => "$path/")->content
206 foreach my $path (map "/feed/$_/", keys %{$self->_feed_configs}) {
207 next unless "$path/" =~ /$check/;
208 print "Generating ${path}\n";
209 my $dir = $out->catdir($path);
211 $dir->catfile('index.atom')->print(
212 $self->run_test_request(GET => $path)->content
218 __PACKAGE__->run_if_script;