File Coverage

blib/lib/DDG/Publisher/File.pm
Criterion Covered Total %
statement 9 43 20.9
branch 0 20 0.0
condition 0 3 0.0
subroutine 3 9 33.3
pod 0 3 0.0
total 12 78 15.3


line stmt bran cond sub pod time code
1             package DDG::Publisher::File;
2             # ABSTRACT: A file inside the publisher
3             $DDG::Publisher::File::VERSION = '1043';
4 1     1   5 use MooX;
  1         2  
  1         5  
5 1     1   978 use Locale::Simple;
  1         29423  
  1         78  
6 1     1   500 use File::ShareDir ':ALL';
  1         5042  
  1         811  
7              
8             sub fullpath {
9 0     0 0   my ( $self ) = @_;
10 0           my $fullpath = join('/',$self->dir->web_path,$self->file);
11 0           $fullpath =~ s!/+!/!g;
12 0           return $fullpath;
13             }
14              
15              
16             has dir => (
17             is => 'ro',
18             required => 1,
19             );
20              
21              
22             has static => (
23             is => 'ro',
24             lazy => 1,
25             default => sub { 0 },
26             );
27              
28              
29             has file => (
30             is => 'ro',
31             required => 1,
32             lazy => 1,
33             builder => 1,
34             );
35              
36             sub _build_file {
37 0     0     my ( $self ) = @_;
38 0 0         return $self->static
39             ? $self->filebase.'.html'
40             : $self->filebase.'/'.$self->locale.'.html'
41             }
42              
43              
44             has filebase => (
45             is => 'ro',
46             required => 1,
47             );
48              
49              
50             has locale => (
51             is => 'ro',
52             required => 1,
53             );
54              
55              
56             has code => (
57             is => 'ro',
58             required => 1,
59             );
60              
61             sub url {
62 0     0 0   my ( $self ) = @_;
63 0 0         return $self->dir->path.$self->file if $self->static;
64 0 0         return $self->dir->path if $self->filebase eq 'index';
65 0           return $self->dir->path.$self->filebase;
66             }
67              
68              
69             has template => (
70             is => 'ro',
71             lazy => 1,
72             builder => 1,
73             );
74              
75             sub _build_template {
76 0     0     my ( $self ) = @_;
77 0           my $template = $self->dir->template_path.'/'.$self->filebase;
78 0           $template =~ s!/+!/!g;
79 0           $template .= '.tx';
80 0           return $template;
81             }
82              
83              
84             has content => (
85             is => 'ro',
86             lazy => 1,
87             builder => 1,
88             );
89 0     0 0   sub uncached_content { shift->_build_content }
90              
91             sub _build_content {
92 0     0     my ( $self ) = @_;
93              
94             #
95             # setting locale for the localilzation (see L<Locale::Simple>)
96             #
97 0           l_dir(dist_dir($self->dir->site->locale_dist));
98 0           ltd($self->dir->site->locale_domain);
99 0           l_lang($self->locale);
100              
101             #
102             # Activating the dryrun if requested.
103             #
104 0 0         l_dry($self->dir->site->publisher->dryrun) if $self->dir->site->publisher->has_dryrun;
105              
106             #
107             # Variables for the template
108             #
109             # f = reference to the file itself
110             # d = reference to the directory of the file
111             # s = reference to the site of directory of the file
112             #
113             # locale_package_version = Version number of the locale package
114             # locales = the hash of the locales for this token domain
115             # maintemplate = name of the template (so that base.tx can use it)
116             # url = the final URL for this specific page
117             #
118              
119 0           my %vars = (
120             f => $self,
121             d => $self->dir,
122             s => $self->dir->site,
123             locale_package_version => $self->dir->site->locale_package->version,
124             locales => $self->dir->site->locale_package->locales,
125             maintemplate => $self->template,
126             url => $self->url,
127             );
128              
129 0           my $site_code = $self->dir->site->can('code');
130              
131 0           my $dir_code = $self->dir->can('code');
132              
133             #
134             # Attach ENV variables, doing at here, so that they can be overriden
135             #
136 0           %vars = ( %vars, 'ENV', \%ENV );
137             #
138             # Execute code from L<DDG::Publisher::SiteRole/code> to get more variables
139             #
140 0 0         %vars = ( %vars, $site_code->($self,\%vars) ) if $site_code;
141             #
142             # Execute code from L<DDG::Publisher::DirRole/code> to get more variables
143             #
144 0 0         %vars = ( %vars, $dir_code->($self,\%vars) ) if $dir_code;
145             #
146             # Execute code from L<code> to get more variables
147             #
148 0           %vars = ( %vars, $self->code->($self,\%vars) );
149              
150             # explicit getting out no_base for template decision later
151 0   0       my $no_base = defined $vars{no_base} && $vars{no_base};
152              
153             # use Data::Dumper;
154             # warn Dumper($vars{'ENV'});
155              
156             #
157             # Gathering the save data for the data files generation
158             #
159 0 0         $self->dir->site->save_data->{locales} = $self->dir->site->locale_package->locales
160             unless defined $self->dir->site->save_data->{locales};
161 0 0         $self->dir->site->save_data->{$self->dir->path} = {}
162             unless defined $self->dir->site->save_data->{$self->dir->path};
163 0 0         $self->dir->site->save_data->{$self->dir->path}->{$self->filebase} = { static => $self->static }
164             unless defined $self->dir->site->save_data->{$self->dir->path}->{$self->filebase};
165 0           $self->dir->site->save_data->{$self->dir->path}->{$self->filebase}->{$self->file} = {
166             locale => $self->locale,
167             url => $self->url,
168             file => $self->file,
169             dir => $self->dir->path,
170             template => $self->template,
171             no_base => $no_base,
172             };
173              
174             # execute template, return rendered content.
175 0 0         return $self->dir->site->template_engine->render($no_base ? $self->template : 'base.tx',\%vars);
176             }
177              
178             1;
179              
180             __END__
181              
182             =pod
183              
184             =head1 NAME
185              
186             DDG::Publisher::File - A file inside the publisher
187              
188             =head1 VERSION
189              
190             version 1043
191              
192             =head1 ATTRIBUTES
193              
194             =head2 dir
195              
196             L<DDG::Publisher::DirRole> object of this file
197              
198             =head2 static
199              
200             This is a static file, so its not generated for every language, only one file.
201              
202             =head2 file
203              
204             Filename inside the directory of the site.
205              
206             =head2 file
207              
208             Base path inside the site directory, must be given on construction.
209              
210             =head2 locale
211              
212             Locale used for this specific file.
213              
214             =head2 code
215              
216             Additional code that needs to be executed for getting the variables for the
217             template of this file.
218              
219             =head2 file
220              
221             Template name used, normally by default this is not used, instead B<base.tx>
222             template is loaded by default, and this one is using the template variable. If
223             there is a variable B<no_base> set inside the resulting variables for this
224             file, then this template name is used instead of B<base.tx>
225              
226             =head2 content
227              
228             The resulting (uncompressed) content of the file. Fetching this will
229             automatically fire up the template engine to generate the content.
230              
231             =head1 AUTHOR
232              
233             Torsten Raudssus <torsten@raudss.us>
234              
235             =head1 COPYRIGHT AND LICENSE
236              
237             This software is Copyright (c) 2012 by DuckDuckGo, Inc. L<http://duckduckgo.com/>.
238              
239             This is free software, licensed under:
240              
241             The Apache License, Version 2.0, January 2004
242              
243             =cut