File Coverage

lib/App/Followme/ConvertPage.pm
Criterion Covered Total %
statement 75 75 100.0
branch 7 12 58.3
condition 2 6 33.3
subroutine 14 14 100.0
pod 2 7 28.5
total 100 114 87.7


line stmt bran cond sub pod time code
1             package App::Followme::ConvertPage;
2 2     2   560 use 5.008005;
  2         15  
3 2     2   11 use strict;
  2         3  
  2         54  
4 2     2   10 use warnings;
  2         4  
  2         54  
5              
6 2     2   21 use lib '../..';
  2         6  
  2         10  
7              
8 2     2   291 use base qw(App::Followme::Module);
  2         4  
  2         571  
9              
10 2     2   14 use File::Spec::Functions qw(abs2rel rel2abs catfile splitdir);
  2         4  
  2         108  
11 2     2   11 use App::Followme::FIO;
  2         4  
  2         1917  
12              
13             our $VERSION = "2.03";
14              
15             #----------------------------------------------------------------------
16             # Read the default parameter values
17              
18             sub parameters {
19 24     24 1 53 my ($pkg) = @_;
20              
21             return (
22 24         83 template_file => 'convert_page.htm',
23             data_pkg => 'App::Followme::TextData',
24             );
25             }
26              
27             #----------------------------------------------------------------------
28             # Convert files to html
29              
30             sub run {
31 5     5 0 22 my ($self, $folder) = @_;
32              
33 5         23 $self->update_folder($folder);
34 5         117 return;
35             }
36              
37             #----------------------------------------------------------------------
38             # Set the date format to iso date format, overriding user
39              
40             sub setup {
41 6     6 1 18 my ($self) = @_;
42              
43 6         16 $self->{data}{date_format} = 'yyyy-mm-ddThh:mm:ss';
44 6         15 return;
45             }
46              
47             #----------------------------------------------------------------------
48             # Construct a filename that represents the title
49              
50             sub title_to_filename {
51 13     13 0 92 my ($self, $filename) = @_;
52              
53 13         37 my ($dir, $base) = fio_split_filename($filename);
54 13 100       71 return $filename if $base =~ /^index\./;
55              
56 9         27 my @parts = split(/\./, $base);
57 9         22 my $ext = pop(@parts);
58              
59 9         15 my $new_filename = ${$self->{data}->build('title', $filename)};
  9         45  
60 9 50       30 return $filename unless $new_filename;
61              
62 9         23 $new_filename = lc($new_filename);
63 9         26 $new_filename =~ s/[^\w\-\_]+/ /g;
64              
65 9         20 $new_filename =~ s/^ +//;
66 9         18 $new_filename =~ s/ +$//;
67 9         16 $new_filename =~ s/ +/\-/g;
68              
69 9         54 $new_filename = catfile($dir, join('.', $new_filename, $ext));
70              
71 9         40 return $new_filename;
72             }
73              
74             #----------------------------------------------------------------------
75             # Convert a single file
76              
77             sub update_file {
78 12     12 0 860 my ($self, $folder, $prototype, $file) = @_;
79              
80 12         57 my $new_file = $self->{data}->convert_filename($file);
81 12         55 my $page = $self->render_file($self->{template_file}, $file);
82 12         77 $page = $self->reformat_file($prototype, $new_file, $page);
83              
84 12         53 $self->write_file($new_file, $page);
85 12         37 return;
86             }
87              
88             #----------------------------------------------------------------------
89             # Find files in directory to convert and do that
90              
91             sub update_folder {
92 9     9 0 720 my ($self, $folder) = @_;
93              
94 9         43 my $index_file = $self->to_file($folder);
95 9         63 my $source_folder = $self->{data}->convert_source_directory($folder);
96 9 50       24 return unless $source_folder;
97              
98             my $same_directory = fio_same_file($folder, $source_folder,
99 9         45 $self->{case_sensitivity});
100            
101 9         36 $index_file = $self->to_file($source_folder);
102 9         65 my $files = $self->{data}->build('files', $index_file);
103              
104 9         17 my $prototype;
105 9         25 foreach my $file (@$files) {
106 11   33     94 my $prototype ||= $self->find_prototype($folder, 0);
107 11         29 eval {$self->update_file($folder, $prototype, $file)};
  11         55  
108 11 50       64 if ($self->check_error($@, $file)) {
109 11 50       707 unlink($file) if $same_directory;
110             }
111             }
112              
113 9         64 my $folders = $self->{data}->build('folders', $index_file);
114              
115 9         30 foreach my $subfolder (@$folders) {
116 3         13 $self->update_folder($subfolder);
117             }
118              
119 9         43 return;
120             }
121              
122             #----------------------------------------------------------------------
123             # Write a file, setting folder level metadata
124              
125             sub write_file {
126 12     12 0 46 my ($self, $filename, $page, $binmode) = @_;
127              
128 12         20 my $time = ${$self->{data}->build('mdate', $filename)};
  12         46  
129 12         53 my $new_filename = $self->title_to_filename($filename);
130              
131 12         54 fio_write_page($new_filename, $page, $binmode);
132              
133             unlink($filename) if -e $filename &&
134 12 50 33     190 ! fio_same_file($filename, $new_filename, $self->{case_sensitivity});
135              
136 12         39 fio_set_date($new_filename, $time);
137              
138 12         41 return;
139             }
140              
141             1;
142             __END__
143              
144             =encoding utf-8
145              
146             =head1 NAME
147              
148             App::Followme::ConvertPage - Convert text files to html
149              
150             =head1 SYNOPSIS
151              
152             use App::Followme::ConvertPage;
153             my $converter = App::Followme::ConvertPage->new($configuration);
154             $converter->run($folder);
155              
156             =head1 DESCRIPTION
157              
158             This module converts text files into web files by substituting the content into
159             a template. The type of file converted is determined by the value of the
160             parameter data_pkg. By default, it converts text files using the methods in
161             App::Followme::TextData. After the conversion the original file is deleted.
162              
163             Along with the content, other variables are calculated from the file name and
164             modification date. Variables in the template are preceded by a sigil, most
165             usually a dollar sign. Thus a link would look like:
166              
167             <li><a href="$url">$title</a></li>
168              
169             =head1 CONFIGURATION
170              
171             The following parameters are used from the configuration:
172              
173             =over 4
174              
175             =item template_file
176              
177             The name of the template file. The template file is either in the current
178             directory, in the same directory as the configuration file used to invoke this
179             method, or if not there, in the _templates subdirectory of the top directory.
180             The default value is 'convert_page.htm'.
181              
182             =item data_pkg
183              
184             The name of the module that parses and retrieves data from the text file. The
185             default value is 'App::Followme::TextData', which by default parses
186             Markdown files.
187              
188             =back
189              
190             =head1 LICENSE
191              
192             Copyright (C) Bernie Simon.
193              
194             This library is free software; you can redistribute it and/or modify
195             it under the same terms as Perl itself.
196              
197             =head1 AUTHOR
198              
199             Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
200              
201             =cut