HiD/Layout.pm

filter: Statementstmt 100% 79 / 79 Branchbran 71.4% 10 / 14 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 93.2% 111 / 119 CC 10 SCAR 11.1
CC
10
Cov
94%
BUILDARGS
6.1
render
5.0
BEGIN
1.0
CRAP 3.0
« HiD/Role/IsConverted.pm ↑ Summary HiD/Post.pm »
1 # ABSTRACT: Class representing a particular layout
2  
3  
4 package HiD::Layout;
5 9 $HiD::Layout::VERSION = '1.5';
6 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use warnings qw/ FATAL utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use open qw/ :std :utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use charnames qw/ :full /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use feature qw/ unicode_strings /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use File::Slurp qw/ read_file / ;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use HiD::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 9 has content => (
24 is => 'ro' ,
25 isa => 'Str' ,
26 required => 1 ,
27 );
28  
29  
30 9 has ext => (
31 is => 'ro' ,
32 isa => 'HiD_FileExtension' ,
33 );
34  
35  
36 9 has filename => (
37 is => 'ro' ,
38 isa => 'HiD_FilePath' ,
39 );
40  
41  
42 9 has layout => (
43 is => 'rw' ,
44 isa => 'Maybe[HiD::Layout]' ,
45 writer => 'set_layout' ,
46 );
47  
48  
49 has metadata => (
50 is => 'ro' ,
51 isa => 'HashRef',
52 lazy => 1 ,
53 1 ▶ default => sub {{}}
Subroutine __ANON__: called CC=1 SCAR=0.0
54 9 );
55  
56  
57 9 has name => (
58 is => 'ro' ,
59 isa => 'Str' ,
60 required => 1 ,
61 );
62  
63  
64 9 has processor => (
65 is => 'ro',
66 isa => 'Object' ,
67 required => 1 ,
68 handles => {
69 process_template => 'process' ,
70 processor_error => 'error' ,
71 },
72 );
73  
74  
75 sub BUILDARGS {
76 43 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=6 SCAR=18.2
Pod documented
77  
78 43 ▶ my %args = ( ref $_[0] and ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq 'HASH' ? : 43 0 43
Condition33%1/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
0043
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
79  
80 43 ▶ unless ( $args{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"content"}) 41 2 43
81 ( $args{name} , $args{ext} ) = $args{filename}
82 41 =~ m|^.*/(.+)\.([^.]+)$|;
83  
84 41 my $content = read_file( $args{filename}, binmode => ':utf8' );
85 41 my $metadata = {};
86  
87 41 ▶ if ( $content =~ /^---\n/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^---\n/su) 3 38 41
88 3 my $meta;
89 3 ( $meta , $content ) = ( $content )
90 =~ m|^---\n(.*?)---\n(.*)$|s;
91 3 ▶ $metadata = Load( encode('utf8', $meta) ) if $meta;
Branch: 1 branch
BranchTrueFalseTotal
if $meta 3 0 3
92 }
93  
94 40 $args{metadata} = $metadata;
95 40 $args{content} = $content;
96 }
97  
98 42 return \%args;
99 }
100  
101  
102 sub render {
103 64 ▶ my( $self , $data ) = @_;
Subroutine render: called CC=5 SCAR=16.1
Pod documented
104  
105 64 ▶ my $page_data = $data->{page} // {};
Condition100%2/2
$data->{"page"} // {}
l!l
604
MC/DC100%1/1
$data->{"page"} // {}
$data->{"page"}
Truth table100%2/2
A
A= $data->{"page"}
Aresult
11
00
106  
107 64 %{ $data->{page} } = (
108 64 %{ $self->metadata } ,
109 64 %{ $page_data },
110 );
111  
112 64 my $processed_input_content;
113 64 my $input_content = delete $data->{content};
114  
115 64 ▶ $self->process_template(
Branch: 1 branch
BranchTrueFalseTotal
$self->process_template(\$input_content, $data, \$processed_input_content) or die $self->processor_error 0 64 64
116 \$input_content ,
117 $data ,
118 \$processed_input_content ,
119 ) or die $self->processor_error;
120  
121 64 $data->{content} = $processed_input_content;
122  
123 64 my $output;
124  
125 64 ▶ $self->process_template(
Branch: 1 branch
BranchTrueFalseTotal
$self->process_template(\$self->content, $data, \$output) or die $self->processor_error 0 64 64
126 \$self->content ,
127 $data ,
128 \$output ,
129 ) or die $self->processor_error;
130  
131 64 ▶ if ( my $embedded_layout = $self->layout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $embedded_layout = $self->layout) 10 54 64
132 10 $data->{content} = $output;
133 10 $output = $embedded_layout->render( $data );
134 }
135  
136 64 return $output;
137 }
138  
139 9 __PACKAGE__->meta->make_immutable;
140 9 1;
141  
142 __END__
143  
144 =pod
145  
146 =encoding utf-8
147  
148 =head1 NAME
149  
150 HiD::Layout - Class representing a particular layout
151  
152 =head1 SYNOPSIS
153  
154 my $layout = HiD::Layout->new({
155 filename => $path_to_file ,
156 processor => $hid_processor_object ,
157 });
158  
159 =head1 DESCRIPTION
160  
161 Class representing layout files.
162  
163 =head1 ATTRIBUTES
164  
165 =head2 content
166  
167 Content of this layout.
168  
169 =head2 ext
170  
171 File extension of this layout.
172  
173 =head2 filename
174  
175 Filename of this layout.
176  
177 =head2 layout
178  
179 Name of a layout that will be used when processing this layout. (Can be
180 applied recursively.)
181  
182 =head2 metadata
183  
184 Metadata for this layout. Populated from the YAML front matter in the layout
185 file.
186  
187 =head2 name
188  
189 Name of the layout.
190  
191 =head2 processor
192  
193 Processor object used to process content through this layout when rendering.
194  
195 =head1 METHODS
196  
197 =head2 render
198  
199 Pass in a hash of data, apply the layout using that hash as input, and return
200 the resulting output string.
201  
202 Will recurse into embedded layouts as needed.
203  
204 =head1 VERSION
205  
206 version 1.5
207  
208 =head1 AUTHOR
209  
210 John SJ Anderson <genehack@genehack.org>
211  
212 =head1 COPYRIGHT AND LICENSE
213  
214 This software is copyright (c) 2014 by John SJ Anderson.
215  
216 This is free software; you can redistribute it and/or modify it under
217 the same terms as the Perl 5 programming language system itself.
218  
219 =cut
« HiD/Role/IsConverted.pm ↑ Summary HiD/Post.pm »