lib/PDF/Boxer/SpecParser.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
« lib/PDF/Boxer.pm ↑ Summary
1 package PDF::Boxer::SpecParser;
2 {
3 $PDF::Boxer::SpecParser::VERSION = '0.004';
4 }
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Convert markup to Perl data
7  
8 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 has 'clean_whitespace' => ( isa => 'Bool', is => 'ro', default => 1 );
13  
14 has 'xml_parser' => ( isa => 'XML::Parser', is => 'ro', lazy_build => 1 );
15  
16 sub _build_xml_parser{
17 XML::Parser->new(Style => 'Tree');
18 }
19  
20 sub parse{
21 my ($self, $xml) = @_;
22 my $data = $self->xml_parser->parse($xml);
23  
24 my $spec = {};
25 $self->mangle_spec($spec, $data);
26 $spec = $spec->{children}[0];
27  
28 return $spec;
29 }
30  
31 sub mangle_spec{
32 my ($self, $spec, $data) = @_;
33 while(@$data){
34 my $tag = shift @$data;
35 my $element = shift @$data;
36 if ($tag eq '0'){
37 # !!??
38 # push(@{$spec->{value}}, $element);
39 } elsif ($tag eq 'text'){
40 $element->[0]{type} = 'Text';
41 my $kid = shift @$element;
42 $kid->{value} = [$self->clean_text($element->[1])];
43 push(@{$spec->{children}}, $kid);
44 } elsif ($tag eq 'textblock'){
45 $element->[0]{type} = 'TextBlock';
46 my $kid = shift @$element;
47 $kid->{value} = [$self->clean_text($element->[1])];
48 push(@{$spec->{children}}, $kid);
49 } elsif (lc($tag) eq 'image'){
50 $element->[0]{type} = 'Image';
51 push(@{$spec->{children}}, shift @$element);
52 } elsif (lc($tag) eq 'row'){
53 $element->[0]{type} = 'Row';
54 push(@{$spec->{children}}, shift @$element);
55 $self->mangle_spec($spec->{children}->[-1], $element);
56 } elsif (lc($tag) eq 'column'){
57 $element->[0]{type} = 'Column';
58 push(@{$spec->{children}}, shift @$element);
59 $self->mangle_spec($spec->{children}->[-1], $element);
60 } elsif (lc($tag) eq 'grid'){
61 $element->[0]{type} = 'Grid';
62 push(@{$spec->{children}}, shift @$element);
63 $self->mangle_spec($spec->{children}->[-1], $element);
64 } elsif (lc($tag) eq 'doc'){
65 $element->[0]{type} = 'Doc';
66 push(@{$spec->{children}}, shift @$element);
67 $self->mangle_spec($spec->{children}->[-1], $element);
68 } else {
69 $element->[0]{type} = 'Box';
70 push(@{$spec->{children}}, shift @$element);
71 $self->mangle_spec($spec->{children}->[-1], $element);
72 }
73 }
74 }
75  
76 sub clean_text{
77 my ($self, $element) = @_;
78 return unless $element;
79 return if $element =~ /^[\s\n\r]*$/;
80 if ($self->clean_whitespace){
81 $element =~ s/^[\s\n\r]+//;
82 $element =~ s/[\s\n\r]+$//;
83 }
84 my @el = split(/\n/,$element);
85 if ($self->clean_whitespace){
86 foreach(@el){
87 s/^\s+//;
88 s/\s+$//;
89 }
90 }
91 return @el;
92 }
93  
94 __PACKAGE__->meta->make_immutable;
95  
96 1;
97  
98  
99 __END__
« lib/PDF/Boxer.pm ↑ Summary