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.02';
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 foreach my $style_name ( values %$transliteration_map ) {
100  
101 next if $style_name =~ /T\d+/;
102  
103 my $oodoc_style =
104 $oodoc_dest_document->textStyle($style_name)
105 or
106 $oodoc_dest_document->createStyle( $style_name =>
107 'class' => 'text',
108 'display-name' => "Default",
109 'family' => 'paragraph',
110 'next' => 'Text_20_body',
111 'parent' => 'Heading',
112 )
113 or
114 next;
115  
116 $oodoc_from_document->updateStyle( $style_name =>
117 'properties' => {
118 '-area' => $_,
119 %{$style_attributes->{$_}},
120 }
121 ) foreach qw/ paragraph text /;
122 }
123  
124 }
125  
126 # _getStyleNameTransliterationMap
127 #
128 # Returns a HashRef that explains how to transliterate autostyles, such that two
129 # documents can be merged without conflicting style names.
130 #
131 sub _getStyleNameTransliterationMap {
132 my $oodoc_dest_document = shift;
133 my $oodoc_from_document = shift;
134 my %params = @_;
135  
136 my $family_max = {};
137 my $transliteration_map = {};
138  
139 foreach my $family ( keys %stylefamily_map ) {
140 $family_max->{$family}->{dest} =
141 _maxUsedNumberAutoStyleFamily($oodoc_dest_document, $family);
142 $family_max->{$family}->{from} =
143 _maxUsedNumberAutoStyleFamily($oodoc_from_document, $family);
144  
145 $transliteration_map->{ $stylefamily_map{$family} . $_ } =
146 $stylefamily_map{$family} . ( $_ + $family_max->{$family}->{dest} )
147 for 1 .. $family_max->{$family}->{from}
148  
149 };
150  
151 # create aditional mappings, just in case we might need them
152 if ( $params{include_body_styles} ) {
153 foreach my $style_name ( qw/ Text_20_body First_20_paragraph /) {
154 my @elements = $oodoc_from_document->getBody->selectChildElements(
155 'text:p',,'text:style-name="'.$style_name.'"'
156 );
157 if ( @elements ) {
158 $transliteration_map->{$style_name} = 'P' .
159 ( $family_max->{'paragraph'}{dest} + ++$family_max->{'paragraph'}{from} )
160 }
161 }
162 }
163  
164 return $transliteration_map
165 }
166  
167 # _maxUsedNumberAutoStyleFamily
168 #
169 # returns the highest used numberr for a given style family (must be one of the
170 # stylefamily_map families)
171 #
172 sub _maxUsedNumberAutoStyleFamily {
173 my $oodoc_document = shift;
174 my $family = shift;
175  
176 my @oodoc_styles = $oodoc_document
177 ->getAutoStyleRoot
178 ->selectChildElements('(style:style|text:list-style)');
179  
180 return max (
181 map {
182 $oodoc_document->getAttribute($_, 'style:name') =~ /$stylefamily_map{$family}(\d+)/
183 } @oodoc_styles
184 ) || 0
185 }
186  
187 # _transliterateStyleNames
188 #
189 # Renames every Auto Style in the given OODoc Element and it's decendants
190 #
191 sub _transliterateStyleNames {
192 my $oodoc_element = shift;
193 my $transliteration_map = shift;
194  
195 _transliterateStyleNamesAtrr($oodoc_element, $transliteration_map, $_)
196 for qw/
197 text:style-name
198 style:name
199 style:list-style-name
200 /
201 ;
202  
203 return
204 }
205  
206 # _transliterateStyleNamesAtrr
207 #
208 # set the style name attributes to a new value, based on the transliteration_map
209 #
210 sub _transliterateStyleNamesAtrr{
211 my $oodoc_element = shift;
212 my $transliteration_map = shift;
213 my $attribute_name = shift;
214  
215 my $xpath = sprintf './/[@%s =~ /^([LPT]\d+|Text_20_body|First_20_paragraph)$/ ]', $attribute_name;
216 foreach my $node ( $oodoc_element->findnodes($xpath) ) {
217 my $stylename_old = $node->getAttribute($attribute_name);
218 my $stylename_new = $transliteration_map->{$stylename_old};
219 $node->setAttribute($attribute_name, $stylename_new);
220 }
221 }
222  
223 1;
224  
225 __END__
↑ Summary