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