lpOD/TextElement.pm

filter: Statementstmt 100% 8 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 11 / 11 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lpOD.pm ↑ Summary
1 #=============================================================================
2 #
3 # Copyright (c) 2010 Ars Aperta, Itaapy, Pierlis, Talend.
4 # Copyright (c) 2011 Jean-Marie Gouarné.
5 # Author: Jean-Marie Gouarné <jean.marie.gouarne@online.fr>
6 #
7 #=============================================================================
8 2 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #=============================================================================
11 # Text Element classes
12 #=============================================================================
13 package ODF::lpOD::TextElement;
14 2 ▶use base 'ODF::lpOD::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 our $VERSION = '1.006';
16 use constant PACKAGE_DATE => '2014-04-30T08:30:28';
17 use ODF::lpOD::Common;
18 #=============================================================================
19  
20 BEGIN {
21 *set_link = *set_hyperlink
22 }
23  
24 #-----------------------------------------------------------------------------
25  
26 our $RECURSIVE_EXPORT = FALSE;
27  
28 sub set_recursive_export
29 {
30 my $caller = shift;
31 $RECURSIVE_EXPORT = is_true(shift);
32 }
33  
34 #--- constructor -------------------------------------------------------------
35  
36 sub _create { ODF::lpOD::TextElement->create(@_) }
37  
38 #-----------------------------------------------------------------------------
39  
40 sub create
41 {
42 my $caller = shift;
43 my %opt = process_options
44 (
45 tag => undef,
46 style => undef,
47 text => undef,
48 @_
49 );
50 my $tag = $opt{tag}; $tag = 'text:' . $tag unless $tag =~ /:/;
51 my $e = ODF::lpOD::Element->create($tag) or return undef;
52 if ($tag eq 'text:h')
53 {
54 $e->set_attribute('outline level', $opt{level} // 1);
55 $e->set_attribute('restart numbering', 'true')
56 if is_true($opt{'restart_numbering'});
57 $e->set_attribute('start value', $opt{start_value})
58 if defined $opt{start_value};
59 $e->set_attribute('is list header', 'true')
60 if defined $opt{suppress_numbering};
61 }
62 $e->set_style($opt{style})
63 if defined $opt{style};
64 $e->set_text($opt{text})
65 if defined $opt{text};
66  
67 return $e;
68 }
69  
70 #=== common tools ============================================================
71  
72 sub set_spaces
73 {
74 my $self = shift;
75 my $count = shift or return undef;
76 my %opt = @_;
77  
78 my $s = $self->insert_element('s', %opt);
79 $s->set_attribute('c', $count);
80 return $s;
81 }
82  
83 sub set_line_break
84 {
85 my $self = shift;
86 return $self->insert_element('line break', @_);
87 }
88  
89 sub set_tab_stop
90 {
91 my $self = shift;
92 return $self->insert_element('tab', @_);
93 }
94  
95 #--- split the content with new child elements -------------------------------
96  
97 sub split_content
98 {
99 my $self = shift;
100 my %opt = process_options
101 (
102 tag => undef,
103 search => undef,
104 offset => undef,
105 length => undef,
106 content => undef,
107 insert => undef,
108 attributes => {},
109 @_
110 );
111 if (defined $opt{search} && defined $opt{length})
112 {
113 alert "Conflicting search and length parameters";
114 return FALSE;
115 }
116 my $search = $opt{search};
117 $opt{repeat} //= (defined $search && ! defined $opt{offset});
118 if (is_true($opt{repeat}))
119 {
120 delete $opt{repeat};
121 my $start = $opt{start_mark};
122 if ($opt{offset})
123 {
124 $start = $self->split_content(%opt);
125 }
126 $opt{offset} = 0;
127 my @elts = ();
128 do {
129 $opt{start_mark} = $start;
130 $start = $self->split_content(%opt);
131 push @elts, $start;
132 }
133 while ($start);
134 return wantarray ? @elts : $elts[0];
135 }
136 my $tag = $self->normalize_name($opt{tag});
137 if (defined $opt{start_mark} || defined $opt{end_mark})
138 {
139 $opt{offset} //= 0;
140 }
141 my $position = $opt{offset} || 0;
142 if ($position eq 'end')
143 {
144 my $e = $self->append_element($tag);
145 $e->set_attributes($opt{attributes});
146 $e->set_text($opt{text});
147 $e->set_class;
148 return $e;
149 }
150 my $range = $opt{length};
151 if ($position == 0 && ! defined $search && ! defined $range)
152 {
153 my $e = $self->insert_element($tag);
154 $e->set_attributes($opt{attributes});
155 $e->set_text($opt{text});
156 $e->set_class;
157 return $e;
158 }
159 my %r = $self->search
160 (
161 $search,
162 offset => $position,
163 range => $range,
164 backward => $opt{backward},
165 start_mark => $opt{start_mark},
166 end_mark => $opt{end_mark}
167 );
168 if (defined $r{segment})
169 {
170 my $e = ODF::lpOD::Element->create($tag);
171 unless ($opt{insert})
172 {
173 my $t = $r{segment}->_get_text;
174 $range = $r{end} - $r{offset}
175 if defined $search;
176 if (defined $range)
177 {
178 substr($t, $r{offset}, $range, "")
179 }
180 else
181 {
182 $t = substr($t, 0, $r{offset});
183 }
184 $r{segment}->_set_text($t);
185 $e->set_text($opt{text} // $r{match});
186 if (
187 (
188 defined $opt{offset}
189 &&
190 $opt{offset} >= 0
191 )
192 ||
193 defined $search
194 )
195 {
196 $e->paste_within
197 ($r{segment}, $r{offset});
198 }
199 else
200 {
201 if ($r{end} < 0)
202 {
203 $e->paste_within
204 ($r{segment}, $r{end});
205 }
206 else
207 {
208 $e->paste_after($r{segment});
209 }
210 }
211  
212 }
213 else
214 {
215 my $p = $opt{insert} eq 'after' ?
216 $r{end} : $r{offset};
217 $e->paste_within($r{segment}, $p);
218 $e->set_text($opt{text});
219 }
220  
221 $e->set_attributes($opt{attributes});
222 $e->set_class;
223 return $e;
224 }
225 return undef;
226 }
227  
228 #--- lpOD-specific bookmark setting ------------------------------------------
229  
230 sub set_lpod_mark
231 {
232 my $self = shift;
233 my %opt = @_;
234 my $id;
235 if (defined $opt{id})
236 {
237 $id = $opt{id}; delete $opt{id};
238 }
239 else
240 {
241 $id = lpod_common->new_id;
242 }
243 $opt{tag} = $ODF::lpOD::Common::LPOD_MARK;
244 $opt{attributes} =
245 {
246 $ODF::lpOD::Common::LPOD_ID => $id
247 };
248 return $self->split_content(%opt);
249 }
250  
251 #--- common bookmark, index mark setting tool --------------------------------
252  
253 sub set_position_mark
254 {
255 my $self = shift;
256 my $tag = shift;
257 my %opt =
258 (
259 offset => undef,
260 before => undef,
261 after => undef,
262 @_
263 );
264 if (defined $opt{before} && defined $opt{after})
265 {
266 alert "Conflicting before and after parameters";
267 return FALSE;
268 }
269  
270 $opt{offset} //= 0;
271 $opt{search} = $opt{before} // $opt{after};
272 if (defined $opt{after}) { $opt{insert} = 'after' }
273 else { $opt{insert} = 'before' }
274 $opt{length} = defined $opt{search} ? undef : 0;
275  
276 delete @opt{qw(before after)};
277 $opt{tag} = $tag;
278 return $self->split_content(%opt);
279 }
280  
281 sub set_text_mark
282 {
283 my $self = shift;
284 my %opt = @_;
285 if (defined $opt{length} && $opt{length} > 0)
286 {
287 unless (ref $opt{offset})
288 {
289 my $start = $opt{offset} // 0;
290 my $end = $start + $opt{length};
291 $opt{offset} = [ $start, $end ];
292 }
293 delete $opt{length};
294 }
295 if (defined $opt{content} || ref $opt{offset})
296 {
297 my $content = $opt{content};
298 my ($p1, $p2, $range_end);
299 if (ref $opt{offset})
300 {
301 $p1 = $opt{offset}[0];
302 $p2 = $opt{offset}[1];
303 $range_end = $self->set_lpod_mark
304 (offset => $p2, length => 0)
305 if defined $p2 && defined $opt{content};
306 $opt{end_mark} = $range_end if $range_end;
307 }
308 else
309 {
310 $p1 = $opt{offset};
311 $p2 = $opt{offset};
312 }
313 delete @opt{qw(content offset)};
314 $opt{offset} = $p1;
315 $opt{before} = $content if defined $content;
316 $opt{role} = 'start';
317 my $start = $self->set_text_mark(%opt)
318 or return FALSE;
319 $opt{offset} = $p2;
320 if (defined $content)
321 {
322 $opt{after} = $content;
323 delete $opt{before};
324 }
325 $opt{role} = 'end';
326 my $end = $self->set_text_mark(%opt)
327 or return FALSE;
328 unless ($start->before($end))
329 {
330 $start->delete; $end->delete;
331 alert("Start is not before end");
332 return FALSE;
333 }
334 if ($range_end)
335 {
336 $range_end->delete(); $self->normalize;
337 }
338 return wantarray ? $start : ($start, $end);
339 }
340  
341 my $tag;
342 if (!defined $opt{role})
343 {
344 $tag = $opt{tag};
345 }
346 elsif ($opt{role} =~ /^(start|end)$/)
347 {
348 $tag = $opt{tag} . '-' . $_;
349 delete $opt{role};
350 }
351 else
352 {
353 alert("Wrong role = $_ option");
354 return undef;
355 }
356  
357 delete $opt{tag};
358 return $self->set_position_mark($tag, %opt);
359 }
360  
361 #=== text content handling ===================================================
362  
363 sub set_text
364 {
365 my $self = shift;
366 my $text = shift;
367 return $self->SUPER::set_text($text, @_) unless $text;
368 return $self->_set_text($text) if caller() eq 'XML::Twig::Elt';
369  
370 $self->_set_text("");
371 my @lines = split("\n", $text, -1);
372 while (@lines)
373 {
374 my $line = shift @lines;
375 my @columns = split("\t", $line, -1);
376 while (@columns)
377 {
378 my $column = shift @columns;
379 my @words = split(/(\s\s+)/, $column, -1);
380 foreach my $word (@words)
381 {
382 my $l = length($word);
383 if ($word =~ m/^ +$/)
384 {
385 $self->set_spaces
386 ($l, position => 'LAST_CHILD');
387 }
388 elsif ($l > 0)
389 {
390 my $n = $self->append_element
391 (TEXT_SEGMENT);
392 $n->set_text($word);
393 }
394 }
395 $self->append_element('tab') if @columns;
396 }
397 $self->append_element('line break') if @lines;
398 }
399 $self->normalize;
400 bless $_, 'ODF::lpOD::TextNode' for $self->children('#PCDATA');
401 return TRUE;
402 }
403  
404 sub get_text
405 {
406 my $self = shift;
407 my %opt = @_;
408 return $self->ODF::lpOD::TextNode::get_text
409 if $self->is(TEXT_SEGMENT);
410 $opt{recursive} //= $RECURSIVE_EXPORT;
411 my $text = undef;
412 NODE: foreach my $node ($self->children)
413 {
414 if (
415 $node->isa('ODF::lpOD::TextNode')
416 or
417 $node->isa('ODF::lpOD::TextElement')
418 )
419 {
420 my $t = $node->get_text(%opt);
421 $text .= $t if defined $t;
422 }
423 else
424 {
425 if ($node->get_tag eq 'text:tab')
426 {
427 $text .= $ODF::lpOD::Common::TAB_STOP;
428 }
429 elsif ($node->get_tag eq 'text:line-break')
430 {
431 $text .= $ODF::lpOD::Common::LINE_BREAK;
432 }
433 elsif ($node->get_tag eq 'text:s')
434 {
435 my $c = $node->get_attribute('c') // 1;
436 $text .= " " while $c-- > 0;
437 }
438 else
439 {
440 if (is_true($opt{recursive}))
441 {
442 my $t = $node->SUPER::get_text(%opt);
443 $text .= $t if defined $t;
444 }
445 }
446 }
447 }
448  
449 return $text;
450 }
451  
452 #=== text internal markup ===================================================
453  
454 sub set_span
455 {
456 my $self = shift;
457 my %opt = @_;
458 unless ($opt{style})
459 {
460 alert("Missing style name");
461 return FALSE;
462 }
463 $opt{search} = $opt{filter} if exists $opt{filter};
464 $opt{attributes} = { 'style name' => $opt{style} };
465 delete @opt{qw(filter style)};
466 unless (defined $opt{length}) { $opt{search} //= ".*" }
467 else { $opt{offset} //= 0 }
468 return $self->split_content(tag => 'span', %opt);
469 }
470  
471 sub remove_spans
472 {
473 my $self = shift;
474  
475 my $tmp = $self->clone;
476 $self->delete_children;
477 my $count = 0;
478 foreach my $e ($tmp->descendants)
479 {
480 unless ($e->is('text:span'))
481 {
482 $e->move(last_child => $self);
483 }
484 else
485 {
486 $count++;
487 }
488 }
489 $tmp->delete;
490 return $count;
491 }
492  
493 sub set_hyperlink
494 {
495 my $self = shift;
496 my %opt = process_options(@_);
497 my $url = $opt{url};
498 delete $opt{url};
499 unless ($url)
500 {
501 alert("Missing URL"); return FALSE;
502 }
503 $opt{search} = $opt{filter} if exists $opt{filter};
504 $opt{attributes} =
505 {
506 'xlink:href' => $url,
507 'office:name' => $opt{name},
508 'office:title' => $opt{title},
509 'style name' => $opt{style},
510 'visited style name' => $opt{visited_style}
511 };
512 delete @opt{qw(filter name title style visited_style)};
513 unless (defined $opt{length}) { $opt{search} //= ".*" }
514 else { $opt{offset} //= 0 }
515 return $self->split_content(tag => 'a', %opt);
516 }
517  
518 sub set_place_mark
519 {
520 my $self = shift;
521 my $type = shift;
522 my $name = shift;
523 unless ($name)
524 {
525 alert "Missing $type name"; return FALSE;
526 }
527  
528 return $self->set_text_mark
529 (
530 tag => $type,
531 attributes =>
532 {
533 name => $name
534 },
535 @_
536 );
537 }
538  
539 sub set_bookmark
540 {
541 my $self = shift;
542 return $self->set_place_mark('bookmark', @_);
543 }
544  
545 sub set_reference_mark
546 {
547 my $self = shift;
548 return $self->set_place_mark('reference mark', @_);
549 }
550  
551 sub set_reference
552 {
553 my $self = shift;
554 my %opt = @_;
555 $opt{type} //= 'reference';
556 my $tag = 'text:' . $opt{type} . '-ref';
557 $opt{attributes} =
558 {
559 ref_name => $opt{name},
560 reference_format => $opt{format}
561 };
562 delete @opt{qw(type name format)};
563 return $self->set_position_mark($tag, %opt);
564 }
565  
566 sub set_index_mark
567 {
568 my $self = shift;
569 my $text = shift;
570  
571 unless ($text)
572 {
573 alert "Missing index entry text";
574 return FALSE;
575 }
576  
577 my %opt = process_options (@_);
578  
579 if ($opt{index_name})
580 {
581 $opt{type} ||= 'user';
582 unless ($opt{type} eq 'user')
583 {
584 alert "Index mark type must be user";
585 return FALSE;
586 }
587 }
588 else
589 {
590 $opt{type} ||= 'lexical';
591 }
592 my $tag;
593 my %attr = $opt{attributes} ? %{$opt{attributes}} : ();
594 if ($opt{type} eq "lexical" || $opt{type} eq "alphabetical")
595 {
596 $tag = 'alphabetical index mark';
597 }
598 elsif ($opt{type} eq 'toc')
599 {
600 $tag = 'toc mark';
601 $attr{'outline level'} = $opt{level} // 1;
602 }
603 elsif ($opt{type} eq 'user')
604 {
605 unless ($opt{index_name})
606 {
607 alert "Missing index name";
608 return FALSE;
609 }
610 $tag = 'user index mark';
611 $attr{'outline level'} = $opt{level} // 1;
612 }
613 else
614 {
615 alert "Wrong index mark type ($opt{type})";
616 return FALSE
617 }
618  
619 if (defined $opt{content} || ref $opt{offset} || $opt{role})
620 { # it's a range index mark
621 $attr{'id'} = $text;
622 }
623 else
624 {
625 $attr{'string value'} = $text;
626 }
627  
628 delete @opt{qw(type index_name level attributes)};
629 $opt{attributes} = {%attr};
630 return $self->set_text_mark(tag => $tag, %opt);
631 }
632  
633 sub set_bibliography_mark
634 {
635 my $self = shift;
636 my %opt = process_options(@_);
637  
638 my $type_ok;
639 foreach my $k (keys %opt)
640 {
641 if (ref $opt{$k} || $k eq 'content' || $k eq 'role')
642 {
643 alert "Not allowed option";
644 delete $opt{$k};
645 next;
646 }
647 unless (
648 $k =~ /^(before|after|offset|start_mark|end_mark)$/
649 )
650 {
651 if ($k eq 'type')
652 {
653 $type_ok = TRUE;
654 $k = 'bibliography type';
655 }
656 $opt{attributes}{$k} = $opt{$k};
657 delete $opt{$k};
658 }
659 }
660 alert "Missing type parameter" unless $type_ok;
661  
662 return $self->set_position_mark('bibliography mark', %opt);
663 }
664  
665 #=== text notes ==============================================================
666  
667 sub set_note
668 {
669 my $self = shift;
670 my $id = shift;
671 unless ($id)
672 {
673 alert "Missing note identifier"; return FALSE;
674 }
675 my %opt = process_options(@_);
676 $opt{attributes} =
677 {
678 'id' => $id,
679 'note class' => $opt{class} || $opt{note_class}
680 || 'footnote'
681 };
682 my $style = $opt{style};
683 my $text = $opt{text};
684 my $body = $opt{body};
685 my $citation = $opt{citation};
686 my $label = $opt{label};
687 delete @opt{qw(note_class style text body citation label)};
688  
689 my $note = $self->set_position_mark('note', %opt);
690 $note->set_citation($citation, $label);
691 $note->{style} = $style;
692 if ($body)
693 {
694 $note->set_body(@{$body});
695 }
696 else
697 {
698 $note->set_body($text);
699 }
700  
701 return $note;
702 }
703  
704 sub set_annotation
705 {
706 my $self = shift;
707 my %opt = process_options(@_);
708 my $date = $opt{date};
709 my $author = $opt{author};
710 my $style = $opt{style};
711 my $content = $opt{content};
712 unshift @$content, $opt{text} if defined $opt{text};
713 delete @opt{qw(date author style content text)};
714 my $a = $self->set_position_mark('office:annotation', %opt);
715 $a->set_date($date);
716 $a->set_author($author);
717 $a->set_style($style);
718 $a->set_content(@$content) if $content;
719 return $a;
720 }
721  
722 #=== text fields =============================================================
723  
724 sub set_field
725 {
726 my $self = shift;
727 my $type = shift;
728 unless ($type)
729 {
730 alert "Missing field type"; return undef;
731 }
732 my %opt = process_options(@_);
733 $opt{search} //= $opt{replace}; delete $opt{replace};
734 $type = 'user field get' if $type eq 'variable';
735 if ($type =~ /^user field/)
736 {
737 unless ($opt{name})
738 {
739 alert "Missing associated variable name";
740 return undef;
741 }
742 }
743 else
744 {
745 unless (ODF::lpOD::TextField::check_type($type))
746 {
747 alert "Unsupported field type"; return undef;
748 }
749 }
750 OPTION: foreach my $k (keys %opt)
751 {
752 if (ref $opt{$k} || ($k eq 'role'))
753 {
754 delete $opt{$k};
755 next;
756 }
757 unless (
758 $k =~ /^(before|after|offset|length|start_mark|end_mark|search)$/
759 )
760 {
761 if ($k eq 'fixed')
762 {
763 $opt{attributes}{$k} =
764 odf_boolean($opt{$k});
765 }
766 elsif ($k eq 'style')
767 {
768 my $a = 'style:data-style-name';
769 $opt{attributes}{$a} = $opt{$k};
770 }
771 else
772 {
773 $opt{attributes}{$k} = $opt{$k};
774 }
775 delete $opt{$k};
776 }
777 }
778 my $tag = 'text:' . $type;
779 my $field;
780 if (defined $opt{search} || defined $opt{length})
781 {
782 $opt{text} = '';
783 $field = $self->split_content(tag => $tag, %opt);
784 }
785 else
786 {
787 $field = $self->set_position_mark($tag , %opt);
788 }
789 return $field;
790 }
791  
792 #=============================================================================
793 package ODF::lpOD::TextHyperlink;
794 use base 'ODF::lpOD::TextElement';
795 our $VERSION = '1.001';
796 use constant PACKAGE_DATE => '2011-08-04T20:52:37';
797 use ODF::lpOD::Common;
798 #-----------------------------------------------------------------------------
799  
800 sub set_type
801 {
802 my $self = shift;
803 return $self->get_attribute('xlink:type');
804 }
805  
806 sub get_type
807 {
808 my $self = shift;
809 return $self->get_attribute('xlink:type');
810 }
811  
812 sub set_style {}
813 sub get_style {}
814  
815 #=============================================================================
816 package ODF::lpOD::Paragraph;
817 use base 'ODF::lpOD::TextElement';
818 our $VERSION = '1.001';
819 use constant PACKAGE_DATE => '2010-12-29T22:28:58';
820 use ODF::lpOD::Common;
821 #--- constructor -------------------------------------------------------------
822  
823 sub _create { ODF::lpOD::Paragraph->create(@_) }
824  
825 #-----------------------------------------------------------------------------
826  
827 sub create
828 {
829 my $caller = shift;
830 return ODF::lpOD::TextElement->create(tag => 'p', @_);
831 }
832  
833 #=============================================================================
834 package ODF::lpOD::Heading;
835 use base 'ODF::lpOD::Paragraph';
836 our $VERSION = '1.001';
837 use constant PACKAGE_DATE => '2010-12-29T22:30:12';
838 use ODF::lpOD::Common;
839 #--- constructor -------------------------------------------------------------
840  
841 sub _create { ODF::lpOD::Heading->create(@_) }
842  
843 #-----------------------------------------------------------------------------
844  
845 sub create
846 {
847 my $caller = shift;
848 return ODF::lpOD::TextElement->create(tag => 'h', @_);
849 }
850  
851 #--- attribute accessors -----------------------------------------------------
852  
853 sub get_level
854 {
855 my $self = shift;
856 return $self->get_attribute('outline level');
857 }
858  
859 sub set_level
860 {
861 my $self = shift;
862 return $self->set_attribute('outline level', @_);
863 }
864  
865 sub get_suppress_numbering
866 {
867 my $self = shift;
868 return $self->get_boolean_attribute('is list header');
869 }
870  
871 sub set_suppress_numbering
872 {
873 my $self = shift;
874 return $self->set_boolean_attribute('is list header', shift);
875 }
876  
877 sub set_start_value
878 {
879 my $self = shift;
880 my $number = shift;
881 unless ($number >= 0)
882 {
883 alert('Wrong start value');
884 return FALSE;
885 }
886 $self->set_attribute('restart numbering', TRUE);
887 $self->set_attribute('start value', $number);
888 }
889  
890 #=============================================================================
891 1;
« lpOD.pm ↑ Summary