using the new Resource::Pack::URL role for jquery
[gitmo/moose-website.git] / lib / Moose / Website.pm
CommitLineData
7476895d 1package Moose::Website;
2use Moose;
3use MooseX::Types::Path::Class;
4
5efab74a 5use Path::Class;
7476895d 6use Template;
7use YAML::XS 'LoadFile';
3cb66fff 8
7476895d 9use Moose::Website::I18N;
3cb66fff 10use Moose::Website::Resource::Templates;
11use Moose::Website::Resource::WebFiles;
7476895d 12
13our $VERSION = '0.01';
14our $AUTHORITY = 'cpan:STEVAN';
15
16with 'MooseX::Getopt';
17
5efab74a 18has 'outdir' => (
7476895d 19 is => 'ro',
5efab74a 20 isa => 'Path::Class::Dir',
7476895d 21 coerce => 1,
22 required => 1,
23);
24
3cb66fff 25has 'locale' => (
26 is => 'ro',
27 isa => 'Str',
28 default => sub { 'en' },
29);
30
5efab74a 31has 'page_file' => (
7476895d 32 is => 'ro',
5efab74a 33 isa => 'Path::Class::File',
7476895d 34 coerce => 1,
5efab74a 35 default => sub {
36 file(__FILE__)->parent->parent->parent->file('data', 'pages.yml')
37 }
7476895d 38);
39
3cb66fff 40# ....
41
42has 'template_resource' => (
43 traits => [ 'NoGetopt' ],
44 is => 'ro',
45 isa => 'Moose::Website::Resource::Templates',
46 lazy => 1,
47 default => sub {
48 Moose::Website::Resource::Templates->new
49 },
50 handles => {
51 'template_root' => 'dir'
5efab74a 52 }
7476895d 53);
54
3cb66fff 55has 'web_file_resource' => (
56 traits => [ 'NoGetopt' ],
7476895d 57 is => 'ro',
3cb66fff 58 isa => 'Moose::Website::Resource::WebFiles',
59 lazy => 1,
60 default => sub {
61 Moose::Website::Resource::WebFiles->new
62 },
7476895d 63);
64
7476895d 65has 'i18n' => (
3cb66fff 66 traits => [ 'NoGetopt' ],
7476895d 67 is => 'ro',
68 isa => 'Object',
69 lazy => 1,
70 default => sub {
71 my $self = shift;
72 Moose::Website::I18N->get_handle( $self->locale )
73 }
74);
75
76has 'pages' => (
77 traits => [ 'NoGetopt' ],
78 is => 'ro',
79 isa => 'ArrayRef[HashRef]',
80 lazy => 1,
81 default => sub {
82 my $self = shift;
83 LoadFile( $self->page_file->stringify );
84 }
85);
86
87has 'tt' => (
88 traits => [ 'NoGetopt' ],
89 is => 'ro',
90 isa => 'Template',
91 lazy => 1,
92 default => sub {
93 my $self = shift;
94 Template->new(
95 INCLUDE_PATH => $self->template_root,
96 %{ $self->template_config }
97 )
98 }
99);
100
5efab74a 101has 'template_config' => (
102 traits => [ 'NoGetopt' ],
103 is => 'ro',
104 isa => 'HashRef',
105 lazy => 1,
dc40457b 106 default => sub { +{
107 ENCODING => 'UTF-8',
108 } },
5efab74a 109);
110
7476895d 111sub log { shift; warn @_, "\n" }
112
113sub run {
114 my $self = shift;
115
116 foreach my $page ( @{ $self->pages } ) {
5efab74a 117
118 my $outfile = $self->outdir->file( $page->{outfile} )->stringify;
119
120 $self->log( "Writing page " . $page->{name} . " to $outfile using template " . $page->{template} );
121
7476895d 122 $self->tt->process(
123 $page->{template},
124 $self->build_template_params( current_page => $page ),
dc40457b 125 $outfile,
126 { binmode => ':encoding(UTF-8)' },
7476895d 127 ) || confess $self->tt->error;
128 }
3cb66fff 129
130 $self->log( "Copying web resources to " . $self->outdir );
5460f713 131 $self->web_file_resource->copy( to => $self->outdir, include_deps => 1 );
7476895d 132}
133
134sub build_template_params {
135 my ($self, %params) = @_;
136
df04a1b7 137 $params{ pages } = $self->pages;
138 $params{ loc } = sub { $self->i18n->loc( @_ ) };
139 $params{ locale } = $self->locale;
7476895d 140
141 \%params;
142}
143
144
145__PACKAGE__->meta->make_immutable;
146
147no Moose; 1;
148
149__END__
150
151=pod
152
153=head1 NAME
154
155Moose::Website - A Moosey solution to this problem
156
157=head1 SYNOPSIS
158
159 use Moose::Website;
160
161=head1 DESCRIPTION
162
163=head1 METHODS
164
165=over 4
166
167=item B<>
168
169=back
170
171=head1 BUGS
172
173All complex software has bugs lurking in it, and this module is no
174exception. If you find a bug please either email me, or add the bug
175to cpan-RT.
176
177=head1 AUTHOR
178
179Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
180
181=head1 COPYRIGHT AND LICENSE
182
183Copyright 2010 Infinity Interactive, Inc.
184
185L<http://www.iinteractive.com>
186
187This library is free software; you can redistribute it and/or modify
188it under the same terms as Perl itself.
189
190=cut