blib/lib/OpenOffice/OODoc/InsertDocument.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package OpenOffice::OODoc::InsertDocument;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Insert, merge or append OpenOffice::OODoc objects
7 #
8 # LICENSE: This file is covered by the EUPL license, please see the
9 # LICENSE file in this repository for more information
10  
11 our $VERSION = '0.03';
12  
13 2 ▶use OpenOffice::OODoc;
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 use List::Util 'max';
16 use Readonly;
17  
18  
19 # patch the original OpenOffice::OODoc module and add these subroutines
20 #
21 *OpenOffice::OODoc::Document::insertDocument =
22 \&OpenOffice::OODoc::InsertDocument::insertDocument;
23  
24 Readonly my %stylefamily_map => (
25 'paragraph' => 'P',
26 'text' => 'T',
27 'list-style' => 'L',
28 );
29  
30  
31 sub appendDocument {
32 ...
33 }
34  
35  
36 sub insertDocument {
37 my $oodoc_dest_document = shift; # or $self
38 my $oodoc_path = shift;
39 my $position = shift;
40 my $oodoc_from_document = shift;
41  
42 # if we have a $oodoc_dest_test_style, it means we have a custom, local
43 # style, otherwise we are using this insertDocument in 'default' styling.
44 my $oodoc_dest_text_style_name =
45 $oodoc_dest_document->textStyle($oodoc_path);
46 my $oodoc_dest_text_style =
47 $oodoc_dest_document->getStyleElement($oodoc_dest_text_style_name);
48  
49 my $transliteration_map =
50 _getStyleNameTransliterationMap(
51 $oodoc_dest_document,
52 $oodoc_from_document,
53 include_body_styles => defined($oodoc_dest_text_style),
54 );
55  
56 # rename, select, and paste the auto-style definitions
57 _transliterateStyleNames(
58 $oodoc_from_document->getAutoStyleRoot,
59 $transliteration_map,
60 );
61 my @oodoc_from_auto_style_nodes =
62 $oodoc_from_document->getAutoStyleRoot->selectChildElements(
63 '(style:style|text:list-style)'
64 );
65 foreach my $oodoc_from_auto_style_node (@oodoc_from_auto_style_nodes) {
66 $oodoc_from_auto_style_node->paste_last_child(
67 $oodoc_dest_document->getAutoStyleRoot );
68 }
69  
70 # rename, select, and paste the document's text elements
71 _transliterateStyleNames(
72 $oodoc_from_document->getBody,
73 $transliteration_map,
74 );
75 my @oodoc_from_text_elements =
76 $oodoc_from_document->getBody->selectChildElements(
77 'text:(p|h|list)'
78 );
79 foreach my $oodoc_from_text_element (@oodoc_from_text_elements) {
80 $oodoc_dest_document->insertElement( $oodoc_path,
81 $oodoc_from_text_element, position => $position, );
82 }
83  
84 return unless defined($oodoc_dest_text_style);
85  
86 # update or create styles
87 my $style_attributes = { text => {}, paragraph => {} };
88 foreach my $area ( keys %$style_attributes ) {
89 my $style_property = "(style:$area-properties)";
90 my ($style_element) =
91 $oodoc_dest_text_style->selectChildElements($style_property)
92 or
93 next;
94  
95 $style_attributes->{$area} =
96 { $oodoc_dest_document->getStyleAttributes($style_element) }->{references} || {};
97 };
98  
99 $style_attributes->{style} = { $oodoc_dest_text_style->getAttributes() };
100  
101 foreach my $style_name ( values %$transliteration_map ) {
102  
103 next if $style_name =~ /T\d+/;
104  
105 my $oodoc_style =
106 $oodoc_dest_document->textStyle($style_name)
107 or
108 $oodoc_dest_document->createStyle( $style_name =>
109 'class' => 'text',
110 'display-name' => "Default",
111 'family' => 'paragraph',
112 'parent' =>
113 $style_attributes->{style}->{'style:parent-style-name'}
114 ||
115 'Standard',
116 )
117 or
118 next;
119  
120 $oodoc_from_document->updateStyle( $style_name =>
121 'properties' => {
122 '-area' => $_,
123 %{$style_attributes->{$_}},
124 }
125 ) foreach qw/ paragraph text /;
126 }
127  
128 }
129  
130 # _getStyleNameTransliterationMap
131 #
132 # Returns a HashRef that explains how to transliterate autostyles, such that two
133 # documents can be merged without conflicting style names.
134 #
135 sub _getStyleNameTransliterationMap {
136 my $oodoc_dest_document = shift;
137 my $oodoc_from_document = shift;
138 my %params = @_;
139  
140 my $family_max = {};
141 my $transliteration_map = {};
142  
143 foreach my $family ( keys %stylefamily_map ) {
144 $family_max->{$family}->{dest} =
145 _maxUsedNumberAutoStyleFamily($oodoc_dest_document, $family);
146 $family_max->{$family}->{from} =
147 _maxUsedNumberAutoStyleFamily($oodoc_from_document, $family);
148  
149 $transliteration_map->{ $stylefamily_map{$family} . $_ } =
150 $stylefamily_map{$family} . ( $_ + $family_max->{$family}->{dest} )
151 for 1 .. $family_max->{$family}->{from}
152  
153 };
154  
155 # create aditional mappings, just in case we might need them
156 if ( $params{include_body_styles} ) {
157 foreach my $style_name ( qw/ Text_20_body First_20_paragraph /) {
158 my @elements = $oodoc_from_document->getBody->selectChildElements(
159 'text:p',,'text:style-name="'.$style_name.'"'
160 );
161 if ( @elements ) {
162 $transliteration_map->{$style_name} = 'P' .
163 ( $family_max->{'paragraph'}{dest} + ++$family_max->{'paragraph'}{from} )
164 }
165 }
166 }
167  
168 return $transliteration_map
169 }
170  
171 # _maxUsedNumberAutoStyleFamily
172 #
173 # returns the highest used numberr for a given style family (must be one of the
174 # stylefamily_map families)
175 #
176 sub _maxUsedNumberAutoStyleFamily {
177 my $oodoc_document = shift;
178 my $family = shift;
179  
180 my @oodoc_styles = $oodoc_document
181 ->getAutoStyleRoot
182 ->selectChildElements('(style:style|text:list-style)');
183  
184 return max (
185 map {
186 $oodoc_document->getAttribute($_, 'style:name') =~ /$stylefamily_map{$family}(\d+)/
187 } @oodoc_styles
188 ) || 0
189 }
190  
191 # _transliterateStyleNames
192 #
193 # Renames every Auto Style in the given OODoc Element and it's decendants
194 #
195 sub _transliterateStyleNames {
196 my $oodoc_element = shift;
197 my $transliteration_map = shift;
198  
199 _transliterateStyleNamesAtrr($oodoc_element, $transliteration_map, $_)
200 for qw/
201 text:style-name
202 style:name
203 style:list-style-name
204 /
205 ;
206  
207 return
208 }
209  
210 # _transliterateStyleNamesAtrr
211 #
212 # set the style name attributes to a new value, based on the transliteration_map
213 #
214 sub _transliterateStyleNamesAtrr{
215 my $oodoc_element = shift;
216 my $transliteration_map = shift;
217 my $attribute_name = shift;
218  
219 my $xpath = sprintf './/[@%s =~ /^([LPT]\d+|Text_20_body|First_20_paragraph)$/ ]', $attribute_name;
220 foreach my $node ( $oodoc_element->findnodes($xpath) ) {
221 my $stylename_old = $node->getAttribute($attribute_name);
222 my $stylename_new = $transliteration_map->{$stylename_old};
223 $node->setAttribute($attribute_name, $stylename_new);
224 }
225 }
226  
227 1;
228  
229 __END__
↑ Summary