File Coverage

lib/Templer/Site/New.pm
Criterion Covered Total %
statement 43 45 95.5
branch 13 20 65.0
condition 7 9 77.7
subroutine 5 5 100.0
pod 2 2 100.0
total 70 81 86.4


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3            
4             Templer::Site::New - Create a new templer site
5            
6             =cut
7              
8             =head1 SYNOPSIS
9            
10             use strict;
11             use warnings;
12            
13             use Templer::Site::New;
14            
15             my $site = Templer::Site::New->new();
16             $site->create( "/tmp/foo" );
17            
18             =cut
19              
20             =head1 DESCRIPTION
21            
22             This class allows a new C<templer> site to be created on-disk. This
23             involves creating a new input tree, stub configuration file, etc.
24            
25             The content of the new site, and the directory names, are taken from
26             the DATA section of this class.
27            
28             =cut
29              
30             =head1 LICENSE
31            
32             This module is free software; you can redistribute it and/or modify it
33             under the terms of either:
34            
35             a) the GNU General Public License as published by the Free Software
36             Foundation; either version 2, or (at your option) any later version,
37             or
38            
39             b) the Perl "Artistic License".
40            
41             =cut
42              
43             =head1 AUTHOR
44            
45             Steve Kemp <steve@steve.org.uk>
46            
47             =cut
48              
49             =head1 COPYRIGHT AND LICENSE
50            
51             Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>.
52            
53             This library is free software. You can modify and or distribute it under
54             the same terms as Perl itself.
55            
56             =cut
57              
58             =head1 METHODS
59            
60             =cut
61              
62              
63              
64 1     1   28282 use strict;
  1         1  
  1         21  
65 1     1   3 use warnings;
  1         1  
  1         29  
66              
67              
68             package Templer::Site::New;
69              
70 1     1   3 use File::Path qw(mkpath);
  1         1  
  1         361  
