lib/App/Followme/PodData.pm

filter: Statementstmt 90.6% 135 / 149 Branchbran 50.0% 18 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 24 / 26 Podpod 13.3% 2 / 15 totaltotal 79.2% 179 / 226 CC 25 SCAR 13.4
CC
25
Cov
83%
convert_source_directory
12.0
find_matching_files
5.9
find_base_directory
5.8
CRAP 3.8
« lib/App/Followme/Initialize.pm ↑ Summary lib/App/Followme/Module.pm »
1 package App::Followme::PodData;
2  
3 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw(App::Followme::FileData);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Pod::Simple::XHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec::Functions qw(abs2rel catfile splitdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use App::Followme::Web;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = "2.03";
18  
19 #----------------------------------------------------------------------
20 # Read the default parameter values
21  
22 4 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
23 4 my ($self) = @_;
24  
25 return (
26 4 package => '',
27 pod_directory => '',
28 final_directory => '',
29 extension => 'pm,pod',
30 title_template => '<h2></h2>',
31 );
32 }
33  
34 #----------------------------------------------------------------------
35 # Alter urls in body of pod file to corect final location
36  
37 16 ▶sub alter_url {
Subroutine alter_url: called CC=5 SCAR=16.1
Pod undocumented
38 16 my ($self, $url) = @_;
39  
40 16 my $site_url = $self->get_site_url($url);
41  
42 16 my $package;
43 16 ▶ if ($site_url eq substr($url, 0, length($site_url))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site_url eq substr($url, 0, length $site_url)) { } 15 1 16
44 15 $package = substr($url, length($site_url));
45 } else {
46 1 $package = $url;
47 }
48  
49 16 $package =~ s/^$self->{package}:://;
50 16 my @package_path = split(/::/, $package);
51  
52 16 my $filename = catfile($self->{base_directory}, @package_path);
53  
54 16 my $found;
55 16 foreach my $ext ('pod', 'pm', 'pl') {
56 33 ▶ if (-e "$filename.$ext") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$filename.$ext") 15 18 33
57 15 $filename .= ".$ext";
58 15 $found = 1;
59 15 last;
60 }
61 }
62  
63 16 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 15 1 16
64 $url = $self->filename_to_url($self->{top_directory},
65 $filename,
66 15 $self->{web_extension});
67 } else {
68 1 $url = '';
69 }
70  
71 16 return $url;
72 }
73  
74 #-----------------------------------------------------------------------
75 # Get the name of the web file a file will be converted to
76  
77 16 ▶sub convert_filename {
Subroutine convert_filename: called CC=2 SCAR=7.0
Pod undocumented
78 16 my ($self, $filename) = @_;
79  
80 16 ▶ die "Base directory is undefined" unless $self->{base_directory};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"base_directory"} 0 16 16
81  
82 16 my $new_file = abs2rel($filename, $self->{base_directory});
83 16 $new_file = join('-', splitdir(lc($new_file)));
84  
85 16 $new_file =~ s/\.[^\.]*$/.$self->{web_extension}/;
86 16 $new_file = catfile($self->{final_directory}, $new_file);
87  
88 16 return $new_file;
89 }
90  
91 #-----------------------------------------------------------------------
92 # Get the name of the source directory for ConvertPage
93  
94 0 ▶sub convert_source_directory {
Subroutine convert_source_directory: not called CC=3 SCAR=24.8
Pod undocumented
95 0 my ($self, $directory) = @_;
96  
97 0 ▶ die "Base directory is undefined" unless $self->{base_directory};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"base_directory"} 0 0 0
98  
99 0 my $source_directory;
100 0 ▶ if (fio_same_file($directory, $self->{final_directory},
Branch: 1 branch
BranchTrueFalseTotal
if (fio_same_file($directory, $self->{"final_directory"}, $self->{"case_sensitivity"})) 0 0 0
101 $self->{case_sensitivity})) {
102 0 $source_directory = $self->{base_directory};
103 }
104  
105 0 return $source_directory;
106 }
107  
108 #----------------------------------------------------------------------
109 # Extract the content between body tags in a web page
110  
111 3 ▶sub extract_body {
Subroutine extract_body: called CC=2 SCAR=7.0
Pod undocumented
112 3 my ($self, $html) = @_;
113  
114 3 my @section = split(/<\s*\/?body[^>]*>/i, $html);
115 3 s/^\s+// foreach @section;
116
117 3 my $body = $section[1];
118  
119 0 $body =~ s/src="([^"]*)"/'src="' . $self->alter_url($1) . '"'/ge;
120 3 $body =~ s/href="([^"]*)"/'href="' . $self->alter_url($1) . '"'/ge;
121  
122 3 return $body;
123 }
124  
125 #----------------------------------------------------------------------
126 # Parse content as html to get values for data
127  
128 3 ▶sub fetch_content {
Subroutine fetch_content: called CC=5 SCAR=16.1
Pod undocumented
129 3 my ($self, $content_block) = @_;
130  
131 3 my %content;
132 3 $content{body} = $content_block;
133  
134 3 ▶ if ($self->{title_template}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"title_template"}) 3 0 3
135 28 ▶ my $title_parser = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 28 my $title = web_only_text(@_);
137 28 $title =~ s/\s+/_/g;
138 28 return lc($title);
139 3 };
140  
141 my $section = web_titled_sections($self->{title_template},
142 3 $content_block,
143 $title_parser);
144  
145 3 my %mapping = ('title' => 'name',
146 'summary' => 'description',
147 'author' => 'author',
148 );
149  
150 3 ▶ while (my ($cname, $sname) = each %mapping) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %mapping) 9 3 12
151 9 $content{$cname} = $section->{$sname};
152 }
153  
154 3 foreach my $cname (qw(author title)) {
155 6 my @tokens = web_split_at_tags($content{$cname});
156 6 $content{$cname} = web_only_text(@tokens);
157 }
158 }
159  
160 3 ▶ if ($content{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content{"title"}) 3 0 3
161 3 my @title_parts = split(/\s+-+\s+/, $content{title}, 2);
162 3 $content{title} = $title_parts[0];
163 }
164  
165 3 return %content;
166 }
167  
168 #----------------------------------------------------------------------
169 # Convert Pod into html
170  
171 3 ▶sub fetch_as_html {
Subroutine fetch_as_html: called CC=1 SCAR=0.0
Pod undocumented
172 3 my ($self, $text) = @_;
173  
174 3 my $psx = $self->initialize_parser();
175  
176 3 my $html;
177 3 $psx->output_string(\$html);
178 3 $psx->parse_string_document($text);
179 3 return $self->extract_body($html);
180 }
181  
182 #----------------------------------------------------------------------
183 # Split into text from file into section blocks.
184  
185 3 ▶sub fetch_sections {
Subroutine fetch_sections: called CC=1 SCAR=0.0
Pod undocumented
186 3 my ($self, $text) = @_;
187  
188 3 my %section;
189 3 $section{body} = $self->fetch_as_html($text);
190 3 $section{metadata} = '';
191  
192 3 return \%section;
193 }
194  
195 #----------------------------------------------------------------------
196 # Convert filename to url
197  
198 15 ▶sub filename_to_url {
Subroutine filename_to_url: called CC=1 SCAR=0.0
Pod undocumented
199 15 my ($self, $directory, $filename, $ext) = @_;
200  
201 15 $filename = $self->convert_filename($filename);
202 15 return $self->SUPER::filename_to_url($directory, $filename, $ext);
203 }
204  
205 #----------------------------------------------------------------------
206 # Find the directory containing the pod files
207  
208 2 ▶sub find_base_directory {
Subroutine find_base_directory: called CC=5 SCAR=17.6
Pod undocumented
209 2 my ($self)= @_;
210  
211 2 my @package_path = split(/::/, $self->{package});
212 2 my $package_folder = catfile(@package_path);
213 2 my $package_file = "$package_folder.pm";
214  
215 2 my @folders;
216 push(@folders, split(/\s*,\s*/, $self->{pod_directory}))
217 2 ▶ if $self->{pod_directory};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pod_directory"} 2 0 2
218 2 push(@folders, @INC);
219  
220 2 for my $folder (@folders) {
221 2 ▶ if (-e catfile($folder, $package_file)) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e catfile($folder, $package_file)) { } 2 0 2
elsif (-e catfile($folder, $package_folder)) { } 0 0 0
222 2 pop(@package_path);
223 2 return ($folder, \@package_path);
224  
225 } elsif(-e catfile($folder, $package_folder)) {
226 0 return ($folder, \@package_path);
227 }
228 }
229  
230 0 return;
231 }
232  
233 #----------------------------------------------------------------------
234 # Treat all pod files as if they were in a single directory
235  
236 1 ▶sub find_matching_directories {
Subroutine find_matching_directories: called CC=1 SCAR=0.0
Pod undocumented
237 1 my ($self, $directory) = @_;
238  
239 1 my @directories = ();
240 1 return @directories;
241 }
242  
243 #----------------------------------------------------------------------
244 # Treat all pod files as if they were in a single directory
245  
246 2 ▶sub find_matching_files {
Subroutine find_matching_files: called CC=5 SCAR=17.8
Pod undocumented
247 2 my ($self, $folder) = @_;
248  
249 2 my ($filenames, $folders) = fio_visit($folder);
250  
251 2 my @files;
252 2 foreach my $filename (@$filenames) {
253 56 ▶ push(@files, $filename) if $self->match_file($filename);
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_file($filename) 56 0 56
254 }
255  
256 2 foreach my $folder (@$folders) {
257 0 ▶ push(@files, $self->find_matching_files($folder))
Branch: 1 branch
BranchTrueFalseTotal
if $self->match_directory($folder) 0 0 0
258 if $self->match_directory($folder);
259 }
260  
261 2 return @files;
262 }
263  
264 #-----------------------------------------------------------------------
265 # Treat all pod files as if they were in a single directory
266  
267 0 ▶sub get_folders {
Subroutine get_folders: not called CC=1 SCAR=6.9
Pod undocumented
268 0 my ($self, $filename) = @_;
269  
270 0 my ($directory, $file) = fio_split_filename($filename);
271 0 my @directories = $self->find_matching_directories($directory);
272  
273 0 return \@directories;
274 }
275  
276 #----------------------------------------------------------------------
277 # Initialize the pod parser
278  
279 3 ▶sub initialize_parser {
Subroutine initialize_parser: called CC=4 SCAR=14.3
Pod undocumented
280 3 my ($self) = @_;
281  
282 3 my ($h_level) = $self->{title_template} =~ /(\d+)/;
283 3 ▶ $h_level = 1 unless defined $h_level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h_level 0 3 3
284  
285 3 my $psx = Pod::Simple::XHTML->new();
286  
287 3 ▶ $psx->html_h_level($h_level)
Branch: 1 branch
BranchTrueFalseTotal
if $psx->can("html_h_level") 3 0 3
288 if $psx->can('html_h_level');
289  
290 $psx->perldoc_url_prefix($self->{site_url})
291 3 ▶ if $psx->can('perldoc_url_prefix');
Branch: 1 branch
BranchTrueFalseTotal
if $psx->can("perldoc_url_prefix") 3 0 3
292  
293 3 return $psx;
294 }
295  
296 #----------------------------------------------------------------------
297 # Initialize pod parser and find pod directory
298  
299 1 ▶sub setup {
Subroutine setup: called CC=2 SCAR=7.0
Pod documented
300 1 my ($self) = @_;
301  
302 1 my ($pod_folder, $package_path) = $self->find_base_directory();
303 1 ▶ die "Couldn't find folder for $self->{package}"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod_folder 0 1 1
304 unless defined $pod_folder;
305  
306 1 $self->{final_directory} = $self->{base_directory};
307 1 $self->{base_directory} = catfile($pod_folder, @$package_path);
308 1 $self->{package} = join('::', @$package_path);
309  
310 1 return;
311 }
312  
313 1 1;
314 __END__
« lib/App/Followme/Initialize.pm ↑ Summary lib/App/Followme/Module.pm »