e5591fa558b3720a9634c1cfbb938dd85f235788
[gitmo/moose-website.git] / lib / Moose / Website.pm
1 package Moose::Website;
2 use Moose;
3 use MooseX::Types::Path::Class;
4
5 use Path::Class;
6 use Template;
7 use YAML::XS 'LoadFile';
8
9 use Moose::Website::I18N;
10 use Moose::Website::Resource::Templates;
11 use Moose::Website::Resource::WebFiles;
12
13 our $VERSION   = '0.01';
14 our $AUTHORITY = 'cpan:STEVAN';
15
16 with 'MooseX::Getopt';
17
18 has 'outdir' => (
19     is       => 'ro',
20     isa      => 'Path::Class::Dir',
21     coerce   => 1,
22     required => 1,
23 );
24
25 has 'locale' => (
26     is      => 'ro',
27     isa     => 'Str',
28     default => sub { 'en' },
29 );
30
31 has 'page_file' => (
32     is       => 'ro',
33     isa      => 'Path::Class::File',
34     coerce   => 1,
35     default  => sub {
36         file(__FILE__)->parent->parent->parent->file('data', 'pages.yml')
37     }
38 );
39
40 # ....
41
42 has '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'
52     }
53 );
54
55 has 'web_file_resource' => (
56     traits  => [ 'NoGetopt' ],
57     is      => 'ro',
58     isa     => 'Moose::Website::Resource::WebFiles',
59     lazy    => 1,
60     default => sub {
61         Moose::Website::Resource::WebFiles->new
62     },
63 );
64
65 has 'i18n' => (
66     traits  => [ 'NoGetopt' ],
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
76 has '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
87 has '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
101 has 'template_config' => (
102     traits  => [ 'NoGetopt' ],
103     is      => 'ro',
104     isa     => 'HashRef',
105     lazy    => 1,
106     default => sub { +{
107         ENCODING => 'UTF-8',
108     } },
109 );
110
111 sub log { shift; warn @_, "\n" }
112
113 sub run {
114     my $self = shift;
115
116     foreach my $page ( @{ $self->pages } ) {
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
122         $self->tt->process(
123             $page->{template},
124             $self->build_template_params( current_page => $page ),
125             $outfile,
126             { binmode => ':encoding(UTF-8)' },
127         ) || confess $self->tt->error;
128     }
129
130     $self->log( "Copying web resources to " . $self->outdir );
131     $self->web_file_resource->copy( to => $self->outdir );
132 }
133
134 sub build_template_params {
135     my ($self, %params) = @_;
136
137     $params{ pages }  = $self->pages;
138     $params{ loc }    = sub { $self->i18n->loc( @_ ) };
139     $params{ locale } = $self->locale;
140
141     \%params;
142 }
143
144
145 __PACKAGE__->meta->make_immutable;
146
147 no Moose; 1;
148
149 __END__
150
151 =pod
152
153 =head1 NAME
154
155 Moose::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
173 All complex software has bugs lurking in it, and this module is no
174 exception. If you find a bug please either email me, or add the bug
175 to cpan-RT.
176
177 =head1 AUTHOR
178
179 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
180
181 =head1 COPYRIGHT AND LICENSE
182
183 Copyright 2010 Infinity Interactive, Inc.
184
185 L<http://www.iinteractive.com>
186
187 This library is free software; you can redistribute it and/or modify
188 it under the same terms as Perl itself.
189
190 =cut