File Coverage

blib/lib/HiD/Layout.pm
Criterion Covered Total %
statement 62 62 100.0
branch 10 14 71.4
condition 3 5 60.0
subroutine 13 13 100.0
pod 2 2 100.0
total 90 96 93.7


line stmt bran cond sub pod time code
1             # ABSTRACT: Class representing a particular layout
2              
3              
4             package HiD::Layout;
5             our $AUTHORITY = 'cpan:GENEHACK';
6             $HiD::Layout::VERSION = '1.99';
7 12     12   73 use Moose;
  12         24  
  12         90  
8 12     12   70518 use namespace::autoclean;
  12         25  
  12         112  
9              
10 12     12   1143 use 5.014; # strict, unicode_strings
  12         38  
11 12     12   60 use utf8;
  12         51  
  12         93  
12 12     12   296 use autodie;
  12         22  
  12         91  
13 12     12   52591 use warnings qw/ FATAL utf8 /;
  12         25  
  12         529  
14 12     12   68 use open qw/ :std :utf8 /;
  12         37  
  12         87  
15 12     12   1969 use charnames qw/ :full /;
  12         25  
  12         81  
16              
17 12     12   2609 use Path::Tiny;
  12         27  
  12         716  
18 12     12   60 use YAML::Tiny qw/ Load /;
  12         25  
  12         637  
19              
20 12     12   71 use HiD::Types;
  12         143  
  12         8317  
21              
22              
23             has content => (
24             is => 'ro' ,
25             isa => 'Str' ,
26             required => 1 ,
27             );
28              
29              
30             has ext => (
31             is => 'ro' ,
32             isa => 'HiD_FileExtension' ,
33             );
34              
35              
36             has filename => (
37             is => 'ro' ,
38             isa => 'HiD_FilePath' ,
39             );
40              
41              
42             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             default => sub {{}}
54             );
55              
56              
57             has name => (
58             is => 'ro' ,
59             isa => 'Str' ,
60             required => 1 ,
61             );
62              
63              
64             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 39     39 1 83 my $class = shift;
77              
78 39 50 33     227 my %args = ( ref $_[0] and ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_;
  39         197  
79              
80 39 100       137 unless ( $args{content} ) {
81             ( $args{name} , $args{ext} ) = $args{filename}
82 37         297 =~ m|^.*/(.+)\.([^.]+)$|;
83              
84 37         212 my $content = path( $args{filename} )->slurp_utf8;
85 37         8646 my $metadata = {};
86              
87 37 100       202 if ( $content =~ /^---\n/s ) {
88 3         8 my $meta;
89 3         36 ( $meta , $content ) = ( $content )
90             =~ m|^---\n(.*?)---\n(.*)$|s;
91 3 50       35 $metadata = Load( $meta ) if $meta;
92             }
93              
94 36         902 $args{metadata} = $metadata;
95 36         109 $args{content} = $content;
96             }
97              
98 38         1077 return \%args;
99             }
100              
101              
102             sub render {
103 108     108 1 81143 my( $self , $data ) = @_;
104              
105 108   100     455 my $page_data = $data->{page} // {};
106              
107 108         573 %{ $data->{page} } = (
108 108         2754 %{ $self->metadata } ,
109 108         218 %{ $page_data },
  108         411  
110             );
111              
112 108         266 my $processed_input_content;
113 108         301 my $input_content = delete $data->{content};
114              
115 108 50       747 $self->process_template(
116             \$input_content ,
117             $data ,
118             \$processed_input_content ,
119             ) or die $self->processor_error;
120              
121 108         94475 $data->{content} = $processed_input_content;
122              
123 108         239 my $output;
124              
125 108 50       3649 $self->process_template(
126             \$self->content ,
127             $data ,
128             \$output ,
129             ) or die $self->processor_error;
130              
131 108 100       49940 if ( my $embedded_layout = $self->layout ) {
132 11         36 $data->{content} = $output;
133 11         45 $output = $embedded_layout->render( $data );
134             }
135              
136 108         648 return $output;
137             }
138              
139             __PACKAGE__->meta->make_immutable;
140             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.99
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) 2015 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