File Coverage

lib/App/Followme/ConvertPage.pm
Criterion Covered Total %
statement 76 76 100.0
branch 7 12 58.3
condition 2 6 33.3
subroutine 14 14 100.0
pod 2 7 28.5
total 101 115 87.8


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