File Coverage

blib/lib/DDG/Publisher.pm
Criterion Covered Total %
statement 24 72 33.3
branch 0 12 0.0
condition n/a
subroutine 8 14 57.1
pod 1 2 50.0
total 33 100 33.0


line stmt bran cond sub pod time code
1             package DDG::Publisher;
2             # ABSTRACT: Generation of the static files of DuckDuckGo and its microsites.
3             $DDG::Publisher::VERSION = '1043';
4 1     1   26349 use MooX;
  1         9970  
  1         6  
5 1     1   11259 use Path::Class;
  1         35006  
  1         61  
6 1     1   497 use Class::Load ':all';
  1         12660  
  1         157  
7 1     1   485 use IO::All -utf8;
  1         8590  
  1         11  
8 1     1   787 use HTML::Packer;
  1         5992  
  1         28  
9 1     1   462 use String::ProgressBar;
  1         761  
  1         23  
10 1     1   612 use JSON;
  1         8925  
  1         5  
11 1     1   183 use File::Path qw(make_path);
  1         1  
  1         575  
12              
13              
14             has site_classes => (
15             is => 'ro',
16             lazy => 1,
17             builder => 1,
18             );
19              
20 0     0     sub _build_site_classes {[qw(
21             Duckduckgo
22             Donttrackus
23             Dontbubbleus
24             Whatisdnt
25             Fixtracking
26             Duckduckhack
27             )]}
28              
29              
30             has extra_template_dirs => (
31             is => 'ro',
32             lazy => 1,
33             builder => 1,
34             );
35              
36 0     0     sub _build_extra_template_dirs {[qw(
37             templates
38             )]}
39              
40              
41             has cache_dir => (
42             is => 'ro',
43             lazy => 1,
44             builder => 1,
45             );
46              
47              
48             sub _build_cache_dir {
49 0 0   0     my $dir = $ENV{DDG_PUBLISHER_CACHE_DIR} ? $ENV{DDG_PUBLISHER_CACHE_DIR} : $ENV{HOME}."/publisher";
50 0 0         make_path($dir) unless -d $dir;
51 0           return $dir;
52             }
53              
54              
55              
56             has sites => (
57             is => 'ro',
58             lazy => 1,
59             builder => 1,
60             );
61              
62             sub _build_sites {
63 0     0     my ( $self ) = @_;
64 0           return {map {
65 0           my $class = 'DDG::Publisher::Site::'.$_;
66 0           load_class($class);
67 0           s/([a-z])([A-Z])/$1_$2/g;
68 0           $_ = lc($_);
69 0           lc($_) => $class->new( key => lc($_), publisher => $self );
70 0           } @{$self->site_classes}};
71             }
72              
73              
74             has compression => (
75             is => 'ro',
76             default => sub { 0 },
77             );
78              
79              
80             has dryrun => (
81             is => 'ro',
82             predicate => 1,
83             );
84              
85             sub BUILD {
86 0     0 0   my ( $self ) = @_;
87 0           $self->sites;
88             }
89              
90              
91             sub publish_to {
92 0     0 1   my ( $self, $target ) = @_;
93 0           my $target_dir = dir($target)->absolute;
94 0 0         $target_dir->mkpath unless -d "$target_dir";
95 0           my $count = 0;
96 0           my $packer;
97 0 0         $packer = HTML::Packer->init() if ($self->compression);
98              
99             #
100             # For every site...
101             #
102              
103 0           for my $site (values %{$self->sites}) {
  0            
104              
105             #
106             # For every dir in the site...
107             #
108              
109 0           for my $dir (values %{$site->dirs}) {
  0            
110 0           print "\n".(ref $site).$dir->web_path."\n\n";
111 0           my @files = values %{$dir->fullpath_files};
  0            
112 0           my $progress = String::ProgressBar->new(
113             max => scalar @files,
114             length => 50,
115             );
116 0           my $i = 0;
117 0           for (sort { $a->fullpath cmp $b->fullpath } @files) {
  0            
118 0           $i++;
119 0           $progress->update($i);
120 0           $progress->write;
121 0           my $real_file = file($target_dir,$site->key,$_->fullpath)->absolute;
122 0 0         $real_file->dir->mkpath unless -f $real_file->dir->absolute->stringify;
123 0           my $content = $_->content;
124 0           utf8::encode($content);
125              
126             #
127             # If compression is requested, then the content of the files
128             # get compressed via HTML::Packer here.
129             #
130              
131 0 0         if ($packer) {
132 0           $packer->minify(\$content,{
133             remove_comments => 0,
134             remove_newlines => 1,
135             do_javascript => 1,
136             do_stylesheet => 1,
137             no_compress_comments => 1,
138             })
139             }
140 0           utf8::decode($content);
141 0           io($real_file->stringify)->print($content);
142 0           $count++;
143             }
144 0           print "\n";
145             }
146              
147             #
148             # Generate a datafile for the site, which can be used for deeper
149             # processing of the static files. (It's used by the internal code
150             # of DDG to generate, for example, the nginx config)
151             #
152              
153 0           my $data_file = file($target_dir,$site->key.'.json')->absolute;
154 0           io($data_file)->print(encode_json($site->save_data));
155             };
156 0           return $count;
157             }
158              
159             1;
160              
161             __END__
162              
163             =pod
164              
165             =head1 NAME
166              
167             DDG::Publisher - Generation of the static files of DuckDuckGo and its microsites.
168              
169             =head1 VERSION
170              
171             version 1043
172              
173             =head1 ATTRIBUTES
174              
175             =head2 site_classes
176              
177             List of classes that should get executed on publishing.
178              
179             =head2 extra_template_dirs
180              
181             List of extra directions that should be used for templates.
182              
183             =head2 sites
184              
185             This attribute holds the objects of the site classes that should get build.
186              
187             =head2 compression
188              
189             See L<DDG::App::Publisher/compression>.
190              
191             =head2 dryrun
192              
193             See L<DDG::App::Publisher/dryrun>.
194              
195             =head1 METHODS
196              
197             =head2 publish_to
198              
199             This method it called to publish the files to the given specific directory.
200              
201             =head1 AUTHOR
202              
203             Torsten Raudssus <torsten@raudss.us>
204              
205             =head1 COPYRIGHT AND LICENSE
206              
207             This software is Copyright (c) 2012 by DuckDuckGo, Inc. L<http://duckduckgo.com/>.
208              
209             This is free software, licensed under:
210              
211             The Apache License, Version 2.0, January 2004
212              
213             =cut