71              
72              
73             =head2 new
74            
75             The constructor. No arguments are required/recognized.
76            
77             =cut
78              
79             sub new
80             {
81 1     1 1 328     my $class = shift;
82 1         3     bless {}, $class;
83             }
84              
85              
86             =head2 create
87            
88             Create a new site in the given directory.
89            
90             This method parses and processes the DATA section of this very module,
91             to know which files/directories to create.
92            
93             =cut
94              
95             sub create
96             {
97 1     1 1 2637     my ( $self, $base, $force ) = (@_);
98              
99             #
100             # Forced defaults to false, if not specified.
101             #
102 1 50       4     $force = 0 if ( !defined($force) );
103              
104              
105             #
106             # Files we created
107             #
108 1         2     my $created = 0;
109              
110 1         1     my $name = undef;
111 1         1     my $marker = undef;
112 1         1     my $tmp = undef;
113              
114             #
115             # Process our data-section.
116             #
117 1         4     while ( my $line = <DATA> )
118                 {
119 241         132         chomp($line);
120              
121             #
122             # Making a directory?
123             #
124 241 100 66     543         if ( $line =~ /^mkdir(.*)/ )
    100 100        
125                     {
126 5         8             my $dir = $1;
127 5         19             $dir =~ s/^\s+|\s+$//g;
128 5         9             $dir = $base . "/" . $dir;
129              
130 5 50       46             if ( !-d $dir )
131                         {
132 5         391                 File::Path::mkpath( $dir, { verbose => 0 } );
133                         }
134              
135                     }
136                     elsif ( !$name &&
137                             !$marker &&
138                             ( $line =~ /file\s+([^\s]+)\s+([^\s]+)/ ) )
139                     {
140              
141             #
142             # Writing to a file?
143             #
144 5         10             $name = $1;
145 5         4             $marker = $2;
146 5         10             $tmp = undef;
147              
148                     }
149                     else
150                     {
151              
152             #
153             # If we have a filename to write to, then append to the temporary
154             # contents - unless we've found the EOF marker.
155             #
156 231 100 66     498             if ( $name && $marker )
157                         {
158 221 100       176                 if ( $line eq $marker )
159                             {
160 5         4                     my $create = 1;
161 5 50       67                     if ( -e $base . "/" . $name )
162                                 {
163 0 0       0                         $create = 0 unless ($force);
164                                 }
165              
166 5 50       6                     if ($create)
167                                 {
168 5         7                         my $dst = $base . "/" . $name;
169              
170 5 50       193                         open my $handle, ">:utf8", $dst or
171                                       die "Failed to write to '$dst' - $!";
172 5         19                         print $handle $tmp;
173 5         98                         close($handle);
174              
175 5         14                         $created += 1;
176                                 }
177                                 else
178                                 {
179 0         0                         print "WARNING: Refusing to over-write $base/$name\n";
180                                 }
181              
182 5         5                     $name = undef;
183 5         3                     $marker = undef;
184 5         13                     $tmp = undef;
185                             }
186                             else
187                             {
188 216         346                     $tmp .= $line . "\n";
189                             }
190                         }
191                     }
192              
193                 }
194 1         4     $created;
195             }
196              
197              
198             1;
199              
200              
201             __DATA__
202             mkdir input
203            
204             mkdir output
205            
206             mkdir plugins
207            
208             mkdir layouts
209            
210             mkdir includes
211            
212             file input/robots.txt EOF
213             User-agent: *
214             Crawl-delay: 10
215             Disallow: /cgi-bin
216             Disallow: /stats
217             EOF
218            
219             file input/index.wgn EOF
220             title: Welcome!
221             ----
222             <p>Welcome to my site.</p>
223             EOF
224            
225             file input/about.wgn EOF
226             title: About my site
227             ----
228             <p>This is my site, it was generated by <a href="https://github.com/skx/templer">templer</a>.</p>
229             EOF
230            
231             file layouts/default.layout EOF
232             <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
233             "http://www.w3.org/TR/html4/loose.dtd">
234             <html>
235             <head>
236             <!-- tmpl_if name='title' -->
237             <title><!-- tmpl_var name='title' escape='html' --></title>
238             <!-- tmpl_else -->
239             <title>Untitled Page</title>
240             <!-- /tmpl_if -->
241             </head>
242             <body>
243             <!-- tmpl_var name='content' -->
244             <p>This is site was generated by <a href="https://github.com/skx/templer">templer</a> on <!-- tmpl_var name='date' -->.</p>
245             </body>
246             </html>
247             EOF
248            
249             file templer.cfg EOF
250             ##
251             #
252             # The first section of the configuration file refers to the
253             # input and output paths.
254             #
255             # Templer will process all files matching "*.skx" beneath a
256             # particular directory. That directory is the input directory.
257             #
258             input = ./input/
259             #
260             ##
261            
262            
263            
264             ##
265             #
266             # Within the input directory we'll process files that match
267             # a given suffix.
268             #
269             # By default this is ".skx", so we'll template-expand files
270             # named "index.skx", "about.skx", etc.
271             #
272             suffix = .wgn
273             #
274             ##
275            
276            
277             ##
278             #
279             # By default all pages will be written in HTML.
280             #
281             # If you have the appropriate depedencies installed you can instead
282             # write your input pages in textile/markdown. Just add to the page
283             #
284             # Title: my title
285             # Format: textile
286             # ----
287             # ... your content here ..
288             #
289             # If all pages are going to be setup in one format you may prefer
290             # to change this default
291             #
292             format = html
293             # format = markdown
294             # format = perl
295             # format = textile
296             #
297             ##
298             #
299            
300            
301            
302             ##
303             #
304             # If we're working in-place then files will be expanded where
305             # they are found.
306             #
307             # This means that the following files will be created:
308             #
309             # ./input/index.skx -> input/index.html
310             # ./input/foo/index.skx -> input/foo/index.html
311             # ..
312             #
313             #
314             # in-place = 1
315             #
316             ##
317            
318            
319            
320             ##
321             #
322             # The more common way of working is to produce the output in a separate
323             # directory.
324             #
325             # NOTE: If you specify both "in-place=1" and an output directory the former
326             # will take precedence.
327             #
328             #
329             output = ./output/
330             #
331             ##
332            
333            
334            
335             ##
336             #
337             # When pages are processed a layout-template will be used to expand the content
338             # into.
339             #
340             # Each page may specify its own layout if it so wishes, but generally we'd
341             # expect only one layout to exist.
342             #
343             # Here we specify both the path to the layout directory and the layout to use
344             # if none is specified:
345             #
346             #
347             layout-path = ./layouts/
348             layout = default.layout
349             #
350             ##
351            
352            
353             ##
354             #
355             # When pages are processed a layout-template will be used to expand the content
356             # into.
357             #
358             # Each page may specify its own layout if it so wishes, but generally we'd
359             # expect only one layout to exist.
360             #
361             # Here we specify both the path to the layout directory and the layout to use
362             # if none is specified:
363             #
364             #
365             # layout-path = ./layouts/
366             #
367             # layout = default.layout
368             #
369             ##
370            
371            
372            
373            
374             ##
375             #
376             # Templer supports plugins for expanding variable definitions
377             # inside the input files, or for formating with text systems
378             # like Textile, Markdown, etc.
379             #
380             # There are several plugins included with the system and you
381             # can write your own in perl. Specify the path to load plugins
382             # from here.
383             #
384             plugin-path = ./plugins/
385             #
386             ##
387            
388            
389             ##
390             #
391             # Templer supports including files via the 'read_file' function, along
392             # with the built-in support that HTML::Template has for file inclusion
393             # via:
394             #
395             # <!-- tmpl_include name='file.inc' -->
396             #
397             # In both cases you may specify a search-path for file inclusion
398             # via the include-path setting:
399             #
400             # include-path = include/:include/local/
401             #
402             # Given the choice you should prefer the templer-provided file-inclusion
403             # method over the HTML::Template facility, because this will force pages to
404             # be rebuilt when the included-files are changed.
405             #
406             # Using a HTML::Template include-file you'll need to explicitly force a
407             # rebuild if you modify an included file, but not the parent.
408             #
409             #
410             include-path = ./includes
411             #
412             ##
413            
414            
415            
416            
417             #
418             # Anything below this is a global variable, accessible by name in your
419             # templates.
420             #
421             # For example this:
422             #
423             # copyright = &copy; Steve Kemp 2012
424             #
425             # Can be used in your template, or you page text via:
426             #
427             # <!-- tmpl_var name='copyright' -->
428             #
429             # Similarly you might wish to include a last-modified date in the layout
430             # and this could be achieved by wruting this:
431             #
432             # <p>Page last rebuilt at <!-- tmpl_var name='date' --></p>
433             #
434             # Providing you uncomment this line:
435             #
436             date = run_command( date '+%A %e %B %Y' )
437             #
438             #
439             #
440             ##
441             EOF
442            
443