File Coverage

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


line stmt bran cond sub pod time code
1             package PYX::Optimization;
2              
3 6     6   155068 use strict;
  6         47  
  6         199  
4 6     6   33 use warnings;
  6         12  
  6         174  
5              
6 6     6   1996 use Class::Utils qw(set_params);
  6         118703  
  6         213  
7 6     6   3884 use Encode;
  6         63454  
  6         511  
8 6     6   46 use Error::Pure qw(err);
  6         11  
  6         268  
9 6     6   2754 use PYX qw(char comment);
  6         50149  
  6         122  
10 6     6   3381 use PYX::Parser;
  6         8666  
  6         199  
11 6     6   43 use PYX::Utils;
  6         13  
  6         4197  
12              
13             our $VERSION = 0.03;
14              
15             # Constructor.
16             sub new {
17 6     6 1 5770 my ($class, @params) = @_;
18 6         20 my $self = bless {}, $class;
19              
20             # Output encoding.
21 6         30 $self->{'output_encoding'} = 'UTF-8';
22              
23             # Output handler.
24 6         20 $self->{'output_handler'} = \*STDOUT;
25              
26             # Process params.
27 6         44 set_params($self, @params);
28              
29             # PYX::Parser object.
30             $self->{'pyx_parser'} = PYX::Parser->new(
31             'output_handler' => $self->{'output_handler'},
32             'output_rewrite' => 1,
33             'callbacks' => {
34             'data' => \&_data,
35             'comment' => \&_comment,
36             },
37             'non_parser_options' => {
38 4         75 'output_encoding' => $self->{'output_encoding'},
39             },
40             );
41              
42             # Object.
43 4         327 return $self;
44             }
45              
46             # Parse pyx text or array of pyx text.
47             sub parse {
48 4     4 1 7803 my ($self, $pyx, $out) = @_;
49 4         23 $self->{'pyx_parser'}->parse($pyx, $out);
50             }
51              
52             # Parse file with pyx text.
53             sub parse_file {
54 4     4 1 6383 my ($self, $file, $out) = @_;
55 4         18 $self->{'pyx_parser'}->parse_file($file, $out);
56             }
57              
58             # Parse from handler.
59             sub parse_handler {
60 4     4 1 7132 my ($self, $input_file_handler, $out) = @_;
61 4         21 $self->{'pyx_parser'}->parse_handler($input_file_handler, $out);
62             }
63              
64             # Process data.
65             sub _data {
66 42     42   4064 my ($pyx_parser_obj, $data) = @_;
67 42         109 my $tmp = PYX::Utils::encode($data);
68 42 100       402 if ($tmp =~ /^[\s\n]*$/) {
69 3         11 return;
70             }
71              
72             # TODO Preserve?
73              
74             # White space on begin of data.
75 39         136 $tmp =~ s/^[\s\n]*//s;
76              
77             # White space on end of data.
78 39         176 $tmp =~ s/[\s\n]*$//s;
79              
80             # White space on middle of data.
81 39         113 $tmp =~ s/[\s\n]+/\ /sg;
82              
83 39         92 $data = PYX::Utils::decode($tmp);
84 39         234 my $out = $pyx_parser_obj->{'output_handler'};
85             my $encoded_output = Encode::encode(
86 39         103 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
87             char($data),
88             );
89 39         1939 print {$out} $encoded_output, "\n";
  39         797  
90             }
91              
92             # Process comment.
93             sub _comment {
94 24     24   2884 my ($pyx_parser_obj, $comment) = @_;
95 24         112 my $tmp = PYX::Utils::encode($comment);
96 24 100       278 if ($tmp =~ /^[\s\n]*$/) {
97 3         9 return;
98             }
99 21         81 $tmp =~ s/^[\s\n]*//s;
100 21         100 $tmp =~ s/[\s\n]*$//s;
101 21         55 $comment = PYX::Utils::decode($tmp);
102 21         130 my $out = $pyx_parser_obj->{'output_handler'};
103             my $encoded_output = Encode::encode(
104 21         66 $pyx_parser_obj->{'non_parser_options'}->{'output_encoding'},
105             comment($comment),
106             );
107 21         1453 print {$out} $encoded_output, "\n";
  21         523  
108             }
109              
110             1;
111              
112             __END__