File Coverage

blib/lib/DDG/Publisher/SiteRole.pm
Criterion Covered Total %
statement 24 74 32.4
branch 0 14 0.0
condition 0 3 0.0
subroutine 8 20 40.0
pod 0 2 0.0
total 32 113 28.3


line stmt bran cond sub pod time code
1             package DDG::Publisher::SiteRole;
2             # ABSTRACT: The role for a site in the publisher
3             $DDG::Publisher::SiteRole::VERSION = '1043';
4 1     1   384 use MooX::Role;
  1         1  
  1         5  
5 1     1   555 use Class::Load ':all';
  1         2  
  1         193  
6 1     1   606 use Text::Xslate qw( mark_raw );
  1         7833  
  1         64  
7 1     1   536 use File::ShareDir::ProjectDistDir;
  1         7270  
  1         7  
8 1     1   343 use Locale::Simple;
  1         2  
  1         79  
9 1     1   476 use Config::INI::Reader;
  1         19989  
  1         36  
10 1     1   8 use Path::Class;
  1         1  
  1         57  
11 1     1   438 use JavaScript::Value::Escape;
  1         473  
  1         709  
12              
13             requires qw(
14             default_hostname
15             dirs_classes
16             locale_package
17             locale_domain
18             );
19              
20              
21             has publisher => (
22             is => 'ro',
23             required => 1,
24             );
25              
26              
27             has key => (
28             is => 'ro',
29             required => 1,
30             );
31              
32              
33             has hostname => (
34             is => 'ro',
35             builder => 'default_hostname',
36             lazy => 1,
37             );
38              
39              
40             has dirs => (
41             is => 'ro',
42             builder => 1,
43             lazy => 1,
44             );
45              
46             sub _build_dirs {
47 0     0     my ( $self ) = @_;
48 0   0       return {map {
49 0           my $class = (ref $self || $self).'::'.$_;
50 0           load_class($class);
51 0           s/([a-z])([A-Z])/$1_$2/g;
52 0           $_ = lc($_);
53 0           $_ => $class->new(
54             key => $_,
55             site => $self,
56             );
57             } $self->dirs_classes};
58             }
59              
60              
61             has default_locale => (
62             is => 'ro',
63             builder => 1,
64             lazy => 1,
65             );
66              
67 0     0     sub _build_default_locale { 'en_US' } # DON'T CHANGE
68              
69              
70             has fullpath_files => (
71             is => 'ro',
72             lazy => 1,
73             builder => 1,
74             );
75              
76             sub _build_fullpath_files {
77 0     0     my ( $self ) = @_;
78 0           my %fullpath_files;
79 0           for my $dir (values %{$self->dirs}) {
  0            
80 0           for my $key (keys %{$dir->fullpath_files}) {
  0            
81 0           $fullpath_files{$key} = $dir->fullpath_files->{$key};
82             }
83             }
84 0           return \%fullpath_files;
85             }
86              
87              
88             has save_data => (
89             is => 'rw',
90             lazy => 1,
91             default => sub {{}},
92             );
93              
94             sub load_locale_package {
95 0     0 0   my ( $self ) = @_;
96 0 0         load_class($self->locale_package) unless (is_class_loaded($self->locale_package));
97             }
98              
99             sub locales {
100 0     0 0   my ( $self ) = @_;
101 0           $self->load_locale_package;
102 0           return (keys %{$self->locale_package->locales});
  0            
103             }
104              
105              
106             has template_engine => (
107             is => 'ro',
108             lazy => 1,
109             builder => 1,
110             );
111              
112             sub _build_template_engine {
113 0     0     my ( $self ) = @_;
114 0           my $site_template_root = dir(dist_dir('DDG-Publisher'),'site',$self->key)->stringify;
115 0           my $core_template_root = dir(dist_dir('DDG-Publisher'),'core')->stringify;
116             #
117             # This hash contains the translation functions, wrapped with the
118             # functionality required to make it work proper with utf8 in the context
119             # of Text::Xslate.
120             #
121 0           my %xslate_locale_functions;
122 0           for my $key (keys %{ Locale::Simple->coderef_hash }) {
  0            
123             $xslate_locale_functions{$key} = sub {
124 0     0     my $translation = Locale::Simple->coderef_hash->{$key}->(@_);
125 0           return mark_raw($translation);
126 0           };
127             }
128 0           return Text::Xslate->new(
129             #
130             # Include share/site/$key and share/core as template directories
131             #
132             path => [@{$self->publisher->extra_template_dirs},$site_template_root,$core_template_root],
133             function => {
134             #
135             # js() function to escape js
136             #
137 0     0     js => sub { return mark_raw(javascript_value_escape(join("",@_))) },
138             #
139             # r() function to mark some output as "clean", which means that
140             # it doesnt get HTML encoded by the Xslate generation system.
141             #
142 0     0     r => sub { return mark_raw(join("",@_)) },
143             %xslate_locale_functions,
144             find_template => sub {
145 0     0     my ($filename) = @_;
146 0 0         return $filename if eval { $self->template_engine->find_file($filename); 1 };
  0            
  0            
147 0           $filename .= $self->template_engine->{suffix};
148 0 0         return $filename if eval { $self->template_engine->find_file($filename); 1 };
  0            
  0            
149 0           return 0;
150             },
151             substr => sub {
152 0     0     my($str, $offset, $length) = @_;
153 0 0         return undef unless defined $str;
154 0 0         $offset = 0 unless defined $offset;
155 0 0         $length = length($str) unless defined $length;
156 0           return CORE::substr($str, $offset, $length);
157             },
158             lowercase => sub {
159 0 0   0     return defined($_[0]) ? CORE::lc($_[0]) : undef;
160             },
161             },
162 0           );
163             }
164              
165             1;
166              
167             __END__
168              
169             =pod
170              
171             =head1 NAME
172              
173             DDG::Publisher::SiteRole - The role for a site in the publisher
174              
175             =head1 VERSION
176              
177             version 1043
178              
179             =head1 ATTRIBUTES
180              
181             =head2 publisher
182              
183             L<DDG::Publisher> object, must be given on construction.
184              
185             =head2 key
186              
187             This is the key used for the directory and general identification of the site
188             inside the publisher system.
189              
190             =head2 hostname
191              
192             This is the hostname, which should get used for the final files, so far this
193             option isn't used and has no effect. B<TODO>
194              
195             =head2 dirs
196              
197             This attribute contains the objects of the L<DDG::Publisher::DirRole> objects
198             of this site.
199              
200             =head2 default_locale
201              
202             Default locale to use on this site. Defaults to en_US and should never be
203             changed on a site of DuckDuckGo, as many other parts of the system are also
204             thinking that en_US is the default.
205              
206             =head2 fullpath
207              
208             Migrated fullpath file references for all dirs of the site
209              
210             =head2 save_data
211              
212             This data is used in the end of the publishing process to generate the data
213             file. It will be filled up on the process of executing all files of the site.
214              
215             =head2 template_engine
216              
217             This function holds the L<Text::Xslate> engine for the specific site.
218              
219             =head1 AUTHOR
220              
221             Torsten Raudssus <torsten@raudss.us>
222              
223             =head1 COPYRIGHT AND LICENSE
224              
225             This software is Copyright (c) 2012 by DuckDuckGo, Inc. L<http://duckduckgo.com/>.
226              
227             This is free software, licensed under:
228              
229             The Apache License, Version 2.0, January 2004
230              
231             =cut