more data and templates
[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';
8use Moose::Website::I18N;
9
10our $VERSION = '0.01';
11our $AUTHORITY = 'cpan:STEVAN';
12
13with 'MooseX::Getopt';
14
5efab74a 15has 'outdir' => (
7476895d 16 is => 'ro',
5efab74a 17 isa => 'Path::Class::Dir',
7476895d 18 coerce => 1,
19 required => 1,
20);
21
5efab74a 22has 'page_file' => (
7476895d 23 is => 'ro',
5efab74a 24 isa => 'Path::Class::File',
7476895d 25 coerce => 1,
5efab74a 26 default => sub {
27 file(__FILE__)->parent->parent->parent->file('data', 'pages.yml')
28 }
7476895d 29);
30
31has 'template_root' => (
32 is => 'ro',
33 isa => 'Path::Class::Dir',
34 coerce => 1,
5efab74a 35 default => sub {
36 file(__FILE__)->parent->parent->parent->subdir('templates')
37 }
7476895d 38);
39
40has 'locale' => (
41 is => 'ro',
42 isa => 'Str',
43 default => sub { 'en' },
44);
45
46# ....
47
48has '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
58has '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
69has '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
5efab74a 83has 'template_config' => (
84 traits => [ 'NoGetopt' ],
85 is => 'ro',
86 isa => 'HashRef',
87 lazy => 1,
88 default => sub { +{} },
89);
90
7476895d 91sub log { shift; warn @_, "\n" }
92
93sub run {
94 my $self = shift;
95
96 foreach my $page ( @{ $self->pages } ) {
5efab74a 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
7476895d 102 $self->tt->process(
103 $page->{template},
104 $self->build_template_params( current_page => $page ),
5efab74a 105 $outfile
7476895d 106 ) || confess $self->tt->error;
107 }
108}
109
110sub 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
122no Moose; 1;
123
124__END__
125
126=pod
127
128=head1 NAME
129
130Moose::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
148All complex software has bugs lurking in it, and this module is no
149exception. If you find a bug please either email me, or add the bug
150to cpan-RT.
151
152=head1 AUTHOR
153
154Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
155
156=head1 COPYRIGHT AND LICENSE
157
158Copyright 2010 Infinity Interactive, Inc.
159
160L<http://www.iinteractive.com>
161
162This library is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself.
164
165=cut