adding in the Moose blog
[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 );
51
52 sub template_root {
53     shift->template_resource->fetch('template_dir')->install_from_absolute;
54 }
55
56 has 'web_file_resource' => (
57     traits  => [ 'NoGetopt' ],
58     is      => 'ro',
59     isa     => 'Moose::Website::Resource::WebFiles',
60     lazy    => 1,
61     default => sub {
62         Moose::Website::Resource::WebFiles->new(install_to => shift->outdir)
63     },
64 );
65
66 has 'i18n' => (
67     traits  => [ 'NoGetopt' ],
68     is      => 'ro',
69     isa     => 'Object',
70     lazy    => 1,
71     default => sub {
72         my $self = shift;
73         Moose::Website::I18N->get_handle( $self->locale )
74     }
75 );
76
77 has 'pages' => (
78     traits  => [ 'NoGetopt' ],
79     is      => 'ro',
80     isa     => 'ArrayRef[HashRef]',
81     lazy    => 1,
82     default => sub {
83         my $self = shift;
84         LoadFile( $self->page_file->stringify );
85     }
86 );
87
88 has 'tt' => (
89     traits  => [ 'NoGetopt' ],
90     is      => 'ro',
91     isa     => 'Template',
92     lazy    => 1,
93     default => sub {
94         my $self = shift;
95         Template->new(
96             INCLUDE_PATH => $self->template_root,
97             %{ $self->template_config }
98         )
99     }
100 );
101
102 has 'template_config' => (
103     traits  => [ 'NoGetopt' ],
104     is      => 'ro',
105     isa     => 'HashRef',
106     lazy    => 1,
107     default => sub { +{
108         ENCODING => 'UTF-8',
109     } },
110 );
111
112 sub log { shift; warn @_, "\n" }
113
114 sub run {
115     my $self = shift;
116
117     foreach my $page ( @{ $self->pages } ) {
118
119         next unless exists $page->{template};
120
121         my $outfile = $self->outdir->file( $page->{outfile} )->stringify;
122
123         $self->log( "Writing page " . $page->{name} . " to $outfile using template " . $page->{template} );
124
125         $self->tt->process(
126             $page->{template},
127             $self->build_template_params( current_page => $page ),
128             $outfile,
129             { binmode => ':encoding(UTF-8)' },
130         ) || confess $self->tt->error;
131     }
132
133     $self->log( "Copying web resources to " . $self->outdir );
134     $self->web_file_resource->install;
135 }
136
137 sub build_template_params {
138     my ($self, %params) = @_;
139
140     $params{ pages }  = $self->pages;
141     $params{ loc }    = sub { $self->i18n->loc( @_ ) };
142     $params{ locale } = $self->locale;
143
144     \%params;
145 }
146
147
148 __PACKAGE__->meta->make_immutable;
149
150 no Moose; 1;
151
152 __END__
153
154 =pod
155
156 =head1 NAME
157
158 Moose::Website - A Moosey solution to this problem
159
160 =head1 SYNOPSIS
161
162   use Moose::Website;
163
164 =head1 DESCRIPTION
165
166 =head1 METHODS
167
168 =over 4
169
170 =item B<>
171
172 =back
173
174 =head1 BUGS
175
176 All complex software has bugs lurking in it, and this module is no
177 exception. If you find a bug please either email me, or add the bug
178 to cpan-RT.
179
180 =head1 AUTHOR
181
182 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
183
184 =head1 COPYRIGHT AND LICENSE
185
186 Copyright 2010 Infinity Interactive, Inc.
187
188 L<http://www.iinteractive.com>
189
190 This library is free software; you can redistribute it and/or modify
191 it under the same terms as Perl itself.
192
193 =cut