lib/Blio/Node.pm

filter: Statementstmt 55.9% 199 / 356 Branchbran 34.0% 45 / 132 Conditioncond 9.8% 6 / 61 MC/DCmcdc 5.2% 2 / 38 Subroutinesub 71.7% 33 / 46 Podpod 0.0% 0 / 21 totaltotal 43.5% 285 / 654 CC 97 SCAR 39.7
CC
97
Cov
46%
write_feed
240.0
write
83.9
prev_next_post
72.0
CRAP 53.0
↑ Summary lib/Blio.pm »
1 package Blio::Node;
2  
3 # ABSTRACT: A Blio Node
4  
5 18 our $VERSION = '2.009'; # VERSION
6  
7 18 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Markup::Unified;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Blio::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use XML::Atom::SimpleFeed;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use DateTime::Format::RFC3339;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 18 class_type 'DateTime';
21 2 ▶coerce 'DateTime' => from 'Int' => via {
Subroutine __ANON__: called CC=1 SCAR=0.4
22 2 my $d = DateTime->from_epoch( epoch => $_ );
23 2 $d->set_time_zone('local');
24 0 return $d;
25 21 ▶} => from 'Str' => via { DateTime::Format::ISO8601->parse_datetime($_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 18 has 'base_dir' => ( is => 'ro', isa => 'Path::Class::Dir', required => 1 );
28 18 has 'source_file' =>
29 ( is => 'ro', isa => 'Path::Class::File', required => 1, coerce => 1 );
30 18 has 'id' => (is => 'ro', isa=>'Str', required=>1, lazy_build=>1);
31 34 ▶sub _build_id {
Subroutine _build_id: called CC=1 SCAR=0.0
32 34 my $self = shift;
33 34 my $path = $self->source_file->relative($self->base_dir)->stringify;
34 34 $path=~s/\.txt$//;
35 34 return $path;
36 }
37 18 has 'url' => ( is => 'ro', isa => 'Str', lazy_build => 1 );
38 34 ▶sub _build_url {
Subroutine _build_url: called CC=1 SCAR=0.0
39 34 my $self = shift;
40 34 return $self->id.'.html';
41 }
42  
43 18 has 'template' => (is=>'rw',isa=>'Str',required=>1,default=>'node.tt');
44 18 has 'title' => ( is => 'ro', isa => 'Str', required => 1 );
45 18 has 'date' => (
46 is => 'rw',
47 isa => 'DateTime',
48 required => 1,
49 lazy_build => 1,
50 coerce => 1
51 );
52 2 ▶sub _build_date {
Subroutine _build_date: called CC=1 SCAR=0.0
53 2 my $self = shift;
54 2 my $stat = $self->source_file->stat;
55 2 return $stat->mtime;
56 }
57  
58 18 has 'language' => (is=>'ro', isa=>'Maybe[Str]');
59 18 has 'converter' => (is=>'ro', isa=>'Maybe[Str]');
60 18 has 'feed' => (is=>'rw',isa=>'Bool',default=>0);
61 18 has 'list' => (is=>'ro',isa=>'Bool',default=>0);
62 18 has 'author' => (is=>'ro',isa=>'Str');
63 18 has 'paged_list' => (is=>'ro',isa=>'Int',default=>0);
64 18 has 'list_image' => (is=>'ro',isa=>'Str');
65 18 has 'list_only' => (is=>'ro',isa=>'Bool',default=>0);
66  
67 18 has 'raw_content' => ( is => 'rw', isa => 'Str' );
68 18 has 'content' => ( is => 'rw', isa => 'Str', lazy_build=>1 );
69 8 ▶sub _build_content {
Subroutine _build_content: called CC=6 SCAR=18.3
70 8 my $self = shift;
71 8 my $converter = $self->converter;
72 8 my $raw_content = $self->raw_content;
73 8 ▶ return $raw_content unless $converter;
Branch: 1 branch
BranchTrueFalseTotal
unless $converter 6 2 8
74  
75 2 ▶ if ($self->inline_images) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->inline_images) 1 1 2
76 1 $raw_content=~s/<bliothumb:(.*?)>/$self->call_on_image_by_name($1,'thumbnail')/ge;
77 1 $raw_content=~s/<blioimg:(.*?)>/$self->call_on_image_by_name($1,'url')/ge;
78  
79 0 $raw_content=~s/<bliothumb#(\d+)>/$self->call_on_image_by_index($1,'thumbnail')/ge;
80 0 $raw_content=~s/<blioimg#(\d+)>/$self->call_on_image_by_index($1,'url')/ge;
81 }
82  
83 2 ▶ if ($converter eq 'html') {
Branch: 2 branches
BranchTrueFalseTotal
if ($converter eq 'html') { } 0 2 2
elsif (&any(sub { $converter eq $_; } , 'textile', 'markdown', 'bbcode')) { } 1 1 2
84 0 return $raw_content;
85 }
86 4 ▶ elsif (any { $converter eq $_ } qw(textile markdown bbcode)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 my $o = Markup::Unified->new();
88 1 return $o->format($raw_content, $converter)->formatted;
89 }
90 else {
91 1 my $method = 'convert_'.$converter;
92 1 ▶ if ($self->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 0 1 1
93 0 return $self->$method($raw_content);
94 }
95 else {
96 1 return "<pre>No such converter: $converter</pre>".$raw_content;
97 }
98 }
99 }
100 has 'tags' => (
101 is => 'rw',
102 isa => 'ArrayRef',
103 46 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 18 traits => ['Array'],
105 handles => {
106 has_tags => 'count',
107 },
108 );
109 has 'images' => (
110 is => 'rw',
111 isa => 'ArrayRef[Blio::Image]',
112 46 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 18 traits => ['Array'],
114 handles => {
115 has_images => 'count',
116 add_image => 'push',
117 },
118 );
119 18 has 'inline_images' => (is=>'ro',isa=>'Bool',default=>0);
120 18 has 'thumbnail' => (is=>'ro',isa=>'Int');
121  
122 has 'children' => (
123 is => 'rw',
124 isa => 'ArrayRef[Blio::Node]',
125 46 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
126 18 traits => ['Array'],
127 handles => {
128 has_children => 'count',
129 add_child => 'push',
130 },
131  
132 );
133 18 has 'parent' => ( is => 'rw', isa => 'Maybe[Blio::Node]', weak_ref => 1);
134 7 ▶has 'stash' => (is=>'ro',isa=>'HashRef',default=>sub {{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
135 18 has 'feed_url' => (is=>'ro',isa=>'Str',lazy_build=>1);
136 0 ▶sub _build_feed_url {
Subroutine _build_feed_url: not called CC=1 SCAR=6.9
137 0 my $self = shift;
138 0 return $self->id.'.xml';
139 }
140  
141 0 ▶sub is_list {
Subroutine is_list: not called CC=2 SCAR=17.9
Pod undocumented
142 0 my $self = shift;
143 0 ▶ return $self->list || $self->feed;
Condition0%0/3
$self->list || $self->feed
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->list || $self->feed
$self->list $self->feed
Truth table0%0/3
A || B
A= $self->listB= $self->feed
ABresult
1X1
011
000
144 }
145  
146 36 ▶sub new_from_file {
Subroutine new_from_file: called CC=8 SCAR=20.8
Pod undocumented
147 36 my ( $class, $blio, $file ) = @_;
148 36 my @lines = $file->slurp(
149 chomp => 1,
150 iomode => '<:encoding(UTF-8)',
151 );
152 36 my ( $header, $raw_content ) = $class->parse(@lines);
153 36 my $tags = delete $header->{tags};
154 36 my $node = $class->new(
155 base_dir => $blio->source_dir,
156 language => $blio->language,
157 converter => $blio->converter,
158 source_file => $file,
159 %$header,
160 raw_content => $raw_content,
161 stash=>$header,
162 );
163  
164 36 ▶ $node->register_tags($blio, $tags) if $tags && $blio->tags;
Branch: 1 branch
BranchTrueFalseTotal
if $tags and $blio->tags 2 34 36
Condition100%3/3
$tags and $blio->tags
!ll&&!rl&&r
2772
MC/DC100%2/2
$tags and $blio->tags
$tags $blio->tags
Truth table100%3/3
A and B
A= $tagsB= $blio->tags
ABresult
0X0
100
111
165  
166 # check and add single image
167 36 foreach my $ext (qw(jpg jpeg png)) {
168 108 my $single_image = $file->basename;
169 108 $single_image =~ s/\.txt$/.$ext/i;
170 108 my $single_image_file = $file->parent->file($single_image);
171 108 ▶ if (-e $single_image_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $single_image_file) 0 108 108
172 0 my $img = Blio::Image->new(
173 base_dir => $blio->source_dir,
174 source_file => $single_image_file,
175 );
176 0 $node->add_image($img);
177 }
178 }
179  
180 # check and add images dir
181 36 my $img_dir = $file->basename;
182 36 $img_dir=~s/\.txt$//;
183 36 $img_dir = $file->parent->subdir($img_dir.'_images');
184 36 ▶ if (-d $img_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $img_dir) 5 31 36
185 5 ▶ while (my $image_file = $img_dir->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $image_file = $img_dir->next) 18 5 23
186 18 ▶ next unless $image_file =~ /\.(jpe?g|png)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $image_file =~ /\.(jpe?g|png)$/iu 10 8 18
187 8 my $img = Blio::Image->new(
188 base_dir => $blio->source_dir,
189 source_file => $image_file,
190 );
191 8 $node->add_image($img);
192 }
193 }
194  
195 36 return $node;
196 }
197  
198 38 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
Pod undocumented
199 38 my ( $class, @lines ) = @_;
200 38 my %header;
201 38 ▶ while ( my $line = shift(@lines) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @lines) 86 37 123
202 86 ▶ last if $line =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s+$/u 1 85 86
203 85 ▶ last unless $line =~ /:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /:/u 0 85 85
204 85 chomp($line);
205 85 $line=~s/\s+$//;
206 85 my ( $key, $value ) = split( /\s*:\s*/, $line, 2 );
207 85 $header{ lc($key) } = $value;
208 }
209 38 my $content = join( "\n", @lines );
210 38 return \%header, $content;
211 }
212  
213 9 ▶sub write {
Subroutine write: called CC=19 SCAR=44.3
Pod undocumented
214 9 my ($self, $blio, $utime) = @_;
215 9 ▶ return if $self->list_only;
Branch: 1 branch
BranchTrueFalseTotal
if $self->list_only 0 9 9
216  
217 9 my $tt = $blio->tt;
218 9 my $outfile = $blio->output_dir->file($self->url);
219 9 ▶ $outfile->parent->mkpath unless (-d $outfile->parent);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $outfile->parent 3 6 9
220  
221 9 ▶ $tt->process($self->template,
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($self->template, {"node", $self, "blio", $blio, "base", $self->relative_root}, $outfile->relative($blio->output_dir)->stringify, "binmode", ":utf8") or die $tt->error 8 1 9
222 {
223 node=>$self,
224 blio=>$blio,
225 base=>$self->relative_root,
226 },
227 ,$outfile->relative($blio->output_dir)->stringify,
228 binmode => ':utf8',
229 ) || die $tt->error;
230  
231 8 ▶ my $utime_ep = $utime ? $utime->epoch : $self->date->epoch;
Branch: 1 branch
BranchTrueFalseTotal
$utime ? : 0 8 8
232 7 ▶ if ($self->has_children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_children) 2 5 7
233 2 my $children = $self->sorted_children;
234 2 my $child_utime = $children->[0]->date->epoch;
235 2 ▶ $utime_ep = $child_utime if $child_utime > $utime_ep;
Branch: 1 branch
BranchTrueFalseTotal
if $child_utime > $utime_ep 1 1 2
236 }
237 7 utime($utime_ep,$utime_ep,$outfile->stringify);
238  
239 7 ▶ if ($self->has_images && $self->template ne 'image.tt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_images and $self->template ne "image.tt") 0 7 7
Condition33%1/3
$self->has_images and $self->template ne "image.tt"
!ll&&!rl&&r
700
MC/DC0%0/2
$self->has_images and $self->template ne "image.tt"
$self->has_images $self->template ne "image.tt"
Truth table33%1/3
A and B
A= $self->has_imagesB= $self->template ne "image.tt"
ABresult
0X0
100
111
240 0 foreach my $img (@{$self->sorted_images}) {
241 0 ▶ if ($blio->force || !-e $blio->output_dir->file($img->thumbnail)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blio->force or not -e $blio->output_dir->file($img->thumbnail)) 0 0 0
Condition0%0/3
$blio->force or not -e $blio->output_dir->file($img->thumbnail)
l!l&&r!l&&!r
000
MC/DC0%0/2
$blio->force or not -e $blio->output_dir->file($img->thumbnail)
$blio->force not -e $blio->output_dir->file($img->thumbnail)
Truth table0%0/3
A or not(B)
A= $blio->forceB= not -e $blio->output_dir->file($img->thumbnail)
ABresult
1X1
011
000
242 0 ▶ say "\timage ".$img->url unless $blio->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $blio->quiet 0 0 0
243 0 $img->publish($blio);
244 0 $img->make_thumbnail($blio, $self->thumbnail);
245 }
246 }
247 0 ▶ if ( ($self->images->@* > 1) && $blio->images_as_nodes && !$self->inline_images) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->images} > 1 and $blio->images_as_nodes and not $self->inline_images) 0 0 0
Condition0%0/6
@{$self->images} > 1 and $blio->images_as_nodes and not $self->inline_images
!ll&&!rl&&r
000
@{$self->images} > 1 and $blio->images_as_nodes
!ll&&!rl&&r
000
MC/DC0%0/3
@{$self->images} > 1 and $blio->images_as_nodes and not $self->inline_images
@{$self->images} > 1 $blio->images_as_nodes not $self->inline_images
Truth table0%0/4
A and B and not(C)
A= @{$self->images} > 1B= $blio->images_as_nodesC= not $self->inline_images
ABCresult
0XX0
10X0
1100
1111
248 0 ▶ if (defined $self->stash->{images_as_nodes} && $self->stash->{images_as_nodes} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->stash->{'images_as_nodes'} and $self->stash->{'images_as_nodes'} == 0) { } 0 0 0
Condition0%0/3
defined $self->stash->{"images_as_nodes"} and $self->stash->{"images_as_nodes"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->stash->{"images_as_nodes"} and $self->stash->{"images_as_nodes"} == 0
defined $self->stash->{"images_as_nodes"} $self->stash->{"images_as_nodes"} == 0
Truth table0%0/3
A and B
A= defined $self->stash->{"images_as_nodes"}B= $self->stash->{"images_as_nodes"} == 0
ABresult
0X0
100
111
249 # image_as_node disabled in node
250 }
251 else {
252 0 $self->make_images_html($blio);
253 }
254 }
255 }
256  
257 7 ▶ $self->write_feed($blio) if $self->feed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->feed 0 7 7
258 }
259  
260 0 ▶sub write_paged_list {
Subroutine write_paged_list: not called CC=7 SCAR=40.3
Pod undocumented
261 0 my ($self, $blio) = @_;
262  
263 0 my $list = $self->sorted_children;
264 0 my $items_per_page = $self->paged_list;
265 0 my $current_page = 1;
266 0 my $outfile = $blio->output_dir->file($self->url);
267 0 my $utime=0;
268 0 my @page;
269 0 foreach my $i (0 .. $#{$list}) {
270 0 ▶ if ($i>0 && $i % $items_per_page == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $i % $items_per_page == 0) 0 0 0
Condition0%0/3
$i > 0 and $i % $items_per_page == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$i > 0 and $i % $items_per_page == 0
$i > 0 $i % $items_per_page == 0
Truth table0%0/3
A and B
A= $i > 0B= $i % $items_per_page == 0
ABresult
0X0
100
111
271 # write this page
272 0 $self->_write_page($blio, \@page, $current_page, $list, $outfile, $utime, $i);
273  
274 # start new page
275 0 my $utime=0;
276 0 @page=();
277 0 $current_page++;
278 0 $outfile = $blio->output_dir->file($self->id."_".$current_page.'.html');
279 }
280 0 push(@page, $list->[$i]);
281 0 my $this_utime = $list->[$i]->date->epoch;
282 0 ▶ $utime = $this_utime if $this_utime > $utime;
Branch: 1 branch
BranchTrueFalseTotal
if $this_utime > $utime 0 0 0
283 # write last page
284 0 ▶ $self->_write_page($blio, \@page, $current_page, $list, $outfile, $utime) if $i == $#{$list} ;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#{$list;} 0 0 0
285 }
286  
287 0 ▶ $self->write_feed($blio) if $self->feed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->feed 0 0 0
288 }
289  
290 0 ▶sub _write_page {
Subroutine _write_page: not called CC=6 SCAR=37.4
291 0 my ($self, $blio, $page, $current_page, $list, $outfile, $utime, $i ) = @_;
292 0 my $tt = $blio->tt;
293 0 my $data = {
294 node=>$self,
295 page=>$page,
296 blio=>$blio,
297 base=>$self->relative_root,
298 };
299 0 ▶ $data->{prev} = sprintf("%s_%i.html",$self->id, $current_page-1) if $current_page > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $current_page > 1 0 0 0
300 0 ▶ $data->{prev} = $self->url if $current_page==2;
Branch: 1 branch
BranchTrueFalseTotal
if $current_page == 2 0 0 0
301 0 ▶ $data->{next} = sprintf("%s_%i.html",$self->id, $current_page+1) if $i && $list->[$i+1];
Branch: 1 branch
BranchTrueFalseTotal
if $i and $$list[$i + 1] 0 0 0
Condition0%0/3
$i and $$list[$i + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$i and $$list[$i + 1]
$i $$list[$i + 1]
Truth table0%0/3
A and B
A= $iB= $$list[$i + 1]
ABresult
0X0
100
111
302 0 ▶ $tt->process($self->template,
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($self->template, $data, $outfile->relative($blio->output_dir)->stringify, "binmode", ":utf8") or die $tt->error 0 0 0
303 $data,
304 ,$outfile->relative($blio->output_dir)->stringify,
305 binmode => ':utf8',
306 ) || die $tt->error;
307 0 utime($utime,$utime,$outfile->stringify);
308 }
309  
310 17 ▶sub relative_root {
Subroutine relative_root: called CC=2 SCAR=6.9
Pod undocumented
311 17 my $self = shift;
312 17 my $url = $self->url;
313 17 my @level = $url=~m{/}g;
314
315 17 ▶ return '' unless @level;
Branch: 1 branch
BranchTrueFalseTotal
unless @level 6 11 17
316
317 11 return join('/',map { '..' } @level).'/';
318 }
319  
320 17 ▶sub possible_parent_url {
Subroutine possible_parent_url: called CC=1 SCAR=0.0
Pod undocumented
321 17 my $self = shift;
322 17 my $ppurl = $self->url;
323 17 $ppurl =~ s{/[\-\w]+.html$}{.html};
324 17 return $ppurl;
325 }
326  
327 6 ▶sub sorted_children {
Subroutine sorted_children: called CC=4 SCAR=15.2
Pod undocumented
328 6 my ($self, $limit) = @_;
329 my @sorted =
330 12 map { $_->[0] }
331 9 sort { $b->[1] <=> $a->[1] }
332 6 map { [$_ => $_->date->epoch] } @{$self->children};
333 5 ▶ if ($limit && $limit < @sorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $limit < @sorted) 0 5 5
Condition33%1/3
$limit and $limit < @sorted
!ll&&!rl&&r
500
MC/DC0%0/2
$limit and $limit < @sorted
$limit $limit < @sorted
Truth table33%1/3
A and B
A= $limitB= $limit < @sorted
ABresult
0X0
100
111
334 0 @sorted = splice(@sorted,0,$limit);
335 }
336 5 ▶ if ($self->stash->{reverse_children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stash->{'reverse_children'}) { } 0 5 5
337 0 return [reverse @sorted];
338 }
339 else {
340 5 return \@sorted;
341 }
342 }
343  
344 0 ▶sub sort_children_by {
Subroutine sort_children_by: not called CC=3 SCAR=24.8
Pod undocumented
345 0 my ($self, $order_by, $limit) = @_;
346 my @sorted =
347 0 map { $_->[0] }
348 0 sort { $a->[1] cmp $b->[1] }
349 0 map { [$_ => lc($_->$order_by)] } @{$self->children};
350 0 ▶ if ($limit && $limit < @sorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $limit < @sorted) 0 0 0
Condition0%0/3
$limit and $limit < @sorted
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $limit < @sorted
$limit $limit < @sorted
Truth table0%0/3
A and B
A= $limitB= $limit < @sorted
ABresult
0X0
100
111
351 0 @sorted = splice(@sorted,0,$limit);
352 }
353 0 return \@sorted;
354 }
355  
356 1 ▶sub sorted_images {
Subroutine sorted_images: called CC=1 SCAR=0.0
Pod undocumented
357 1 my $self = shift;
358 my @sorted =
359 2 map { $_->[0] }
360 1 sort { $a->[1] cmp $b->[1] }
361 1 map { [$_ => $_->source_file->basename ] } @{$self->images};
362 1 return \@sorted;
363 }
364  
365 0 ▶sub teaser { # TODO enable simple "--fold--" syntax
Subroutine teaser: not called CC=5 SCAR=34.0
Pod undocumented
366 0 my ($self, $length) = @_;
367 0 ▶ return unless $self->raw_content;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw_content 0 0 0
368 0 ▶ $length ||= 200;
Condition0%0/2
$length ||= 200
l!l
00
MC/DC0%0/1
$length ||= 200
$length
Truth table0%0/2
A
A= $length
Aresult
11
00
369 0 my $teaser;
370 0 ▶ if ($length =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length =~ /^\d+$/u) { } 0 0 0
371 0 $teaser = $self->content;
372 0 $teaser =~ s{</?(.*?)>}{}g;
373 0 $teaser = substr($teaser,0,$length);
374 0 $teaser =~s/\s\S+$/ .../;
375 }
376 else {
377 0 my $rest;
378 0 ($teaser, $rest) = split($length, $self->content, 2);
379 0 ▶ unless ($rest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rest) 0 0 0
380 0 $teaser = $self->teaser(200);
381 }
382 }
383 0 return $teaser;
384 }
385  
386 0 ▶sub write_feed {
Subroutine write_feed: not called CC=15 SCAR=54.8
Pod undocumented
387 0 my ($self, $blio) = @_;
388  
389 0 my $site_url = $blio->site_url;
390 0 ▶ die "Cannot generate Atom Feed without site_url, use --site_url to set it" unless $site_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $site_url 0 0 0
391 0 ▶ $site_url .= '/' unless $site_url =~m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $site_url =~ m[/$]u 0 0 0
392  
393 0 my $children = $self->sorted_children(5);
394  
395 0 ▶ return unless @$children;
Branch: 1 branch
BranchTrueFalseTotal
unless @$children 0 0 0
396 0 my $rfc3339 = DateTime::Format::RFC3339->new();
397  
398 0 ▶ my $feed = XML::Atom::SimpleFeed->new(
Condition0%0/5
$self->title || 'no title'
l!l
00
$blio->site_author || $0
l!l&&r!l&&!r
000
MC/DC0%0/1
$self->title || 'no title'
$self->title
MC/DC0%0/2
$blio->site_author || $0
$blio->site_author $0
Truth table0%0/2
A
A= $self->title
Aresult
11
00
Truth table0%0/3
A || B
A= $blio->site_authorB= $0
ABresult
1X1
011
000
399 title=>$self->title || 'no title',
400 author=>$blio->site_author || $0,
401 link=>{
402 href=>$site_url.$self->feed_url,
403 rel=>'self',
404 },
405 id=>$site_url.$self->feed_url,
406 updated=>$rfc3339->format_datetime($children->[0]->date),
407 );
408  
409 0 foreach my $child (@$children) {
410 0 ▶ next unless $child->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $child->parent 0 0 0
411 0 eval {
412 0 ▶ my @entry = (
Condition0%0/4
$child->title || 'no title'
l!l
00
$child->teaser || ' '
l!l
00
MC/DC0%0/1
$child->title || 'no title'
$child->title
MC/DC0%0/1
$child->teaser || ' '
$child->teaser
Truth table0%0/2
A
A= $child->title
Aresult
11
00
Truth table0%0/2
A
A= $child->teaser
Aresult
11
00
413 title=>$child->title || 'no title',
414 link=>$site_url.$child->url,
415 id=>$site_url.$child->url,
416 updated=>$rfc3339->format_datetime($child->date),
417 category=>$child->parent->id,
418 summary=>($child->teaser || ' '),
419 content=>$child->content,
420 );
421 0 ▶ push (@entry,author => $self->author) if $self->author;
Branch: 1 branch
BranchTrueFalseTotal
if $self->author 0 0 0
422 0 ▶ if ($child->has_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->has_tags) 0 0 0
423 0 foreach my $tag (@{$child->tags}) {
424 0 push (@entry, category => $tag->title);
425 }
426 }
427 0 $feed->add_entry( @entry );
428 };
429 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
430 0 say "ERR $@";
431 }
432 }
433 0 my $feed_file = $blio->output_dir->file($self->feed_url);
434 0 ▶ open(my $fh,'>:encoding(UTF-8)',$feed_file->stringify) || die "Cannot write to Atom feed file $feed_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $feed_file->stringify or die "Cannot write to Atom feed file ${feed_file}: $!" 0 0 0
435 0 $feed->print($fh);
436 0 close $fh;
437  
438 0 my $utime = $children->[0]->date->epoch;
439 0 utime($utime,$utime,$feed_file->stringify);
440 }
441  
442 2 ▶sub register_tags {
Subroutine register_tags: called CC=5 SCAR=16.1
Pod undocumented
443 2 my ($self, $blio, $tags ) = @_;
444 2 my @tags = split(/\s*,\s*/,$tags);
445 2 my $tagindex = $blio->tagindex;
446 2 my @tagnodes;
447 2 foreach my $tag (@tags) {
448 4 my $tagid = $tag;
449 4 $tagid=~s/\s/_/g;
450 4 $tagid=~s/\W/_/g;
451 4 my $tagnode = $blio->nodes_by_url->{"tags/$tagid.html"};
452 4 ▶ unless ($tagnode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagnode) 3 1 4
453 3 $tagnode = Blio::Node->new(
454 base_dir => $blio->source_dir,
455 source_file => $0,
456 id=>$tagid,
457 url=>"tags/$tagid.html",
458 title=>$tag,
459 date=>DateTime->new(year=>1980),
460 content=>'',
461 stash=>{
462 is_tag=>1,
463 }
464 );
465 3 $blio->nodes_by_url->{$tagnode->url} = $tagnode;
466 3 $tagnode->parent($tagindex);
467 3 $tagindex->add_child($tagnode);
468 }
469 4 $tagnode->add_child($self);
470 4 ▶ if ($self->date > $tagnode->date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->date > $tagnode->date) 3 1 4
471 3 $tagnode->date($self->date);
472 }
473 4 push(@tagnodes,$tagnode);
474 }
475 2 ▶ $self->tags(\@tagnodes) if @tagnodes;
Branch: 1 branch
BranchTrueFalseTotal
if @tagnodes 2 0 2
476 }
477  
478 4 ▶sub image_by_name {
Subroutine image_by_name: called CC=2 SCAR=7.2
Pod undocumented
479 4 my ($self, $name) = @_;
480 4 my @found = grep { $name eq $_->source_file->basename } @{$self->images};
481 4 ▶ return $found[0] if @found == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found == 1 4 0 4
482 0 return;
483 }
484  
485 4 ▶sub call_on_image_by_name {
Subroutine call_on_image_by_name: called CC=3 SCAR=11.3
Pod undocumented
486 4 my ($self, $name, $method) = @_;
487 4 ▶ $method ||= 'url';
Condition50%1/2
$method ||= "url"
l!l
40
MC/DC0%0/1
$method ||= "url"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
488 4 my $img = $self->image_by_name($name);
489 4 ▶ return "cannnot_resolve_image_$name" unless $img;
Branch: 1 branch
BranchTrueFalseTotal
unless $img 0 4 4
490 4 return $self->relative_root . $img->$method;
491 }
492  
493 0 ▶sub image_by_index {
Subroutine image_by_index: not called CC=1 SCAR=6.9
Pod undocumented
494 0 my ($self, $index) = @_;
495  
496 0 return $self->images->[$index - 1];
497 }
498  
499 0 ▶sub call_on_image_by_index {
Subroutine call_on_image_by_index: not called CC=2 SCAR=17.9
Pod undocumented
500 0 my ($self, $index, $method) = @_;
501 0 my $img = $self->image_by_index($index);
502 0 ▶ return "cannnot_resolve_image_$index" unless $img;
Branch: 1 branch
BranchTrueFalseTotal
unless $img 0 0 0
503 0 return $self->relative_root . $img->$method;
504 }
505  
506 0 ▶sub primary_image {
Subroutine primary_image: not called CC=3 SCAR=24.8
Pod undocumented
507 0 my $self = shift;
508 0 my $img;
509 0 ▶ if ($self->list_image) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_image) 0 0 0
510 0 $img = $self->image_by_name( $self->list_image);
511 }
512 0 ▶ $img ||= $self->sorted_images->[0];
Condition0%0/3
$img ||= $self->sorted_images->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$img ||= $self->sorted_images->[0]
$img $self->sorted_images->[0]
Truth table0%0/3
A ||= B
A= $imgB= $self->sorted_images->[0]
ABresult
1X1
011
000
513 0 return $img;
514 }
515  
516 0 ▶sub prev_next_post {
Subroutine prev_next_post: not called CC=8 SCAR=42.8
Pod undocumented
517 0 my $self = shift;
518 0 ▶ return unless my $p = $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless my $p = $self->parent 0 0 0
519 0 my $siblings = $p->sorted_children;
520 0 my $hit;
521 my $i;
522 0 ▶ for ($i=0;$i<@$siblings;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$siblings) 0 0 0
523 0 my $this = $siblings->[$i];
524 0 ▶ if ($this->id eq $self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->id eq $self->id) 0 0 0
525 0 $hit = $i;
526 0 last;
527 }
528 }
529 0 my %sib;
530 0 ▶ $sib{prev} = $siblings->[$i+1] if $i < $#{$siblings} && $siblings->[$i+1];
Branch: 1 branch
BranchTrueFalseTotal
if $i < $#{$siblings;} and $$siblings[$i + 1] 0 0 0
Condition0%0/3
$i < $#{$siblings;} and $$siblings[$i + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $#{$siblings;} and $$siblings[$i + 1]
$i < $#{$siblings;} $$siblings[$i + 1]
Truth table0%0/3
A and B
A= $i < $#{$siblings;}B= $$siblings[$i + 1]
ABresult
0X0
100
111
531 0 ▶ $sib{next} = $siblings->[$i-1] if ($i-1 >= 0 && $siblings->[$i-1]);
Branch: 1 branch
BranchTrueFalseTotal
if $i - 1 >= 0 and $$siblings[$i - 1] 0 0 0
Condition0%0/3
$i - 1 >= 0 and $$siblings[$i - 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$i - 1 >= 0 and $$siblings[$i - 1]
$i - 1 >= 0 $$siblings[$i - 1]
Truth table0%0/3
A and B
A= $i - 1 >= 0B= $$siblings[$i - 1]
ABresult
0X0
100
111
532 0 return \%sib;
533 }
534  
535 0 ▶sub older_younger {
Subroutine older_younger: not called CC=8 SCAR=42.8
Pod undocumented
536 0 my $self = shift;
537 0 ▶ return unless my $p = $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless my $p = $self->parent 0 0 0
538 0 my $siblings = $p->sorted_children;
539 0 my $hit;
540 my $i;
541 0 ▶ for ($i=0;$i<@$siblings;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$siblings) 0 0 0
542 0 my $this = $siblings->[$i];
543 0 ▶ if ($this->id eq $self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->id eq $self->id) 0 0 0
544 0 $hit = $i;
545 0 last;
546 }
547 }
548 0 my %sib;
549 0 ▶ $sib{older} = $siblings->[$i+1] if $i < $#{$siblings} && $siblings->[$i+1];
Branch: 1 branch
BranchTrueFalseTotal
if $i < $#{$siblings;} and $$siblings[$i + 1] 0 0 0
Condition0%0/3
$i < $#{$siblings;} and $$siblings[$i + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$i < $#{$siblings;} and $$siblings[$i + 1]
$i < $#{$siblings;} $$siblings[$i + 1]
Truth table0%0/3
A and B
A= $i < $#{$siblings;}B= $$siblings[$i + 1]
ABresult
0X0
100
111
550 0 ▶ $sib{younger} = $siblings->[$i-1] if ($i-1 >= 0 && $siblings->[$i-1]);
Branch: 1 branch
BranchTrueFalseTotal
if $i - 1 >= 0 and $$siblings[$i - 1] 0 0 0
Condition0%0/3
$i - 1 >= 0 and $$siblings[$i - 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$i - 1 >= 0 and $$siblings[$i - 1]
$i - 1 >= 0 $$siblings[$i - 1]
Truth table0%0/3
A and B
A= $i - 1 >= 0B= $$siblings[$i - 1]
ABresult
0X0
100
111
551 0 return \%sib;
552 }
553  
554 0 ▶sub make_images_html {
Subroutine make_images_html: not called CC=4 SCAR=30.0
Pod undocumented
555 0 my ($self, $blio) = @_;
556  
557 0 my $old_children = $self->children;
558 0 $self->children([]);
559  
560 0 my @sorted = $self->sorted_images->@*;
561 0 ▶ if ($self->stash->{reverse_children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stash->{"reverse_children"}) 0 0 0
562 0 @sorted = reverse @sorted;
563 }
564 0 my @images;
565  
566 0 foreach my $img (@sorted) {
567 0 my $img_basename = $img->source_file->basename;
568 0 my $image_node = Blio::Node->new(
569 base_dir => $blio->source_dir,
570 source_file => $img->source_file,
571 id=> $self->id.'_images/'.$img_basename,
572 title=>$img_basename,
573 date=>$self->date,
574 content=>$img_basename,
575 stash=>{
576 no_comments=>1,
577 },
578 template=>'image.tt',
579 images => $self->images,
580 );
581 0 $image_node->parent($self);
582 0 $self->add_child($image_node);
583  
584 0 push(@images, $image_node);
585 }
586  
587 0 foreach my $in (@images) {
588 0 $in->write($blio);
589 }
590  
591 0 $self->children($old_children);
592 }
593  
594 18 __PACKAGE__->meta->make_immutable;
595 18 1;
596  
597 =pod
598  
599 =encoding UTF-8
600  
601 =head1 NAME
602  
603 Blio::Node - A Blio Node
604  
605 =head1 VERSION
606  
607 version 2.009
608  
609 =head1 AUTHOR
610  
611 Thomas Klausner <domm@plix.at>
612  
613 =head1 COPYRIGHT AND LICENSE
614  
615 This software is copyright (c) 2013 - 2025 by Thomas Klausner.
616  
617 This is free software; you can redistribute it and/or modify it under
618 the same terms as the Perl 5 programming language system itself.
619  
620 =cut
621  
622 __END__
↑ Summary lib/Blio.pm »