| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Indent::Block; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 84200 | use strict; | 
|  | 4 |  |  |  |  | 29 |  | 
|  | 4 |  |  |  |  | 122 |  | 
| 4 | 4 |  |  | 4 |  | 21 | use warnings; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 121 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 2182 | use Class::Utils qw(set_params); | 
|  | 4 |  |  |  |  | 126476 |  | 
|  | 4 |  |  |  |  | 91 |  | 
| 7 | 4 |  |  | 4 |  | 2512 | use Indent::Utils qw(line_size_check string_len); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 238 |  | 
| 8 | 4 |  |  | 4 |  | 27 | use Readonly; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 2865 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # Constants. | 
| 11 |  |  |  |  |  |  | Readonly::Scalar my $EMPTY_STR => q{}; | 
| 12 |  |  |  |  |  |  | Readonly::Scalar my $LINE_SIZE => 79; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our $VERSION = 0.08; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Constructor. | 
| 17 |  |  |  |  |  |  | sub new { | 
| 18 | 5 |  |  | 5 | 1 | 6094 | my ($class, @params) = @_; | 
| 19 | 5 |  |  |  |  | 16 | my $self = bless {}, $class; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # Line size. | 
| 22 | 5 |  |  |  |  | 22 | $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 |  |  |  |  | 9 | $self->{'strict'} = 1; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # Process params. | 
| 34 | 5 |  |  |  |  | 27 | set_params($self, @params); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # 'line_size' check. | 
| 37 | 3 |  |  |  |  | 45 | line_size_check($self); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # Save current piece. | 
| 40 | 3 |  |  |  |  | 6 | $self->{'_current'} = $EMPTY_STR; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # Object. | 
| 43 | 3 |  |  |  |  | 15 | return $self; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # Parses tag to indented data. | 
| 47 |  |  |  |  |  |  | sub indent { | 
| 48 | 5 |  |  | 5 | 1 | 2115 | my ($self, $data_ar, $act_indent, $non_indent) = @_; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | # Undef indent. | 
| 51 | 5 | 100 |  |  |  | 12 | 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 |  |  |  | 14 | if ($non_indent) { | 
| 60 | 1 |  |  |  |  | 6 | return $act_indent.join($EMPTY_STR, @input); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # Indent. | 
| 64 | 4 |  |  |  |  | 6 | my @data = (); | 
| 65 | 4 |  |  |  |  | 7 | my ($first, $second); | 
| 66 | 4 |  |  |  |  | 7 | $first = shift @input; | 
| 67 | 4 |  |  |  |  | 7 | my $tmp_indent = $act_indent; | 
| 68 | 4 |  |  |  |  | 9 | while (@input) { | 
| 69 | 4 |  |  |  |  | 5 | $second = shift @input; | 
| 70 | 4 | 100 |  |  |  | 10 | if ($self->_compare($first, $second, $tmp_indent)) { | 
| 71 | 2 |  |  |  |  | 5 | push @data, $self->{'_current'}; | 
| 72 | 2 |  |  |  |  | 4 | $first = $second; | 
| 73 | 2 |  |  |  |  | 3 | $second = $EMPTY_STR; | 
| 74 | 2 |  |  |  |  | 7 | $tmp_indent = $act_indent.$self->{'next_indent'}; | 
| 75 |  |  |  |  |  |  | } else { | 
| 76 | 2 |  |  |  |  | 6 | $first .= $second; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | # Add other data to @data array. | 
| 81 | 4 | 50 |  |  |  | 11 | if ($first) { | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | # White space optimalization. | 
| 84 | 4 | 50 |  |  |  | 10 | if (! $self->{'strict'}) { | 
| 85 | 0 |  |  |  |  | 0 | $first =~ s/^\s*//ms; | 
| 86 | 0 |  |  |  |  | 0 | $first =~ s/\s*$//ms; | 
| 87 |  |  |  |  |  |  | } | 
| 88 | 4 | 50 |  |  |  | 10 | if ($first ne $EMPTY_STR) { | 
| 89 | 4 |  |  |  |  | 9 | push @data, $tmp_indent.$first; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # Return as array or one line with output separator between its. | 
| 94 | 4 | 100 |  |  |  | 21 | 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 |  | 9 | my ($self, $first, $second, $act_indent) = @_; | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # Without optimalization. | 
| 102 | 4 | 50 |  |  |  | 12 | if ($self->{'strict'}) { | 
| 103 | 4 | 100 | 66 |  |  | 22 | 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 |  |  |  |  | 21 | $self->{'_current'} = $act_indent.$first; | 
| 110 | 2 |  |  |  |  | 7 | return 1; | 
| 111 |  |  |  |  |  |  | } else { | 
| 112 | 2 |  |  |  |  | 8 | 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__ |