/
Site.pm
280 lines (205 loc) · 5.71 KB
/
Site.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package Statocles::Site;
# ABSTRACT: An entire, configured website
use Statocles::Base 'Class';
use Statocles::Store;
use Mojo::URL;
use Mojo::DOM;
use Mojo::Log;
=attr title
The site title, used in templates.
=cut
has title => (
is => 'ro',
isa => Str,
);
=attr base_url
The base URL of the site, including protocol and domain. Used mostly for feeds.
=cut
has base_url => (
is => 'ro',
isa => Str,
);
=attr apps
The applications in this site. Each application has a name
that can be used later.
=cut
has apps => (
is => 'ro',
isa => HashRef[InstanceOf['Statocles::App']],
);
=attr index
The application to use as the site index. The application's individual index()
method will be called to get the index page.
=cut
has index => (
is => 'ro',
isa => Str,
default => sub { '' },
);
=attr nav
Named navigation lists. A hash of arrays of hashes with the following keys:
title - The title of the link
href - The href of the link
The most likely name for your navigation will be C<main>. Navigation names
are defined by your L<theme|Statocles::Theme>. For example:
{
main => [
{
title => 'Blog',
href => '/blog/index.html',
},
{
title => 'Contact',
href => '/contact.html',
},
],
}
=cut
has nav => (
is => 'ro',
isa => HashRef[ArrayRef[HashRef[Str]]],
default => sub { {} },
);
=attr build_store
The L<store|Statocles::Store> object to use for C<build()>. This is a workspace
and will be rebuilt often, using the C<build> and C<daemon> commands. This is
also the store the C<daemon> command reads to serve the site.
=cut
has build_store => (
is => 'ro',
isa => Store,
required => 1,
coerce => Store->coercion,
);
=attr deploy_store
The L<store|Statocles::Store> object to use for C<deploy()>. Defaults to L<build_store>.
This is intended to be the production deployment of the site. A build gets promoted
to production by using the C<deploy> command.
=cut
has deploy_store => (
is => 'ro',
isa => Store,
lazy => 1,
default => sub { $_[0]->build_store },
coerce => Store->coercion,
);
=attr log
A L<Mojo::Log> object to write logs to. Defaults to STDERR.
=cut
has log => (
is => 'ro',
isa => InstanceOf['Mojo::Log'],
lazy => 1,
default => sub {
Mojo::Log->new( level => 'info' );
},
);
=method BUILD
Register this site as the global site.
=cut
sub BUILD {
$Statocles::SITE = shift;
}
=method app( name )
Get the app with the given C<name>.
=cut
sub app {
my ( $self, $name ) = @_;
return $self->apps->{ $name };
}
=method build
Build the site in its build location
=cut
sub build {
my ( $self ) = @_;
$self->write( $self->build_store );
}
=method deploy
Write each application to its destination.
=cut
sub deploy {
my ( $self ) = @_;
$self->write( $self->deploy_store );
}
=method write( store )
Write the application to the given L<store|Statocles::Store>.
=cut
sub write {
my ( $self, $store ) = @_;
my $apps = $self->apps;
my @pages;
my %args = (
site => $self,
);
# Collect all the pages for this site
for my $app_name ( keys %{ $apps } ) {
my $app = $apps->{$app_name};
my @app_pages = $app->pages;
if ( $self->index eq $app_name ) {
# Rename the app's page so that we don't get two pages with identical
# content, which is bad for SEO
$app_pages[0]->path( '/index.html' );
}
push @pages, @app_pages;
}
# Rewrite page content to add base URL
my $base_path = Mojo::URL->new( $self->base_url )->path;
$base_path =~ s{/$}{};
for my $page ( @pages ) {
my $content = $page->render( %args );
if ( !ref $content ) {
if ( $base_path =~ /\S/ ) {
my $dom = Mojo::DOM->new( $content );
for my $attr ( qw( src href ) ) {
for my $el ( $dom->find( "[$attr]" )->each ) {
my $url = $el->attr( $attr );
next unless $url =~ m{^/};
$el->attr( $attr, join "", $base_path, $url );
}
}
$content = $dom->to_string;
}
}
$store->write_file( $page->path, $content );
}
# Build the sitemap.xml
my @indexed_pages = grep { !$_->isa( 'Statocles::Page::Feed' ) } @pages;
my $default_theme = Statocles::Theme->new( store => '::default' );
my $tmpl = $default_theme->template( site => 'sitemap.xml' );
$store->write_file( 'sitemap.xml', $tmpl->render( site => $self, pages => \@indexed_pages ) );
# robots.txt is the best way for crawlers to automatically discover sitemap.xml
# We should do more with this later...
my @robots = (
"Sitemap: " . $self->url( 'sitemap.xml' ),
"User-Agent: *",
"Disallow: ",
);
$store->write_file( 'robots.txt', join "\n", @robots );
}
=method url( path )
Get the full URL to the given path by prepending the C<base_url>.
=cut
sub url {
my ( $self, $path ) = @_;
my $base = $self->base_url;
# Remove the / from both sides of the join so we don't double up
$base =~ s{/$}{};
$path =~ s{^/}{};
return join "/", $base, $path;
}
1;
__END__
=head1 SYNOPSIS
my $site = Statocles::Site->new(
title => 'My Site',
nav => [
{ title => 'Home', href => '/' },
{ title => 'Blog', href => '/blog' },
],
apps => {
blog => Statocles::App::Blog->new( ... ),
},
);
$site->deploy;
=head1 DESCRIPTION
A Statocles::Site is a collection of L<applications|Statocles::App>.