| 1 | package MsOffice::Word::Template::Engine; | ||||||||||
| 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 | ▶ | use MooseX::AbstractMethod; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use MsOffice::Word::Surgeon::Utils qw(decode_entities); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use namespace::clean -except => 'meta'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | our $VERSION = '2.05'; | |||||||||
| 10 | |||||||||||
| 11 | #====================================================================== | ||||||||||
| 12 | # ATTRIBUTES | ||||||||||
| 13 | #====================================================================== | ||||||||||
| 14 | |||||||||||
| 15 | # passed through the constructor | ||||||||||
| 16 | 1 | has 'word_template' => (is => 'ro', isa => 'MsOffice::Word::Template', required => 1, weak_ref => 1); | |||||||||
| 17 | |||||||||||
| 18 | # lazily constructed, not received through the constructor | ||||||||||
| 19 | 1 | has 'xml_regexes' => (is => 'ro', isa => 'ArrayRef[RegexpRef]', | |||||||||
| 20 | lazy => 1, builder => '_xml_regexes', init_arg => undef); | ||||||||||
| 21 | 1 | has 'compiled_template' => (is => 'ro', isa => 'HashRef', | |||||||||
| 22 | lazy => 1, builder => '_compiled_template', init_arg => undef); | ||||||||||
| 23 | 1 | has '_constructor_args' => (is => 'bare', isa => 'HashRef', init_arg => undef); | |||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | #====================================================================== | ||||||||||
| 27 | # ABSTRACT METHODS -- to be defined in subclasses | ||||||||||
| 28 | #====================================================================== | ||||||||||
| 29 | |||||||||||
| 30 | 1 | abstract 'start_tag'; | |||||||||
| 31 | 1 | abstract 'end_tag'; | |||||||||
| 32 | 1 | abstract 'compile_template'; | |||||||||
| 33 | 1 | abstract 'process_part'; | |||||||||
| 34 | 1 | abstract 'process'; | |||||||||
| 35 | |||||||||||
| 36 | #====================================================================== | ||||||||||
| 37 | # GLOBALS | ||||||||||
| 38 | #====================================================================== | ||||||||||
| 39 | |||||||||||
| 40 | 1 | my $XML_COMMENT_FOR_MARKING_DIRECTIVES = '<!--TEMPLATE_DIRECTIVE_ABOVE-->'; | |||||||||
| 41 | |||||||||||
| 42 | #====================================================================== | ||||||||||
| 43 | # INSTANCE CONSTRUCTION | ||||||||||
| 44 | #====================================================================== | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 48 | 2 | my ($self, $args) = @_; | |||||||||
| 49 | 2 | $self->{_constructor_args} = $args; # stored to be available for lazy attr constructors in subclasses | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | #====================================================================== | ||||||||||
| 54 | # LAZY ATTRIBUTE CONSTRUCTORS | ||||||||||
| 55 | #====================================================================== | ||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub _xml_regexes { | ||||||||
|
Subroutine
_xml_regexes: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 2 | my ($self) = @_; | |||||||||
| 59 | |||||||||||
| 60 | # start and end character sequences for a template fragment | ||||||||||
| 61 | 2 | my $rx_start = quotemeta $self->start_tag; | |||||||||
| 62 | 2 | my $rx_end = quotemeta $self->end_tag; | |||||||||
| 63 | |||||||||||
| 64 | # Regexes for extracting template directives within the XML. | ||||||||||
| 65 | # Such directives are identified through a specific XML comment -- this comment is | ||||||||||
| 66 | # inserted by method "template_fragment_for_run()" below. | ||||||||||
| 67 | # The (*SKIP) instructions are used to avoid backtracking after a | ||||||||||
| 68 | # closing tag for the subexpression has been found. Otherwise the | ||||||||||
| 69 | # .*? inside could possibly match across boundaries of the current | ||||||||||
| 70 | # XML node, we don't want that. | ||||||||||
| 71 | |||||||||||
| 72 | # regex for matching directives to be treated outside the text flow. | ||||||||||
| 73 | 2 | my $rx_outside_text_flow = qr{ | |||||||||
| 74 | <w:r\b [^>]*> # start run node | ||||||||||
| 75 | (?: <w:rPr> .*? </w:rPr> (*SKIP) )? # optional run properties | ||||||||||
| 76 | <w:t\b [^>]*> # start text node | ||||||||||
| 77 | ($rx_start .*? $rx_end) (*SKIP) # template directive | ||||||||||
| 78 | $XML_COMMENT_FOR_MARKING_DIRECTIVES # specific XML comment | ||||||||||
| 79 | </w:t> # close text node | ||||||||||
| 80 | </w:r> # close run node | ||||||||||
| 81 | }sx; | ||||||||||
| 82 | |||||||||||
| 83 | # regex for matching paragraphs that contain only a directive | ||||||||||
| 84 | 2 | my $rx_paragraph = qr{ | |||||||||
| 85 | <w:p\b [^>]*> # start paragraph node | ||||||||||
| 86 | (?: <w:pPr> .*? </w:pPr> (*SKIP) )? # optional paragraph properties | ||||||||||
| 87 | $rx_outside_text_flow | ||||||||||
| 88 | </w:p> # close paragraph node | ||||||||||
| 89 | }sx; | ||||||||||
| 90 | |||||||||||
| 91 | # regex for matching table rows that contain only a directive in the first cell | ||||||||||
| 92 | 2 | my $rx_row = qr{ | |||||||||
| 93 | <w:tr\b [^>]*> # start row node | ||||||||||
| 94 | <w:tc\b [^>]*> # start cell node | ||||||||||
| 95 | (?:<w:tcPr> .*? </w:tcPr> (*SKIP) )? # cell properties | ||||||||||
| 96 | $rx_paragraph # paragraph in cell | ||||||||||
| 97 | </w:tc> # close cell node | ||||||||||
| 98 | (?:<w:tc> .*? </w:tc> (*SKIP) )* # ignore other cells on the same row | ||||||||||
| 99 | </w:tr> # close row node | ||||||||||
| 100 | }sx; | ||||||||||
| 101 | |||||||||||
| 102 | 2 | return [$rx_row, $rx_paragraph, $rx_outside_text_flow]; | |||||||||
| 103 | # Note : the order is important -- the most specific regex is tried first, the least specific is tried last | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | 1 | ▶ | sub _compiled_template { | ||||||||
|
Subroutine
_compiled_template: called CC=4 SCAR=13.9
| |||||||||||
| 109 | 1 | my ($self) = @_; | |||||||||
| 110 | |||||||||||
| 111 | 1 | my $surgeon = $self->word_template->surgeon; | |||||||||
| 112 | 1 | my %compiled_template; | |||||||||
| 113 | |||||||||||
| 114 | # build a compiled template for each document part | ||||||||||
| 115 | 1 | foreach my $part_name ($self->word_template->part_names->@*) { | |||||||||
| 116 | 7 | my $part = $surgeon->part($part_name); | |||||||||
| 117 | 7 | my $template_text = $self->template_text_for_part($part); | |||||||||
| 118 | 7 | $compiled_template{$part_name} = $self->compile_template($template_text); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | # build a compiled template for each property file (core.xml, app.xml, custom.xml) | ||||||||||
| 122 | 1 | foreach my $property_file ($self->word_template->property_files->@*) { | |||||||||
| 123 | 3 | ▶ | if ($surgeon->zip->memberNamed($property_file)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 2 | my $xml = $surgeon->xml_member($property_file); | |||||||||
| 125 | 2 | $compiled_template{$property_file} = $self->compile_template($xml); | |||||||||
| 126 | } | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 1 | return \%compiled_template; | |||||||||
| 130 | |||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | #====================================================================== | ||||||||||
| 138 | # COMPILING INNER TEMPLATES | ||||||||||
| 139 | #====================================================================== | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | |||||||||||
| 143 | 8 | ▶ | sub template_text_for_part { | ||||||||
|
Subroutine
template_text_for_part: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 144 | 8 | my ($self, $part) = @_; | |||||||||
| 145 | |||||||||||
| 146 | # assemble template fragments from all runs in the part into a global template text | ||||||||||
| 147 | 8 | $part->cleanup_XML; | |||||||||
| 148 | 8 | my @template_fragments = map {$self->_template_fragment_for_run($_)} $part->runs->@*; | |||||||||
| 149 | 8 | my $template_text = join "", @template_fragments; | |||||||||
| 150 | |||||||||||
| 151 | # remove markup around directives, successively for table rows, for paragraphs, and finally | ||||||||||
| 152 | # for remaining directives embedded within text runs. | ||||||||||
| 153 | 8 | $template_text =~ s/$_/$1/g foreach $self->xml_regexes->@*; | |||||||||
| 154 | |||||||||||
| 155 | 8 | return $template_text; | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | |||||||||||
| 159 | 82 | ▶ | sub _template_fragment_for_run { # given an instance of Surgeon::Run, build a template fragment | ||||||||
|
Subroutine
_template_fragment_for_run: called CC=6 SCAR=17.9
| |||||||||||
| 160 | 82 | my ($self, $run) = @_; | |||||||||
| 161 | |||||||||||
| 162 | 82 | my $props = $run->props; | |||||||||
| 163 | 82 | my $data_color = $self->word_template->data_color; | |||||||||
| 164 | 82 | my $control_color = $self->word_template->control_color; | |||||||||
| 165 | |||||||||||
| 166 | # if this run is highlighted in data or control color, it must be translated into a template directive | ||||||||||
| 167 | 82 | ▶ | if ($props =~ s{<w:highlight w:val="($data_color|$control_color)"/>}{}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | 41 | my $color = $1; | |||||||||
| 169 | 41 | my $xml = $run->xml_before; | |||||||||
| 170 | |||||||||||
| 171 | # re-build the run, removing the highlight, and adding the start/end tags for the template engine | ||||||||||
| 172 | 41 | my $inner_texts = $run->inner_texts; | |||||||||
| 173 | 41 | ▶ | if (@$inner_texts) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 41 | $xml .= "<w:r>"; # opening XML tag for run node | |||||||||
| 175 | 41 | ▶ | $xml .= "<w:rPr>" . $props . "</w:rPr>" if $props; # optional run properties | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 41 | $xml .= "<w:t>"; # opening XML tag for text node | |||||||||
| 177 | 41 | $xml .= $self->start_tag; # start a template directive | |||||||||
| 178 | 41 | foreach my $inner_text (@$inner_texts) { # loop over text nodes | |||||||||
| 179 | 43 | my $txt = $inner_text->literal_text; # just take inner literal text | |||||||||
| 180 | 43 | decode_entities($txt); | |||||||||
| 181 | 43 | $xml .= $txt . "\n"; | |||||||||
| 182 | # NOTE : adding "\n" because the template parser may need them for identifying end of comments | ||||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 41 | $xml .= $self->end_tag; # end of template directive | |||||||||
| 186 | 41 | ▶ | $xml .= $XML_COMMENT_FOR_MARKING_DIRECTIVES | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | if $color eq $control_color; # XML comment for marking | ||||||||||
| 188 | 41 | $xml .= "</w:t>"; # closing XML tag for text node | |||||||||
| 189 | 41 | $xml .= "</w:r>"; # closing XML tag for run node | |||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | 41 | return $xml; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | # otherwise this run is just regular MsWord content | ||||||||||
| 196 | else { | ||||||||||
| 197 | 41 | return $run->as_xml; | |||||||||
| 198 | } | ||||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | |||||||||||
| 202 | |||||||||||
| 203 | |||||||||||
| 204 | 1 | 1; | |||||||||
| 205 | |||||||||||
| 206 | __END__ | ||||||||||