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   232843 use strict;
  6         15  
  6         221  
4 6     6   34 use warnings;
  6         35  
  6         339  
5              
6 6     6   1873 use Class::Utils qw(set_params);
  6         49690  
  6         218  
7 6     6   3840 use Encode;
  6         127462  
  6         640  
8 6     6   51 use Error::Pure qw(err);
  6         10  
  6         345  
9 6     6   3357 use PYX qw(char comment);
  6         59625  
  6         166  
10 6     6   4148 use PYX::Parser;
  6         11731  
  6         270  
11 6     6   56 use PYX::Utils;
  6         18  
  6         5315  
12              
13             our $VERSION = 0.06;
14              
15             # Constructor.
16             sub new {
17 6     6 1 1281695 my ($class, @params) = @_;
18 6         17 my $self = bless {}, $class;
19              
20             # Output encoding.
21 6         22 $self->{'output_encoding'} = 'utf-8';
22              
23             # Output handler.
24 6         19 $self->{'output_handler'} = \*STDOUT;
25              
26             # Process params.
27 6         55 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         64 'output_encoding' => $self->{'output_encoding'},
40             },
41             );
42              
43             # Object.
44 4         265 return $self;
45             }
46              
47             # Parse pyx text or array of pyx text.
48             sub parse {
49 6     6 1 8249 my ($self, $pyx, $out) = @_;
50              
51 6         44 $self->{'pyx_parser'}->parse($pyx, $out);
52              
53 6         420 return;
54             }
55              
56             # Parse file with pyx text.
57             sub parse_file {
58 6     6 1 19961 my ($self, $file, $out) = @_;
59              
60 6         45 $self->{'pyx_parser'}->parse_file($file, $out);
61              
62 6         1322 return;
63             }
64              
65             # Parse from handler.
66             sub parse_handler {
67 6     6 1 13607 my ($self, $input_file_handler, $out) = @_;
68              
69 6         52 $self->{'pyx_parser'}->parse_handler($input_file_handler, $out);
70              
71 6         748 return;
72             }
73              
74             # Process data.
75             sub _data {
76 63     63   5002 my ($pyx_parser_obj, $data) = @_;
77              
78 63         171 my $tmp = PYX::Utils::encode($data);
79 63 100       774 if ($tmp =~ /^[\s\n]*$/) {
80 6         15 return;
81             }
82              
83             # TODO Preserve?
84              
85             # White space on begin of data.
86 57         195 $tmp =~ s/^[\s\n]*//s;
87              
88             # White space on end of data.
89 57         283 $tmp =~ s/[\s\n]*$//s;
90              
91             # White space on middle of data.
92 57         161 $tmp =~ s/[\s\n]+/\ /sg;
93              
94 57         126 $data = PYX::Utils::decode($tmp);
95 57         377 my $out = $pyx_parser_obj->{'output_handler'};
96             my $encoded_output = Encode::encode(
97 57         151 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
98             char($data),
99             );
100 57         2356 print {$out} $encoded_output, "\n";
  57         1696  
101              
102 57         220 return;
103             }
104              
105             # Process comment.
106             sub _comment {
107 24     24   2135 my ($pyx_parser_obj, $comment) = @_;
108              
109 24         78 my $tmp = PYX::Utils::encode($comment);
110 24 100       293 if ($tmp =~ /^[\s\n]*$/) {
111 3         10 return;
112             }
113 21         68 $tmp =~ s/^[\s\n]*//s;
114 21         86 $tmp =~ s/[\s\n]*$//s;
115 21         48 $comment = PYX::Utils::decode($tmp);
116 21         124 my $out = $pyx_parser_obj->{'output_handler'};
117             my $encoded_output = Encode::encode(
118 21         68 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
119             comment($comment),
120             );
121 21         964 print {$out} $encoded_output, "\n";
  21         631  
122              
123 21         69 return;
124             }
125              
126             1;
127              
128             __END__