Muster/Hook/FieldSubst.pm

filter: Statementstmt 20.0% 17 / 85 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 5 / 10 Podpod 100% 4 / 4 totaltotal 19.5% 26 / 133 CC 20 SCAR 43.7
CC
20
Cov
14%
get_field_value
132.0
get_other_page_field_value
90.0
process
6.0
CRAP 78.7
« Muster/Hook/HeadFoot.pm ↑ Summary Muster/Hook/Include.pm »
1 package Muster::Hook::FieldSubst;
2 1 $Muster::Hook::FieldSubst::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::FieldSubst - Muster hook for field substitution
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 SYNOPSIS
12  
13 # CamelCase plugin name
14 package Muster::Hook::FieldSubst;
15 use Mojo::Base 'Muster::Hook';
16  
17 =head1 DESCRIPTION
18  
19 L<Muster::Hook::FieldSubst> does field substition;
20 that is, it replaces a "field macro" with the content of that field
21 (aka the meta-data for the Leaf).
22  
23 The pattern for fields is "{{$I<fieldname>}}".
24  
25 =cut
26  
27 1 ▶use Mojo::Base 'Muster::Hook';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 METHODS
34  
35 =head2 register
36  
37 Initialize, and register hooks.
38  
39 =cut
40 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
41 0 my $self = shift;
42 0 my $hookmaster = shift;
43 0 my $conf = shift;
44  
45 # we need to be able to look things up in the database
46 0 $self->{metadb} = $hookmaster->{metadb};
47 0 $self->{_nt} = Text::NeatTemplate->new();
48  
49 0 ▶ $hookmaster->add_hook('fieldsubst' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my %args = @_;
51  
52 0 return $self->process(%args);
53 },
54 0 );
55 0 return $self;
56 } # register
57  
58 =head2 process
59  
60 Process (scan or modify) a leaf object.
61 In scanning phase, this will do simple substitutions of known meta-data
62 (for example, "page").
63 In assembly phase, it will do the same, but the contents of the Leaf meta-data will likely be more complete.
64  
65 In either case, this expects the leaf meta-data to be populated.
66  
67 my $new_leaf = $self->process(%args);
68  
69 =cut
70 0 ▶sub process {
Subroutine process: not called CC=2 SCAR=17.9
Pod documented
71 0 my $self = shift;
72 0 my %args = @_;
73  
74 0 my $leaf = $args{leaf};
75 0 my $phase = $args{phase};
76  
77 0 my $content = $leaf->cooked();
78 0 my $page = $leaf->pagename;
79  
80 # substitute {{$var}} variables (source-page)
81 0 $content =~ s/(\\?)\{\{\$([-_\w:]+)\}\}/$self->get_field_value($1,$2,$leaf)/eg;
82  
83 # we can't get other pages' meta-data if we are scanning
84 # because they haven't been added to the database yet
85 0 ▶ if ($phase eq $Muster::Hooks::PHASE_BUILD)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq $Muster::Hooks::PHASE_BUILD) 0 0 0
86 {
87 # substitute {{$page#var}} variables (source-page)
88 0 $content =~ s/(\\?)\{\{\$([-\w\/]+)#([-_\w:]+)\}\}/$self->get_other_page_field_value($1, $3,$leaf,$2)/eg;
89 }
90  
91 0 $leaf->{cooked} = $content;
92 0 return $leaf;
93 } # process
94  
95 =head2 get_field_value
96  
97 Get the field value for this page.
98  
99 =cut
100 0 ▶sub get_field_value {
Subroutine get_field_value: not called CC=11 SCAR=48.8
Pod documented
101 0 my $self = shift;
102 0 my $escape = shift;
103 0 my $field = shift;
104 0 my $leaf = shift;
105  
106 0 ▶ if (length $escape)
Branch: 1 branch
BranchTrueFalseTotal
if (length $escape) 0 0 0
107 {
108 0 return "{{\$${field}}}";
109 }
110  
111 # force all fields to lower-case
112 0 $field = lc($field);
113 0 my ($varname, @formats) = split(':', $field);
114  
115 0 my $value = '';
116 0 ▶ if ($varname eq 'page') # page is pagename
Branch: 4 branches
BranchTrueFalseTotal
if ($varname eq 'page') { } 0 0 0
elsif ($varname eq 'pagesrc') { } 0 0 0
elsif (exists $leaf->{'meta'}{$varname}) { } 0 0 0
elsif (exists $leaf->{$varname}) { } 0 0 0
117 {
118 0 $value = $leaf->pagename;
119 }
120 elsif ($varname eq 'pagesrc')
121 {
122 0 $value = $leaf->pagesrcname;
123 }
124 elsif (exists $leaf->{meta}->{$varname})
125 {
126 0 $value = $leaf->{meta}->{$varname};
127 }
128 elsif (exists $leaf->{$varname})
129 {
130 0 $value = $leaf->{$varname};
131 }
132 0 ▶ if (!defined $value)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
133 {
134 0 return '';
135 }
136 0 ▶ if (ref $value eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
137 {
138 0 $value = join('|', @{$value});
139 }
140 elsif (ref $value eq 'HASH')
141 {
142 0 $value = Dump($value);
143 }
144  
145 # Format the value
146 0 foreach my $format (@formats) {
147 0 $value = $self->{_nt}->convert_value(value=>$value,
148 format=>$format,
149 name=>$varname);
150 }
151 0 ▶ return ($value ? $value : '');
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
152 } # get_field_value
153  
154 =head2 get_other_page_field_value
155  
156 Get the field value for a different page.
157  
158 =cut
159 0 ▶sub get_other_page_field_value {
Subroutine get_other_page_field_value: not called CC=9 SCAR=45.0
Pod documented
160 0 my $self = shift;
161 0 my $escape = shift;
162 0 my $field = shift;
163 0 my $leaf = shift;
164 0 my $other_page = shift;
165  
166 0 ▶ if (length $escape)
Branch: 1 branch
BranchTrueFalseTotal
if (length $escape) 0 0 0
167 {
168 0 return "{{\$${other_page}#${field}}}";
169 }
170  
171 # force all fields to lower-case
172 0 $field = lc($field);
173 0 my ($varname, @formats) = split(':', $field);
174  
175 0 my $value = '';
176 0 my $use_page = $self->{metadb}->bestlink($leaf->pagename, $other_page);
177 # $use_page will be blank if the page doesn't exist
178 0 ▶ if ($use_page)
Branch: 1 branch
BranchTrueFalseTotal
if ($use_page) 0 0 0
179 {
180 0 my $info = $self->{metadb}->page_or_file_info($use_page);
181 0 ▶ if ($varname eq 'page') # page is pagename
Branch: 2 branches
BranchTrueFalseTotal
if ($varname eq 'page') { } 0 0 0
elsif (exists $info->{$varname}) { } 0 0 0
182 {
183 0 $value = $info->{pagename};
184 }
185 elsif (exists $info->{$varname})
186 {
187 0 $value = $info->{$varname};
188 0 ▶ if (ref $value eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
189 {
190 0 $value = join('|', @{$value});
191 }
192 elsif (ref $value eq 'HASH')
193 {
194 0 $value = Dump($value);
195 }
196 }
197 }
198  
199 # Format the value
200 0 foreach my $format (@formats) {
201 0 $value = $self->{_nt}->convert_value(value=>$value,
202 format=>$format,
203 name=>$varname);
204 }
205 0 ▶ return ($value ? $value : '');
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
206 } # get_other_page_field_value
207  
208 1 1;
« Muster/Hook/HeadFoot.pm ↑ Summary Muster/Hook/Include.pm »