Commit | Line | Data |
7476895d |
1 | package Moose::Website; |
2 | use Moose; |
3 | use MooseX::Types::Path::Class; |
4 | |
5efab74a |
5 | use Path::Class; |
7476895d |
6 | use Template; |
7 | use YAML::XS 'LoadFile'; |
3cb66fff |
8 | |
7476895d |
9 | use Moose::Website::I18N; |
3cb66fff |
10 | use Moose::Website::Resource::Templates; |
11 | use Moose::Website::Resource::WebFiles; |
7476895d |
12 | |
13 | our $VERSION = '0.01'; |
14 | our $AUTHORITY = 'cpan:STEVAN'; |
15 | |
16 | with 'MooseX::Getopt'; |
17 | |
5efab74a |
18 | has 'outdir' => ( |
7476895d |
19 | is => 'ro', |
5efab74a |
20 | isa => 'Path::Class::Dir', |
7476895d |
21 | coerce => 1, |
22 | required => 1, |
23 | ); |
24 | |
3cb66fff |
25 | has 'locale' => ( |
26 | is => 'ro', |
27 | isa => 'Str', |
28 | default => sub { 'en' }, |
29 | ); |
30 | |
5efab74a |
31 | has '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 | |
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' |
5efab74a |
52 | } |
7476895d |
53 | ); |
54 | |
3cb66fff |
55 | has '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 |
65 | has '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 | |
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 | |
5efab74a |
101 | has 'template_config' => ( |
102 | traits => [ 'NoGetopt' ], |
103 | is => 'ro', |
104 | isa => 'HashRef', |
105 | lazy => 1, |
106 | default => sub { +{} }, |
107 | ); |
108 | |
7476895d |
109 | sub log { shift; warn @_, "\n" } |
110 | |
111 | sub run { |
112 | my $self = shift; |
113 | |
114 | foreach my $page ( @{ $self->pages } ) { |
5efab74a |
115 | |
116 | my $outfile = $self->outdir->file( $page->{outfile} )->stringify; |
117 | |
118 | $self->log( "Writing page " . $page->{name} . " to $outfile using template " . $page->{template} ); |
119 | |
7476895d |
120 | $self->tt->process( |
121 | $page->{template}, |
122 | $self->build_template_params( current_page => $page ), |
5efab74a |
123 | $outfile |
7476895d |
124 | ) || confess $self->tt->error; |
125 | } |
3cb66fff |
126 | |
127 | $self->log( "Copying web resources to " . $self->outdir ); |
128 | $self->web_file_resource->copy( to => $self->outdir ); |
7476895d |
129 | } |
130 | |
131 | sub build_template_params { |
132 | my ($self, %params) = @_; |
133 | |
134 | $params{ pages } = $self->pages; |
135 | $params{ loc } = sub { $self->i18n->loc( @_ ) }; |
136 | |
137 | \%params; |
138 | } |
139 | |
140 | |
141 | __PACKAGE__->meta->make_immutable; |
142 | |
143 | no Moose; 1; |
144 | |
145 | __END__ |
146 | |
147 | =pod |
148 | |
149 | =head1 NAME |
150 | |
151 | Moose::Website - A Moosey solution to this problem |
152 | |
153 | =head1 SYNOPSIS |
154 | |
155 | use Moose::Website; |
156 | |
157 | =head1 DESCRIPTION |
158 | |
159 | =head1 METHODS |
160 | |
161 | =over 4 |
162 | |
163 | =item B<> |
164 | |
165 | =back |
166 | |
167 | =head1 BUGS |
168 | |
169 | All complex software has bugs lurking in it, and this module is no |
170 | exception. If you find a bug please either email me, or add the bug |
171 | to cpan-RT. |
172 | |
173 | =head1 AUTHOR |
174 | |
175 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
176 | |
177 | =head1 COPYRIGHT AND LICENSE |
178 | |
179 | Copyright 2010 Infinity Interactive, Inc. |
180 | |
181 | L<http://www.iinteractive.com> |
182 | |
183 | This library is free software; you can redistribute it and/or modify |
184 | it under the same terms as Perl itself. |
185 | |
186 | =cut |