File Coverage

blib/lib/Indent/Block.pm
Criterion Covered Total %
statement 55 70 78.5
branch 14 22 63.6
condition 4 12 33.3
subroutine 8 8 100.0
pod 2 2 100.0
total 83 114 72.8


line stmt bran cond sub pod time code
1             package Indent::Block;
2              
3 4     4   79782 use strict;
  4         27  
  4         117  
4 4     4   21 use warnings;
  4         6  
  4         112  
5              
6 4     4   1959 use Class::Utils qw(set_params);
  4         116344  
  4         83  
7 4     4   1979 use Indent::Utils qw(line_size_check string_len);
  4         10  
  4         230  
8 4     4   30 use Readonly;
  4         6  
  4         2840  
9              
10             # Constants.
11             Readonly::Scalar my $EMPTY_STR => q{};
12             Readonly::Scalar my $LINE_SIZE => 79;
13              
14             our $VERSION = 0.06;
15              
16             # Constructor.
17             sub new {
18 5     5 1 5520 my ($class, @params) = @_;
19 5         18 my $self = bless {}, $class;
20              
21             # Line size.
22 5         19 $self->{'line_size'} = $LINE_SIZE;
23              
24             # Next indent.
25 5         14 $self->{'next_indent'} = "\t";
26              
27             # Output.
28 5         10 $self->{'output_separator'} = "\n";
29              
30             # Strict mode - without white space optimalization.
31 5         10 $self->{'strict'} = 1;
32              
33             # Process params.
34 5         22 set_params($self, @params);
35              
36             # 'line_size' check.
37 3         43 line_size_check($self);
38              
39             # Save current piece.
40 3         6 $self->{'_current'} = $EMPTY_STR;
41              
42             # Object.
43 3         14 return $self;
44             }
45              
46             # Parses tag to indented data.
47             sub indent {
48 5     5 1 2010 my ($self, $data_ar, $act_indent, $non_indent) = @_;
49              
50             # Undef indent.
51 5 100       13 if (! $act_indent) {
52 4         8 $act_indent = $EMPTY_STR;
53             }
54              
55             # Input data.
56 5         7 my @input = @{$data_ar};
  5         11  
57              
58             # If non_indent data, than return.
59 5 100       11 if ($non_indent) {
60 1         6 return $act_indent.join($EMPTY_STR, @input);
61             }
62              
63             # Indent.
64 4         5 my @data = ();
65 4         9 my ($first, $second);
66 4         5 $first = shift @input;
67 4         8 my $tmp_indent = $act_indent;
68 4         7 while (@input) {
69 4         6 $second = shift @input;
70 4 100       10 if ($self->_compare($first, $second, $tmp_indent)) {
71 2         5 push @data, $self->{'_current'};
72 2         2 $first = $second;
73 2         17 $second = $EMPTY_STR;
74 2         8 $tmp_indent = $act_indent.$self->{'next_indent'};
75             } else {
76 2         7 $first .= $second;
77             }
78             }
79              
80             # Add other data to @data array.
81 4 50       9 if ($first) {
82              
83             # White space optimalization.
84 4 50       9 if (! $self->{'strict'}) {
85 0         0 $first =~ s/^\s*//ms;
86 0         0 $first =~ s/\s*$//ms;
87             }
88 4 50       9 if ($first ne $EMPTY_STR) {
89 4         8 push @data, $tmp_indent.$first;
90             }
91             }
92              
93             # Return as array or one line with output separator between its.
94 4 100       22 return wantarray ? @data : join($self->{'output_separator'}, @data);
95             }
96              
97             # Compare strings with 'line_size' and save right current string.
98             sub _compare {
99 4     4   10 my ($self, $first, $second, $act_indent) = @_;
100              
101             # Without optimalization.
102 4 50       9 if ($self->{'strict'}) {
103 4 100 66     23 if (length $first > 0
      66        
104             && (string_len($act_indent.$first)
105             >= $self->{'line_size'}
106             || string_len($act_indent.$first.$second)
107             > $self->{'line_size'})) {
108              
109 2         4 $self->{'_current'} = $act_indent.$first;
110 2         6 return 1;
111             } else {
112 2         9 return 0;
113             }
114              
115             # With optimalizaton.
116             # TODO Rewrite.
117             } else {
118 0           my $tmp1 = $first;
119 0           $tmp1 =~ s/^\s*//ms;
120 0           $tmp1 =~ s/\s*$//ms;
121 0 0 0       if (length $tmp1 > 0
122             && string_len($act_indent.$tmp1)
123             >= $self->{'line_size'}) {
124              
125 0           $self->{'_current'} = $act_indent.$tmp1;
126 0           return 1;
127             } else {
128 0           my $tmp2 = $first.$second;
129 0           $tmp2 =~ s/^\s*//ms;
130 0           $tmp2 =~ s/\s*$//ms;
131 0 0 0       if (length $tmp1 > 0
132             && string_len($act_indent.$tmp2)
133             > $self->{'line_size'}) {
134              
135 0           $self->{'_current'} = $act_indent.$tmp1;
136 0           return 1;
137             } else {
138 0           return 0;
139             }
140             }
141             }
142             }
143              
144             1;
145              
146             __END__