File Coverage

blib/lib/PYX/Optimization.pm
Criterion Covered Total %
statement 65 65 100.0
branch 4 4 100.0
condition n/a
subroutine 14 14 100.0
pod 4 4 100.0
total 87 87 100.0


line stmt bran cond sub pod time code
1             package PYX::Optimization;
2              
3 6     6   153396 use strict;
  6         47  
  6         171  
4 6     6   29 use warnings;
  6         11  
  6         178  
5              
6 6     6   1900 use Class::Utils qw(set_params);
  6         111308  
  6         188  
7 6     6   3728 use Encode;
  6         62103  
  6         437  
8 6     6   48 use Error::Pure qw(err);
  6         10  
  6         244  
9 6     6   2748 use PYX qw(char comment);
  6         47150  
  6         118  
10 6     6   3217 use PYX::Parser;
  6         8238  
  6         188  
11 6     6   54 use PYX::Utils;
  6         13  
  6         4064  
12              
13             our $VERSION = 0.04;
14              
15             # Constructor.
16             sub new {
17 6     6 1 5602 my ($class, @params) = @_;
18 6         19 my $self = bless {}, $class;
19              
20             # Output encoding.
21 6         30 $self->{'output_encoding'} = 'utf-8';
22              
23             # Output handler.
24 6         19 $self->{'output_handler'} = \*STDOUT;
25              
26             # Process params.
27 6         29 set_params($self, @params);
28              
29             # PYX::Parser object.
30             $self->{'pyx_parser'} = PYX::Parser->new(
31             'output_encoding' => $self->{'output_encoding'},
32             'output_handler' => $self->{'output_handler'},
33             'output_rewrite' => 1,
34             'callbacks' => {
35             'data' => \&_data,
36             'comment' => \&_comment,
37             },
38             'non_parser_options' => {
39 4         72 'output_encoding' => $self->{'output_encoding'},
40             },
41             );
42              
43             # Object.
44 4         268 return $self;
45             }
46              
47             # Parse pyx text or array of pyx text.
48             sub parse {
49 6     6 1 9167 my ($self, $pyx, $out) = @_;
50              
51 6         31 $self->{'pyx_parser'}->parse($pyx, $out);
52              
53 6         550 return;
54             }
55              
56             # Parse file with pyx text.
57             sub parse_file {
58 6     6 1 9815 my ($self, $file, $out) = @_;
59              
60 6         35 $self->{'pyx_parser'}->parse_file($file, $out);
61              
62 6         929 return;
63             }
64              
65             # Parse from handler.
66             sub parse_handler {
67 6     6 1 9255 my ($self, $input_file_handler, $out) = @_;
68              
69 6         27 $self->{'pyx_parser'}->parse_handler($input_file_handler, $out);
70              
71 6         973 return;
72             }
73              
74             # Process data.
75             sub _data {
76 63     63   4453 my ($pyx_parser_obj, $data) = @_;
77              
78 63         144 my $tmp = PYX::Utils::encode($data);
79 63 100       560 if ($tmp =~ /^[\s\n]*$/) {
80 6         15 return;
81             }
82              
83             # TODO Preserve?
84              
85             # White space on begin of data.
86 57         178 $tmp =~ s/^[\s\n]*//s;
87              
88             # White space on end of data.
89 57         231 $tmp =~ s/[\s\n]*$//s;
90              
91             # White space on middle of data.
92 57         152 $tmp =~ s/[\s\n]+/\ /sg;
93              
94 57         113 $data = PYX::Utils::decode($tmp);
95 57         304 my $out = $pyx_parser_obj->{'output_handler'};
96             my $encoded_output = Encode::encode(
97 57         144 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
98             char($data),
99             );
100 57         2589 print {$out} $encoded_output, "\n";
  57         968  
101              
102 57         247 return;
103             }
104              
105             # Process comment.
106             sub _comment {
107 24     24   2010 my ($pyx_parser_obj, $comment) = @_;
108              
109 24         97 my $tmp = PYX::Utils::encode($comment);
110 24 100       241 if ($tmp =~ /^[\s\n]*$/) {
111 3         9 return;
112             }
113 21         111 $tmp =~ s/^[\s\n]*//s;
114 21         84 $tmp =~ s/[\s\n]*$//s;
115 21         53 $comment = PYX::Utils::decode($tmp);
116 21         117 my $out = $pyx_parser_obj->{'output_handler'};
117             my $encoded_output = Encode::encode(
118 21         64 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
119             comment($comment),
120             );
121 21         1127 print {$out} $encoded_output, "\n";
  21         476  
122              
123 21         101 return;
124             }
125              
126             1;
127              
128             __END__