Template/Engine/TT2.pm

filter: Statementstmt 76.5% 75 / 98 Branchbran 60.0% 6 / 10 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 84.2% 16 / 19 Podpod 0.0% 0 / 4 totaltotal 64.1% 102 / 159 CC 15 SCAR 16.2
CC
15
Cov
68%
__ANON__
20.0
__ANON__
12.0
_TT2
3.1
CRAP 5.1
↑ Summary Template.pm »
1 package MsOffice::Word::Template::Engine::TT2;
2 1 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'MsOffice::Word::Template::Engine';
5  
6 1 ▶use Template::AutoFilter; # a subclass of Template that adds automatic html filtering
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Template::Config; # loaded explicitly so that we can override its $PROVIDER global variable
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MsOffice::Word::Surgeon::Utils qw(encode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MsOffice::Word::Template::Engine::TT2::Provider;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '2.05';
14  
15 #======================================================================
16 # ATTRIBUTES
17 #======================================================================
18  
19 1 has 'start_tag' => (is => 'ro', isa => 'Str', default => "[% ");
20 1 has 'end_tag' => (is => 'ro', isa => 'Str', default => " %]");
21 1 has 'TT2' => (is => 'ro', isa => 'Template', lazy => 1, builder => "_TT2", init_arg => undef);
22  
23 #======================================================================
24 # LAZY ATTRIBUTE CONSTRUCTORS
25 #======================================================================
26  
27 1 ▶sub _TT2 {
Subroutine _TT2: called CC=3 SCAR=11.2
28 1 my ($self) = @_;
29  
30 1 my $TT2_args = delete $self->{_constructor_args};
31  
32 # inject precompiled blocks into the Template parser
33 1 my $precompiled_blocks = $self->_precompiled_blocks;
34 1 ▶ $TT2_args->{BLOCKS}{$_} //= $precompiled_blocks->{$_} for keys %$precompiled_blocks;
Condition33%1/3
$TT2_args->{"BLOCKS"}{$_} //= $precompiled_blocks->{$_}
l!l&&r!l&&!r
040
MC/DC0%0/2
$TT2_args->{"BLOCKS"}{$_} //= $precompiled_blocks->{$_}
$TT2_args->{"BLOCKS"}{$_} $precompiled_blocks->{$_}
Truth table33%1/3
A //= B
A= $TT2_args->{"BLOCKS"}{$_}B= $precompiled_blocks->{$_}
ABresult
1X1
011
000
35  
36 # instantiate the Template object but supply our own Provider subclass. This way of doing is a little
37 # bit rude, but so much easier than using the LOAD_TEMPLATES config options ! ... because here the
38 # Template Toolkit will take care of supplying all default values to the Provider.
39 1 local $Template::Config::PROVIDER = 'MsOffice::Word::Template::Engine::TT2::Provider';
40 1 my $tt2 = Template::AutoFilter->new($TT2_args);
41  
42 1 return $tt2;
43 }
44  
45 #======================================================================
46 # METHODS
47 #======================================================================
48  
49 9 ▶sub compile_template {
Subroutine compile_template: called CC=1 SCAR=0.0
Pod undocumented
50 9 my ($self, $template_text) = @_;
51  
52 9 return $self->TT2->template(\$template_text);
53 }
54  
55  
56 14 ▶sub process_part {
Subroutine process_part: called CC=1 SCAR=0.0
Pod undocumented
57 14 my ($self, $part_name, $package_part, $vars) = @_;
58  
59 # extend $vars with a pointer to the part object, so that it can be called from
60 # the template, for example for replacing an image
61 14 my $extended_vars = {package_part => $package_part, %$vars};
62  
63 14 return $self->process($part_name, $extended_vars);
64 }
65  
66  
67 18 ▶sub process {
Subroutine process: called CC=2 SCAR=7.0
Pod undocumented
68 18 my ($self, $template_name, $vars) = @_;
69  
70 # get the compiled template
71 18 ▶ my $tmpl = $self->compiled_template->{$template_name}
Branch: 1 branch
BranchTrueFalseTotal
my $tmpl = $self->compiled_template->{$template_name} or die "don't have a compiled template for '${template_name}'" 18 0 18
72 or die "don't have a compiled template for '$template_name'";
73  
74 # produce the new contents
75 18 my $new_contents = $self->TT2->context->process($tmpl, $vars);
76  
77 18 return $new_contents;
78 }
79  
80  
81 #======================================================================
82 # PRE-COMPILED BLOCKS THAT CAN BE INVOKED FROM TEMPLATE DIRECTIVES
83 #======================================================================
84  
85 # arbitrary value for the first bookmark id. 100 should be enough to be above other
86 # bookmarks generated by Word itself. A cleaner but more complicated way would be to parse the template
87 # to find the highest id number really used.
88 1 my $first_bookmark_id = 100;
89  
90 my %barcode_generator = (
91  
92 0 ▶ Code128 => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
93 0 my ($to_encode, %options) = @_;
94 0 require Barcode::Code128;
95  
96 0 ▶ $options{border} //= 0;
Condition0%0/2
$options{"border"} //= 0
l!l
00
MC/DC0%0/1
$options{"border"} //= 0
$options{"border"}
Truth table0%0/2
A
A= $options{"border"}
Aresult
11
00
97 0 ▶ $options{show_text} //= 0;
Condition0%0/2
$options{"show_text"} //= 0
l!l
00
MC/DC0%0/1
$options{"show_text"} //= 0
$options{"show_text"}
Truth table0%0/2
A
A= $options{"show_text"}
Aresult
11
00
98 0 ▶ $options{padding} //= 0;
Condition0%0/2
$options{"padding"} //= 0
l!l
00
MC/DC0%0/1
$options{"padding"} //= 0
$options{"padding"}
Truth table0%0/2
A
A= $options{"padding"}
Aresult
11
00
99 0 my $bc = Barcode::Code128->new;
100 0 $bc->option(%options);
101 0 return $bc->png($to_encode);
102 },
103  
104 0 ▶ QRCode => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 my ($to_encode, %options) = @_;
106 0 require Image::PNG::QRCode;
107 0 return Image::PNG::QRCode::qrpng(text => $to_encode);
108 },
109  
110 1 );
111  
112  
113  
114 # precompiled blocks as facilities to be used within templates
115 1 ▶sub _precompiled_blocks {
Subroutine _precompiled_blocks: called CC=1 SCAR=0.5
116  
117 return {
118  
119 # a wrapper block for inserting a Word bookmark
120 8 ▶ bookmark => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
121 8 my $context = shift;
122 8 my $stash = $context->stash;
123  
124 # assemble xml markup
125 8 ▶ my $bookmark_id = $stash->get('global.bookmark_id') || $first_bookmark_id;
Condition66%2/3
$stash->get('global.bookmark_id') || $first_bookmark_id
l!l&&r!l&&!r
710
MC/DC0%0/2
$stash->get('global.bookmark_id') || $first_bookmark_id
$stash->get('global.bookmark_id') $first_bookmark_id
Truth table66%2/3
A || B
A= $stash->get('global.bookmark_id')B= $first_bookmark_id
ABresult
1X1
011
000
126 8 ▶ my $name = fix_bookmark_name($stash->get('name') || 'anonymous_bookmark');
Condition50%1/2
$stash->get('name') || 'anonymous_bookmark'
l!l
80
MC/DC0%0/1
$stash->get('name') || 'anonymous_bookmark'
$stash->get('name')
Truth table50%1/2
A
A= $stash->get('name')
Aresult
11
00
127  
128 8 my $xml = qq{<w:bookmarkStart w:id="$bookmark_id" w:name="$name"/>}
129 . $stash->get('content') # content of the wrapper
130 . qq{<w:bookmarkEnd w:id="$bookmark_id"/>};
131  
132 # next bookmark will need a fresh id
133 8 $stash->set('global.bookmark_id', $bookmark_id+1);
134  
135 8 return $xml;
136 },
137  
138 # a wrapper block for linking to a bookmark
139 8 ▶ link_to_bookmark => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
140 8 my $context = shift;
141 8 my $stash = $context->stash;
142  
143 # assemble xml markup
144 8 ▶ my $name = fix_bookmark_name($stash->get('name') || 'anonymous_bookmark');
Condition50%1/2
$stash->get('name') || 'anonymous_bookmark'
l!l
80
MC/DC0%0/1
$stash->get('name') || 'anonymous_bookmark'
$stash->get('name')
Truth table50%1/2
A
A= $stash->get('name')
Aresult
11
00
145 8 my $content = $stash->get('content');
146 8 my $tooltip = $stash->get('tooltip');
147 8 ▶ if ($tooltip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tooltip) 2 6 8
148 2 encode_entities($tooltip);
149 2 $tooltip = qq{ w:tooltip="$tooltip"};
150 }
151 8 my $xml = qq{<w:hyperlink w:anchor="$name"$tooltip>$content</w:hyperlink>};
152  
153 8 return $xml;
154 },
155  
156 # a block for generating a Word field. Can also be used as wrapper.
157 6 ▶ field => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
158 6 my $context = shift;
159 6 my $stash = $context->stash;
160 6 my $code = $stash->get('code'); # field code, including possible flags
161 6 my $text = $stash->get('content'); # initial text content (before updating the field)
162  
163 6 my $xml = qq{<w:r><w:fldChar w:fldCharType="begin"/></w:r>}
164 . qq{<w:r><w:instrText xml:space="preserve"> $code </w:instrText></w:r>};
165 6 ▶ $xml .= qq{<w:r><w:fldChar w:fldCharType="separate"/></w:r>$text} if $text;
Branch: 1 branch
BranchTrueFalseTotal
if $text 2 4 6
166 6 $xml .= qq{<w:r><w:fldChar w:fldCharType="end"/></w:r>};
167  
168 6 return $xml;
169 },
170  
171 # a block for replacing a placeholder image by a generated barcode
172 0 ▶ barcode => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
173  
174 # get parameters from stash
175 0 my $context = shift;
176 0 my $stash = $context->stash;
177 0 my $barcode_type = $stash->get('type'); # either 'Code128' or 'QRCode'
178 0 my $package_part = $stash->get('package_part'); # Word::Surgeon::PackagePart
179 0 my $img = $stash->get('img'); # title of an existing image to replace
180 0 my $to_encode = $stash->get('content'); # text to be encoded
181 0 ▶ my $options = $stash->get('options') || {};
Condition0%0/2
$stash->get('options') || {}
l!l
00
MC/DC0%0/1
$stash->get('options') || {}
$stash->get('options')
Truth table0%0/2
A
A= $stash->get('options')
Aresult
11
00
182 0 $to_encode =~ s(<[^>]+>)()g;
183  
184 # generate PNG image
185 0 ▶ my $generator = $barcode_generator{$barcode_type}
Branch: 1 branch
BranchTrueFalseTotal
my $generator = $barcode_generator{$barcode_type} or die "unknown type for barcode generator : '${barcode_type}'" 0 0 0
186 or die "unknown type for barcode generator : '$barcode_type'";
187 0 my $png = $generator->($to_encode, %$options);
188  
189 # inject image into the .docx file
190 0 $package_part->replace_image($img, $png);
191  
192 0 return "";
193 },
194  
195 1 };
196 }
197  
198  
199  
200 #======================================================================
201 # UTILITY ROUTINES (not methods)
202 #======================================================================
203  
204  
205 16 ▶sub fix_bookmark_name {
Subroutine fix_bookmark_name: called CC=2 SCAR=7.0
Pod undocumented
206 16 my $name = shift;
207  
208 # see https://stackoverflow.com/questions/852922/what-are-the-limitations-for-bookmark-names-in-microsoft-word
209  
210 16 $name =~ s/[^\w_]+/_/g; # only digits, letters or underscores
211 16 $name =~ s/^(\d)/_$1/; # cannot start with a digit
212 16 ▶ $name = substr($name, 0, 40) if length($name) > 40; # max 40 characters long
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 40 0 16 16
213  
214 16 return $name;
215 }
216  
217  
218 1 1;
219  
220 __END__
↑ Summary Template.pm »