line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PDF::Boxer::SpecParser; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$PDF::Boxer::SpecParser::VERSION = '0.004'; |
4
|
|
|
|
|
|
|
} |
5
|
3
|
|
|
3
|
|
5468
|
use Moose; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
29
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Convert markup to Perl data |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
19601
|
use namespace::autoclean; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
34
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
1361
|
use XML::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
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__ |
100
|
|
|
|
|
|
|
=pod |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 NAME |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
PDF::Boxer::SpecParser - Convert markup to Perl data |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 VERSION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
version 0.004 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 AUTHOR |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Jason Galea <lecstor@cpan.org> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Jason Galea. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
119
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|