Muster/LeafFile.pm

filter: Statementstmt 27.1% 38 / 140 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 22.8% 8 / 35 Podpod 100% 18 / 18 totaltotal 27.4% 64 / 233 CC 18 SCAR 20.6
CC
18
Cov
22%
build_title
30.0
build_raw
20.0
cooked
12.0
CRAP 7.8
« Muster/PagesHelper.pm ↑ Summary Muster/LeafFile/txt.pm »
1 package Muster::LeafFile;
2 34 $Muster::LeafFile::VERSION = '0.96';
3 #ABSTRACT: Muster::LeafFile - a file in a Muster content tree
4 =head1 NAME
5  
6 Muster::LeafFile - a file in a Muster content tree
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 SYNOPSIS
13  
14 use Muster::LeafFile;
15 my $file = Muster::LeafFile->new(
16 filename => 'foo.md'
17 );
18 my $html = $file->html;
19  
20 =head1 DESCRIPTION
21  
22 File nodes represent files in a content tree.
23  
24 =cut
25  
26 34 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 34 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 34 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 34 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 34 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 34 ▶use Lingua::EN::Titlecase;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 34 ▶use File::MimeInfo::Magic;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 34 has pagename => '';
37 34 has pagesrcname => '';
38 34 has parent_page => '';
39 0 ▶has is_binary => sub { shift->is_this_a_binary };
Subroutine __ANON__: not called CC=1 SCAR=1.2
40 0 ▶has bald_name => sub { shift->build_bald_name };
Subroutine __ANON__: not called CC=1 SCAR=1.2
41 0 ▶has hairy_name => sub { shift->build_hairy_name };
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 0 ▶has title => sub { shift->build_title };
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 0 ▶has filename => sub { croak 'no filename given' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44 0 ▶has filetype => sub { shift->build_filetype };
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 0 ▶has extension => sub { shift->build_ext };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46 0 ▶has pagename => sub { shift->build_pagename };
Subroutine __ANON__: not called CC=1 SCAR=1.2
47 0 ▶has pagesrcname => sub { shift->build_pagesrcname };
Subroutine __ANON__: not called CC=1 SCAR=1.2
48  
49 =head2 raw
50  
51 The raw content.
52  
53 =cut
54 0 ▶sub raw {
Subroutine raw: not called CC=2 SCAR=17.9
Pod documented
55 0 my $self = shift;
56 0 ▶ if (!exists $self->{raw})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"raw"}) 0 0 0
57 {
58 0 $self->{raw} = $self->build_raw();
59 }
60 0 return $self->{raw};
61 }
62  
63 =head2 cooked
64  
65 The "cooked" (processed) content.
66  
67 =cut
68 0 ▶sub cooked {
Subroutine cooked: not called CC=3 SCAR=24.8
Pod documented
69 0 my $self = shift;
70 0 my $new_content = shift;
71 0 ▶ if (defined $new_content)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $new_content) { } 0 0 0
elsif (not exists $self->{'cooked'}) { } 0 0 0
72 {
73 0 $self->{cooked} = $new_content;
74 }
75 elsif (!exists $self->{cooked})
76 {
77 0 $self->{cooked} = $self->raw;
78 }
79 0 return $self->{cooked};
80 }
81  
82 =head2 html
83  
84 HTML generation.
85  
86 =cut
87 0 ▶sub html {
Subroutine html: not called CC=2 SCAR=17.9
Pod documented
88 0 my $self = shift;
89 0 ▶ if (!exists $self->{html})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"html"}) 0 0 0
90 {
91 0 $self->{html} = $self->build_html();
92 }
93 0 return $self->{html};
94 }
95  
96 =head2 meta
97  
98 Get the meta-data from the file.
99  
100 =cut
101 0 ▶sub meta {
Subroutine meta: not called CC=2 SCAR=17.9
Pod documented
102 0 my $self = shift;
103 0 ▶ if (!exists $self->{meta})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"meta"}) 0 0 0
104 {
105 0 $self->{meta} = $self->build_meta();
106 }
107 0 return $self->{meta};
108 }
109  
110 =head2 decache
111  
112 Removed the cached information.
113 I'm not sure we still need to do this?
114  
115 =cut
116 0 ▶sub decache {
Subroutine decache: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self = shift;
118
119 0 delete $self->{raw};
120 0 delete $self->{html};
121 0 delete $self->{meta};
122 }
123  
124 =head2 build_title
125  
126 Build the title for this page.
127  
128 =cut
129 0 ▶sub build_title {
Subroutine build_title: not called CC=5 SCAR=34.0
Pod documented
130 0 my $self = shift;
131  
132 # try to extract title
133 0 ▶ return $self->meta->{title} if exists $self->{meta} and exists $self->meta->{title};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"meta"} and exists $self->meta->{"title"} 0 0 0
Condition0%0/3
exists $self->{"meta"} and exists $self->meta->{"title"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"meta"} and exists $self->meta->{"title"}
exists $self->{"meta"} exists $self->meta->{"title"}
Truth table0%0/3
A and B
A= exists $self->{"meta"}B= exists $self->meta->{"title"}
ABresult
0X0
100
111
134 0 ▶ return $1 if defined $self->html and $self->html =~ m|<h1>(.*?)</h1>|i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->html and $self->html =~ m[<h1>(.*?)</h1>]iu 0 0 0
Condition0%0/3
defined $self->html and $self->html =~ m[<h1>(.*?)</h1>]iu
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->html and $self->html =~ m[<h1>(.*?)</h1>]iu
defined $self->html $self->html =~ m[<h1>(.*?)</h1>]iu
Truth table0%0/3
A and B
A= defined $self->htmlB= $self->html =~ m[<h1>(.*?)</h1>]iu
ABresult
0X0
100
111
135 0 return $self->bald_name;
136 } # build_title
137  
138 =head2 reclassify
139  
140 Reclassify this object as a Muster::LeafFile subtype.
141 If a subtype exists, cast to that subtype and return the object;
142 if not, return self.
143 To simplify things, filetypes are determined by the file extension,
144 and the object name will be Muster::LeafFile::$filetype
145  
146 =cut
147  
148 0 ▶sub reclassify {
Subroutine reclassify: not called CC=2 SCAR=17.9
Pod documented
149 0 my $self = shift;
150  
151 0 my $filetype = $self->filetype;
152 0 ▶ if ($filetype)
Branch: 1 branch
BranchTrueFalseTotal
if ($filetype) 0 0 0
153 {
154 0 my $subtype = __PACKAGE__ . "::" . $filetype;
155 0 eval "require $subtype;"; # needs to be quoted because $subtype is a variable
156 0 $subtype->import();
157 0 bless $self, $subtype;
158 # initialize the meta data for this leaf, now that it knows how to parse it
159 0 $self->meta();
160 0 return $self;
161 }
162 0 return $self;
163 }
164  
165 =head2 build_bald_name
166  
167 Build the base name of the related page with no suffix.
168 It is "bald" because the suffix has been chopped off.
169  
170 =cut
171 0 ▶sub build_bald_name {
Subroutine build_bald_name: not called CC=1 SCAR=6.9
Pod documented
172 0 my $self = shift;
173  
174 # get last filename part
175 0 my $base = basename($self->filename);
176  
177 # delete the suffix
178 0 $base =~ s/\.\w+$//;
179  
180 0 return $base;
181 } # build_bald_name
182  
183 =head2 build_hairy_name
184  
185 Build the base name of the related page, including the suffix.
186 It is "hairy" because the suffix has not been chopped off.
187  
188 =cut
189 0 ▶sub build_hairy_name {
Subroutine build_hairy_name: not called CC=1 SCAR=6.9
Pod documented
190 0 my $self = shift;
191  
192 # get last filename part
193 0 my $base = basename($self->filename);
194  
195 0 return $base;
196 } # build_hairy_name
197  
198 =head2 is_this_a_binary
199  
200 If we don't know what it is, assume it is a binary file.
201 Returns undef if the file is NOT a binary file.
202 This is so we can use "IS NULL" tests in SQL for it.
203  
204 =cut
205 0 ▶sub is_this_a_binary {
Subroutine is_this_a_binary: not called CC=1 SCAR=6.9
Pod documented
206 0 my $self = shift;
207 0 return 1;
208 }
209  
210 =head2 build_pagename
211  
212 Create the pagename from the filename.
213  
214 =cut
215 0 ▶sub build_pagename {
Subroutine build_pagename: not called CC=1 SCAR=6.9
Pod documented
216 0 my $self = shift;
217  
218 # build from parent_page, infix slash and name
219 0 return join '/' => grep {$_ ne ''} $self->parent_page, $self->bald_name;
220 }
221  
222 =head2 build_pagesrcname
223  
224 Create the pagesrcname from the filename.
225  
226 =cut
227 0 ▶sub build_pagesrcname {
Subroutine build_pagesrcname: not called CC=1 SCAR=6.9
Pod documented
228 0 my $self = shift;
229  
230 # build from parent_page, infix slash and hairy name
231 0 return join '/' => grep {$_ ne ''} $self->parent_page, $self->hairy_name;
232 }
233  
234 =head2 build_filetype
235  
236 Derive the filetype for this file, if it has a known module for it.
237 Otherwise, the filetype is empty.
238  
239 =cut
240 0 ▶sub build_filetype {
Subroutine build_filetype: not called CC=3 SCAR=24.8
Pod documented
241 0 my $self = shift;
242  
243 0 my $file=$self->filename;
244  
245 # the extension is the filetype only if there exists a Muster::LeafFile::*ext* module for it.
246 0 ▶ if ($file =~ /\.([^.]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.([^.]+)$/u) 0 0 0
247 0 my $pt = $1;
248 0 my $subtype = __PACKAGE__ . "::" . $pt;
249 0 my $has_filetype = eval "require $subtype;"; # needs to be quoted because $subtype is a variable
250 0 ▶ return $pt if $has_filetype;
Branch: 1 branch
BranchTrueFalseTotal
if $has_filetype 0 0 0
251 }
252 0 return '';
253 }
254  
255 =head2 build_ext
256  
257 The file's extension.
258  
259 =cut
260 0 ▶sub build_ext {
Subroutine build_ext: not called CC=2 SCAR=17.9
Pod documented
261 0 my $self = shift;
262  
263 0 my $ext = '';
264 0 ▶ if ($self->filename =~ /\.(\w+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filename =~ /\.(\w+)$/u) 0 0 0
265 {
266 0 $ext = $1;
267 }
268 0 return $ext;
269 }
270  
271 =head2 build_raw
272  
273 The raw content of the page.
274  
275 =cut
276 0 ▶sub build_raw {
Subroutine build_raw: not called CC=4 SCAR=30.0
Pod documented
277 0 my $self = shift;
278  
279 # We only want to read text files;
280 # since there could be all sorts of binary files
281 # that don't have their own LeafFile module,
282 # it behooves us to check before opening it.
283 0 my $fn = $self->filename;
284 0 my $mime_type = mimetype($fn);
285 0 my $content = '';
286 0 ▶ if ($mime_type =~ /text/)
Branch: 1 branch
BranchTrueFalseTotal
if ($mime_type =~ /text/u) 0 0 0
287 {
288 # Open file for decoded reading
289 0 ▶ open my $fh, '<:encoding(UTF-8)', $fn or croak "couldn't open $fn: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $fn or croak("couldn't open ${fn}: $!") 0 0 0
290  
291 # slurp
292 0 $content = do { local $/; <$fh> };
293  
294 # Test if it is really UTF-8
295 # See: https://www.perlmonks.org/?node_id=669902
296 0 ▶ utf8::decode($content)
Branch: 1 branch
BranchTrueFalseTotal
utf8::decode($content) or carp("LeafFile::build_raw INVALID UTF-8 ($mime_type) ", $self->filename) 0 0 0
297 or carp "LeafFile::build_raw INVALID UTF-8 ($mime_type) ", $self->filename;
298 }
299 0 return $content;
300 }
301  
302 =head2 build_meta
303  
304 The meta-data extracted from the file.
305  
306 =cut
307 0 ▶sub build_meta {
Subroutine build_meta: not called CC=2 SCAR=17.9
Pod documented
308 0 my $self = shift;
309  
310 # Unix filesystems do NOT store the file creation time. The default date
311 # of a file is the mtime (the last modification time), so that's what we'll
312 # have to live with.
313  
314 # NOTE: Many people think that the ctime is the creation time, but it is
315 # the "change time", which is the last modification of the inode. This will
316 # either be the same as the mtime, or more recent than the mtime, if the
317 # file permissions have been changed without changing the content. A file
318 # may store its creation-date in its meta-data, but that will depend on the
319 # filetype of that particular file, so we can't deal with it here.
320 # Therefore the most relevant file "date" comes from the mtime.
321  
322 0 my $st = stat($self->filename);
323 0 my $date = strftime('%Y-%m-%d %H:%M', localtime($st->mtime));
324  
325 0 my $grandparent_page = '';
326 0 ▶ if ($self->pagename =~ m{^(.*)/[-\.\w]+/[-\.\w]+$}o)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pagename =~ m[^(.*)/[-\.\w]+/[-\.\w]+$]ou) { } 0 0 0
327 {
328 0 $grandparent_page = $1;
329 }
330 else # top-level page
331 {
332 0 $grandparent_page = '';
333 }
334  
335 # There is always the default information
336 # of pagename, filename etc.
337 0 my $meta = {
338 pagename=>$self->pagename,
339 pagesrcname=>$self->pagesrcname,
340 parent_page=>$self->parent_page,
341 filename=>$self->filename,
342 filetype=>$self->filetype,
343 is_binary=>$self->is_binary,
344 extension=>$self->extension,
345 bald_name=>$self->bald_name,
346 hairy_name=>$self->hairy_name,
347 title=>$self->derive_title,
348 date=>$date,
349 mtime=>$st->mtime,
350 grandparent_page=>$grandparent_page,
351 };
352  
353 0 return $meta;
354 }
355  
356 =head2 derive_title
357  
358 Derive the title without trying to create HTML.
359  
360 =cut
361 0 ▶sub derive_title {
Subroutine derive_title: not called CC=1 SCAR=6.9
Pod documented
362 0 my $self = shift;
363  
364 # get the title from the name of the file
365 0 my $name = $self->bald_name;
366 0 $name =~ s/[_-]/ /g;
367 0 my $tc = Lingua::EN::Titlecase->new($name);
368 0 return $tc->title();
369 }
370  
371 =head2 build_html
372  
373 Create the default HTML for an unknown page.
374  
375 =cut
376 0 ▶sub build_html {
Subroutine build_html: not called CC=1 SCAR=6.9
Pod documented
377 0 my $self = shift;
378
379 0 my $me = $self->pagename();
380 0 my $title = $self->derive_title();
381 0 return <<EOT;
382 <h1>$title</h1>
383 <p>
384 $me
385 </p>
386 EOT
387  
388 }
389  
390 34 1;
391  
392 __END__
« Muster/PagesHelper.pm ↑ Summary Muster/LeafFile/txt.pm »