| 1 | package MsOffice::Word::Template; | ||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.024; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use MsOffice::Word::Surgeon 2.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # syntactic sugar for attributes | ||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | sub has_inner ($@) {my $attr = shift; has($attr => @_, init_arg => undef, lazy => 1, builder => "_$attr")} | ||||||||||||||||||||||||||||||
|
Subroutine
has_inner: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '2.05'; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 16 | # ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 17 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | # constructor attributes for interacting with MsWord | ||||||||||||||||||||||||||||||||
| 20 | # See also BUILDARGS: the constructor can also take a "docx" arg | ||||||||||||||||||||||||||||||||
| 21 | # that will be automatically translated into a "surgeon" attribute | ||||||||||||||||||||||||||||||||
| 22 | 3 | has 'surgeon' => (is => 'ro', isa => 'MsOffice::Word::Surgeon', required => 1); | |||||||||||||||||||||||||||||||
| 23 | 3 | has 'data_color' => (is => 'ro', isa => 'Str', default => "yellow"); | |||||||||||||||||||||||||||||||
| 24 | 3 | has 'control_color' => (is => 'ro', isa => 'Str', default => "green"); | |||||||||||||||||||||||||||||||
| 25 | has 'part_names' => (is => 'ro', isa => 'ArrayRef[Str]', lazy => 1, | ||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | default => sub {[keys shift->surgeon->parts->%*]}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | has 'property_files'=> (is => 'ro', isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | default => sub {[qw(docProps/core.xml docProps/app.xml docProps/custom.xml)]}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | # constructor attributes for building a templating engine | ||||||||||||||||||||||||||||||||
| 31 | 3 | has 'engine_class' => (is => 'ro', isa => 'Str', default => 'TT2'); | |||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | has 'engine_args' => (is => 'ro', isa => 'ArrayRef', default => sub {[]}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | # attributes lazily constructed by the module -- not received through the constructor | ||||||||||||||||||||||||||||||||
| 35 | 3 | has_inner 'engine' => (is => 'ro', isa => 'MsOffice::Word::Template::Engine'); | |||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 39 | # BUILDING INSTANCES | ||||||||||||||||||||||||||||||||
| 40 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # syntactic sugar for supporting ->new($surgeon) instead of ->new(surgeon => $surgeon) | ||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||
| 44 | 2 | my $orig = shift; | |||||||||||||||||||||||||||||||
| 45 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | # if there is a unique arg without any keyword ... | ||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if ( @_ == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # if the unique arg is an instance of Surgeon, it's the "surgeon" parameter | ||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | unshift @_, 'surgeon' if $_[0]->isa('MsOffice::Word::Surgeon'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | # if the unique arg is a string, it's the "docx" parameter | ||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | unshift @_, 'docx' if $_[0] && !ref $_[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] and not ref $_[0]
MC/DC0%0/2
$_[0] and not ref $_[0]
$_[0]
not ref $_[0]
Truth table33%1/3
A and not(B)
A= $_[0]B= not ref $_[0]
| |||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # translate the "docx" parameter into a "surgeon" parameter | ||||||||||||||||||||||||||||||||
| 58 | 2 | my %args = @_; | |||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | if (my $docx = delete $args{docx}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 2 | $args{surgeon} = MsOffice::Word::Surgeon->new(docx => $docx); | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | # now call the regular Moose method | ||||||||||||||||||||||||||||||||
| 64 | 2 | return $class->$orig(%args); | |||||||||||||||||||||||||||||||
| 65 | 3 | }; | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 69 | # LAZY ATTRIBUTE CONSTRUCTORS | ||||||||||||||||||||||||||||||||
| 70 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub _engine { | ||||||||||||||||||||||||||||||
|
Subroutine
_engine: called CC=6 SCAR=20.5
| |||||||||||||||||||||||||||||||||
| 74 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # instantiate the templating engine | ||||||||||||||||||||||||||||||||
| 77 | 2 | my $engine_class = $self->engine_class; | |||||||||||||||||||||||||||||||
| 78 | 2 | my $engine; | |||||||||||||||||||||||||||||||
| 79 | my @load_errors; | ||||||||||||||||||||||||||||||||
| 80 | CLASS: | ||||||||||||||||||||||||||||||||
| 81 | 2 | for my $class ("MsOffice::Word::Template::Engine::$engine_class", $engine_class) { | |||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | eval "require $class; 1" or push @load_errors, $@ and next CLASS; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
eval "require $class; 1" || push(@load_errors, $@) && (next CLASS)
push @load_errors, $@ and next CLASS
MC/DC0%0/2
eval "require $class; 1" || push(@load_errors, $@) && (next CLASS)
eval "require $class; 1"
push @load_errors, $@
Truth table33%1/3
A || B
A= eval "require $class; 1"B= push @load_errors, $@
| |||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | $engine = $class->new(word_template => $self, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | $self->engine_args->@*) and last CLASS; | ||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | $engine or die "could not load engine class '$engine_class'", @load_errors; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 2 | return $engine; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 94 | # PROCESSING THE TEMPLATE | ||||||||||||||||||||||||||||||||
| 95 | #====================================================================== | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 2 | ▶ | sub process { | ||||||||||||||||||||||||||||||
|
Subroutine
process: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | 2 | my ($self, $vars) = @_; | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | # create a clone of the original | ||||||||||||||||||||||||||||||||
| 101 | 2 | my $new_doc = $self->surgeon->clone; | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # process each package part | ||||||||||||||||||||||||||||||||
| 104 | 2 | foreach my $part_name ($self->part_names->@*) { | |||||||||||||||||||||||||||||||
| 105 | 14 | my $new_doc_part = $new_doc->part($part_name); | |||||||||||||||||||||||||||||||
| 106 | 14 | my $new_contents = $self->engine->process_part($part_name, $new_doc_part, $vars); | |||||||||||||||||||||||||||||||
| 107 | 14 | $new_doc_part->contents($new_contents); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # process the property files (core.xml, app.xml. custom.xml -- if present in the original word template) | ||||||||||||||||||||||||||||||||
| 111 | 2 | foreach my $property_file ($self->property_files->@*) { | |||||||||||||||||||||||||||||||
| 112 | 6 | ▶ | if ($self->surgeon->zip->memberNamed($property_file)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | 4 | my $new_contents = $self->engine->process($property_file, $vars); | |||||||||||||||||||||||||||||||
| 114 | 4 | $new_doc->xml_member($property_file, $new_contents); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 2 | return $new_doc; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 3 | 1; | |||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||