| 1 | # ABSTRACT: Class representing a particular layout | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package HiD::Layout; | ||||||||||||||||||||||||||||
| 5 | 18 | our $AUTHORITY = 'cpan:GENEHACK'; | |||||||||||||||||||||||||||
| 6 | 18 | $HiD::Layout::VERSION = '1.96'; | |||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use 5.014; # strict, unicode_strings | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use warnings qw/ FATAL utf8 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 18 | ▶ | use open qw/ :std :utf8 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use charnames qw/ :full /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 18 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 18 | ▶ | use YAML::Tiny qw/ Load /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 18 | ▶ | use HiD::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 18 | has content => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro' , | ||||||||||||||||||||||||||||
| 25 | isa => 'Str' , | ||||||||||||||||||||||||||||
| 26 | required => 1 , | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 18 | has ext => ( | |||||||||||||||||||||||||||
| 31 | is => 'ro' , | ||||||||||||||||||||||||||||
| 32 | isa => 'HiD_FileExtension' , | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 18 | has filename => ( | |||||||||||||||||||||||||||
| 37 | is => 'ro' , | ||||||||||||||||||||||||||||
| 38 | isa => 'HiD_FilePath' , | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 18 | 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 | 18 | ); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 18 | has name => ( | |||||||||||||||||||||||||||
| 58 | is => 'ro' , | ||||||||||||||||||||||||||||
| 59 | isa => 'Str' , | ||||||||||||||||||||||||||||
| 60 | required => 1 , | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 18 | 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 | 41 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 41 | ▶ | my %args = ( ref $_[0] and ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[0] and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 41 | ▶ | unless ( $args{content} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | ( $args{name} , $args{ext} ) = $args{filename} | ||||||||||||||||||||||||||||
| 82 | 39 | =~ m|^.*/(.+)\.([^.]+)$|; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 39 | my $content = path( $args{filename} )->slurp_utf8; | |||||||||||||||||||||||||||
| 85 | 39 | my $metadata = {}; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 39 | ▶ | if ( $content =~ /^---\n/s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 3 | my $meta; | |||||||||||||||||||||||||||
| 89 | 3 | ( $meta , $content ) = ( $content ) | |||||||||||||||||||||||||||
| 90 | =~ m|^---\n(.*?)---\n(.*)$|s; | ||||||||||||||||||||||||||||
| 91 | 3 | ▶ | $metadata = Load( $meta ) if $meta; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 38 | $args{metadata} = $metadata; | |||||||||||||||||||||||||||
| 95 | 38 | $args{content} = $content; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 40 | 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"} // {}
MC/DC100%1/1
$data->{"page"} // {}
$data->{"page"}
Truth table100%2/2
A
A= $data->{"page"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | 10 | $data->{content} = $output; | |||||||||||||||||||||||||||
| 133 | 10 | $output = $embedded_layout->render( $data ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 64 | return $output; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 18 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 140 | 18 | 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.96 | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||