HiD/Role/IsConverted.pm

filter: Statementstmt 94.2% 98 / 104 Branchbran 77.7% 14 / 18 Conditioncond 56.2% 9 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 95.4% 21 / 22 Podpod n/a totaltotal 85.2% 144 / 169 CC 16 SCAR 12.2
CC
16
Cov
88%
__ANON__
8.5
__ANON__
4.5
__ANON__
3.0
CRAP 3.4
« HiD/Role/IsPost.pm ↑ Summary HiD/Layout.pm »
1 # ABSTRACT: Role for objects that are converted during the publishing process
2  
3  
4 package HiD::Role::IsConverted;
5 23 our $AUTHORITY = 'cpan:GENEHACK';
6 23 $HiD::Role::IsConverted::VERSION = '1.96';
7 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 ▶use 5.014; # strict, unicode_strings
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use warnings qw/ FATAL utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use open qw/ :std :utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use charnames qw/ :full /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use Class::Load qw/ load_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 ▶use YAML::XS qw/ Load /; # YAML::Tiny doesn't support 'bool' types which we need 8^/
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 23 ▶use HiD::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 23 requires 'get_default_layout';
25  
26  
27 23 has content => (
28 is => 'ro',
29 isa => 'Str',
30 required => 1 ,
31 );
32  
33  
34 has converted_content => (
35 is => 'ro' ,
36 isa => 'Str' ,
37 lazy => 1 ,
38 default => sub {
39 51 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
40 51 return _convert_by_extension( $self->content , $self->ext );
41 }
42 23 );
43  
44  
45 has converted_excerpt => (
46 is => 'ro' ,
47 isa => 'Str' ,
48 lazy => 1 ,
49 default => sub {
50 10 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
51  
52 10 my $converted_excerpt = _convert_by_extension( $self->excerpt , $self->ext );
53  
54 10 ▶ if ( $self->excerpt ne $self->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->excerpt ne $self->content) 1 9 10
55 # Add the "read more" link
56 ### FIXME this should be configurable
57 1 $converted_excerpt .= $self->readmore_link;
58 }
59  
60 10 return $converted_excerpt;
61 },
62 23 );
63  
64  
65 23 has hid => (
66 is => 'ro' ,
67 isa => 'HiD' ,
68 required => 1 ,
69 handles => [ qw/ get_config /] ,
70 );
71  
72  
73 23 has layouts => (
74 is => 'ro' ,
75 isa => 'HashRef[HiD::Layout]' ,
76 required => 1 ,
77 );
78  
79  
80 has metadata => (
81 is => 'ro' ,
82 isa => 'HashRef' ,
83 0 ▶ default => sub {{}} ,
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 23 lazy => 1,
85 traits => [ 'Hash' ] ,
86 handles => {
87 get_metadata => 'get',
88 },
89 );
90  
91  
92 has readmore_link => (
93 is => 'ro' ,
94 isa => 'Str' ,
95 lazy => 1 ,
96 default => sub {
97 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=9.9
98  
99 1 ▶ if ( defined $self->get_config('readmore_link')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_config("readmore_link")) 0 1 1
100 0 my $link = $self->get_config('readmore_link');
101 0 my $url = $self->url;
102 0 $link =~ s/__URL__/$url/;
103 0 return $link;
104 };
105  
106 return
107 1 q{<p class="readmore"><a href="}
108 . $self->url
109 . q{" class="readmore">read more</a></p>};
110 },
111 23 );
112  
113  
114 has rendered_content => (
115 is => 'ro' ,
116 isa => 'Str' ,
117 lazy => 1 ,
118 default => sub {
119 51 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=15.0
120  
121 51 ▶ my $layout_name = $self->get_metadata( 'layout' ) // $self->get_default_layout;
Condition66%2/3
$self->get_metadata("layout") // $self->get_default_layout
l!l&&r!l&&!r
16350
MC/DC0%0/2
$self->get_metadata("layout") // $self->get_default_layout
$self->get_metadata("layout") $self->get_default_layout
Truth table66%2/3
A // B
A= $self->get_metadata("layout")B= $self->get_default_layout
ABresult
1X1
011
000
122  
123 my $layout = $self->layouts->{$layout_name} // $self->layouts->{default} //
124 51 ▶ die "FIXME no default layout?";
Condition40%2/5
($self->layouts->{$layout_name} // $self->layouts->{"default"}) // die("FIXME no default layout?")
l!l
510
$self->layouts->{$layout_name} // $self->layouts->{"default"}
l!l&&r!l&&!r
5100
MC/DC0%0/2
($self->layouts->{$layout_name} // $self->layouts->{"default"}) // die("FIXME no default layout?")
$self->layouts->{$layout_name} $self->layouts->{"default"}
Truth table33%1/3
A // B
A= $self->layouts->{$layout_name}B= $self->layouts->{"default"}
ABresult
1X1
011
000
125  
126 51 my $output = $layout->render( $self->template_data );
127  
128 50 return $output;
129 }
130 23 );
131  
132  
133 has template_data => (
134 is => 'ro' ,
135 isa => 'HashRef' ,
136 lazy => 1 ,
137 default => sub {
138 51 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
139  
140 my $data = {
141 baseurl => $self->hid->config->{baseurl} ,
142 51 content => $self->converted_content ,
143 page => $self->metadata ,
144 site => $self->hid ,
145 };
146 51 ▶ $data->{post} = $self if $self->does('HiD::Role::IsPost');
Branch: 1 branch
BranchTrueFalseTotal
if $self->does("HiD::Role::IsPost") 20 31 51
147  
148 51 ▶ $data->{page}{url} = $self->url if $self->can( 'url' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("url") 51 0 51
149  
150 50 return $data;
151 },
152 23 );
153  
154 around BUILDARGS => sub {
155 88 ▶ my $orig = shift;
Subroutine __ANON__: called CC=8 SCAR=21.4
156 88 my $class = shift;
157  
158 88 ▶ my %args = ( ref $_[0] and ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq 'HASH' ? : 88 0 88
Condition33%1/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
0088
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
159  
160 88 ▶ unless ( $args{content} and $args{metadata} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"content"} and $args{"metadata"}) 85 3 88
Condition66%2/3
$args{"content"} and $args{"metadata"}
!ll&&!rl&&r
8503
MC/DC50%1/2
$args{"content"} and $args{"metadata"}
$args{"content"} $args{"metadata"}
Truth table66%2/3
A and B
A= $args{"content"}B= $args{"metadata"}
ABresult
0X0
100
111
161 85 my $file_content = path( $args{input_filename} )->slurp_utf8;
162  
163 85 my( $metadata , $content );
164 85 ▶ if ( $file_content =~ /^---/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file_content =~ /^---/u) { } 77 8 85
elsif ($args{'input_filename'} =~ /\.html?$/u) { } 0 8 8
165 77 ( $metadata , $content ) = $file_content
166 =~ /^---\n?(.*?)---\n?(.*)$/ms;
167 }
168 elsif ( $args{input_filename} =~ /\.html?$/ ) {
169 0 die "plain HTML file without YAML front matter"
170 }
171 else {
172 8 $content = $file_content;
173 8 $metadata = '';
174 }
175  
176 85 $args{content} = $content;
177 85 ▶ $args{metadata} = Load( $metadata ) // {};
Condition100%2/2
Load($metadata) // {}
l!l
769
MC/DC100%1/1
Load($metadata) // {}
Load($metadata)
Truth table100%2/2
A
A= Load($metadata)
Aresult
11
00
178 }
179  
180 88 return $class->$orig( \%args );
181 23 };
182  
183 { # hide the map
184  
185 ### FIXME make this extensible
186 23 my %conversion_extension_map = (
187 markdown => [ 'Text::Markdown' , 'markdown' ] ,
188 mkdn => [ 'Text::Markdown' , 'markdown' ] ,
189 mk => [ 'Text::Markdown' , 'markdown' ] ,
190 md => [ 'Text::Markdown' , 'markdown' ] ,
191 mmd => [ 'Text::MultiMarkdown' , 'markdown' ] ,
192 textile => [ 'Text::Textile' , 'process' ] ,
193 );
194  
195 sub _convert_by_extension {
196 61 ▶ my( $content , $extension ) = @_;
Subroutine _convert_by_extension: called CC=2 SCAR=6.9
197  
198 return $content
199 61 ▶ unless exists $conversion_extension_map{ $extension };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conversion_extension_map{$extension} 12 49 61
200  
201 49 my( $module , $method ) = @{ $conversion_extension_map{ $extension }};
202 49 load_class( $module );
203  
204 49 my $converted = $module->new->$method( $content );
205 49 return $converted;
206 }
207 }
208  
209 23 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
210 23 1;
211  
212 __END__
213  
214 =pod
215  
216 =encoding UTF-8
217  
218 =head1 NAME
219  
220 HiD::Role::IsConverted - Role for objects that are converted during the publishing process
221  
222 =head1 SYNOPSIS
223  
224 package HiD::ThingThatIsConverted
225 use Moose;
226 with 'HiD::Role::IsConverted';
227  
228 ...
229  
230 1;
231  
232 =head1 DESCRIPTION
233  
234 This role is consumed by objects that are converted during the publication
235 process -- e.g., from Markdown or Textile to HTML, or rendered through a
236 layout object. This role provides required attributes and methods used during
237 that process.
238  
239 =head1 ATTRIBUTES
240  
241 =head2 content ( ro / Str / required )
242  
243 Page content (stuff after the YAML front matter)
244  
245 =head2 converted_content ( ro / Str / lazily built from content )
246  
247 Content after it has gone through the conversion process.
248  
249 =head2 converted_excerpt ( ro / Str / lazily built from content )
250  
251 Excerpt after it has gone through the conversion process
252  
253 =head2 hid
254  
255 The HiD object for the current site. Here primarily to provide access to site
256 metadata.
257  
258 =head2 layouts ( ro / HashRef[HiD::Layout] / required )
259  
260 Hashref of layout objects keyed by name.
261  
262 =head2 metadata ( ro / HashRef )
263  
264 Hashref of info from YAML front matter
265  
266 =head2 readmore_link
267  
268 Placed at the bottom of rendered excerpts. Intended to link to the full
269 version of the content.
270  
271 A string matching C<__URL__> will be replaced with the URL of the object (i.e.,
272 the output of C<$self->url>) being converted.
273  
274 =head2 rendered_content
275  
276 Content after any layouts have been applied
277  
278 =head2 template_data
279  
280 Data for passing to template processing function.
281  
282 =head1 VERSION
283  
284 version 1.96
285  
286 =head1 AUTHOR
287  
288 John SJ Anderson <genehack@genehack.org>
289  
290 =head1 COPYRIGHT AND LICENSE
291  
292 This software is copyright (c) 2015 by John SJ Anderson.
293  
294 This is free software; you can redistribute it and/or modify it under
295 the same terms as the Perl 5 programming language system itself.
296  
297 =cut
« HiD/Role/IsPost.pm ↑ Summary HiD/Layout.pm »