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