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