File Coverage

blib/lib/MsOffice/Word/Template/Engine/TT2/Provider.pm
Criterion Covered Total %
statement 18 18 100.0
branch 3 6 50.0
condition n/a
subroutine 4 4 100.0
pod n/a
total 25 28 89.2


line stmt bran cond sub pod time code
1             package MsOffice::Word::Template::Engine::TT2::Provider;
2 1     1   8 use strict;
  1         3  
  1         57  
3 1     1   6 use warnings;
  1         3  
  1         100  
4 1     1   7 use base 'Template::Provider';
  1         2  
  1         422  
5              
6             our $VERSION = '2.05';
7              
8              
9             sub _template_content {
10 1     1   3773 my ($self, $path) = @_;
11              
12 1 50       6 $path
13             or return (undef, "No path specified to fetch content from ");
14              
15             # if the path is not a Microsoft Word document, let the parent class handle it
16 1 50       10 $path =~ /\.do[ct][xm]?$/
17             or return $self->SUPER::_template_content($path);
18              
19             # parse the subtemplate
20 1         18 my $mod_date = (stat($path))[9];
21 1         14 my $subtemplate = MsOffice::Word::Template->new($path);
22 1         2445 my $doc_part = $subtemplate->surgeon->part("document");
23 1         10125 my $data = $subtemplate->engine->template_text_for_part($doc_part);
24              
25             # just keep the XML inside the document body
26 1         52 $data =~ s{^.*?<w:body>(.*?)</w:body>.*}{$1}s;
27              
28             # comply with the Provider API (see below)
29 1 50       16 return wantarray ? ( $data, undef, $mod_date )
30             : $data;
31             }
32              
33             #------------------------------------------------------------------------
34             # _template_content($path)
35             #
36             # Fetches content pointed to by $path.
37             # Returns the content in scalar context.
38             # Returns ($data, $error, $mtime) in list context where
39             # $data - content
40             # $error - error string if there was an error, otherwise undef
41             # $mtime - last modified time from calling stat() on the path
42             #------------------------------------------------------------------------
43              
44              
45              
46             1;
47              
48             __END__
49              
50             =encoding ISO-8859-1
51              
52             =head1 NAME
53              
54             MsOffice::Word::Template::Engine::TT2::Provider -- subclass of Template::Provider for loading .docx templates
55              
56             =head1 DESCRIPTION
57              
58             This subclass of L<Template::Provider> is called whenever a C<MsOffice::Word::Template> document
59             requires a C<.docx> subtemplate through the C<INSERT>, C<INCLUDE> or C<PROCESS> directives.
60              
61             The subtemplate is parsed as usual, but only the inner document body is returned to the caller,
62             so that it can be inserted in a parent document.
63              
64             =head1 AUTHOR
65              
66             Laurent Dami, E<lt>dami AT cpan DOT org<gt>
67              
68             =head1 COPYRIGHT AND LICENSE
69              
70             Copyright 2024 by Laurent Dami.
71              
72             This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
73