| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Indent::Data; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 86207 | use strict; | 
|  | 4 |  |  |  |  | 39 |  | 
|  | 4 |  |  |  |  | 119 |  | 
| 4 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 114 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 4 |  |  | 4 |  | 1986 | use Class::Utils qw(set_params); | 
|  | 4 |  |  |  |  | 116068 |  | 
|  | 4 |  |  |  |  | 88 |  | 
| 7 | 4 |  |  | 4 |  | 315 | use Error::Pure qw(err); | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 165 |  | 
| 8 | 4 |  |  | 4 |  | 1864 | use Indent::Utils qw(line_size_check string_len); | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 228 |  | 
| 9 | 4 |  |  | 4 |  | 27 | use Readonly; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 1752 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # Constants. | 
| 12 |  |  |  |  |  |  | Readonly::Scalar my $EMPTY_STR => q{}; | 
| 13 |  |  |  |  |  |  | Readonly::Scalar my $LINE_SIZE => 79; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our $VERSION = 0.08; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # Constructor. | 
| 18 |  |  |  |  |  |  | sub new { | 
| 19 | 11 |  |  | 11 | 1 | 12815 | my ($class, @params) = @_; | 
| 20 | 11 |  |  |  |  | 28 | my $self = bless {}, $class; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # Options. | 
| 23 | 11 |  |  |  |  | 33 | $self->{'line_size'} = $LINE_SIZE; | 
| 24 | 11 |  |  |  |  | 20 | $self->{'next_indent'} = "\t"; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Output. | 
| 27 | 11 |  |  |  |  | 18 | $self->{'output_separator'} = "\n"; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # Process params. | 
| 30 | 11 |  |  |  |  | 43 | set_params($self, @params); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # 'line_size' check. | 
| 33 | 9 |  |  |  |  | 161 | line_size_check($self); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # Error with 'next_indent' length greater than 'line_size'. | 
| 36 | 9 | 100 |  |  |  | 30 | if ($self->{'line_size'} <= length $self->{'next_indent'}) { | 
| 37 | 1 |  |  |  |  | 8 | err "Bad line_size = '$self->{'line_size'}' ". | 
| 38 |  |  |  |  |  |  | "or length of string '$self->{'next_indent'}'."; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | # Object. | 
| 42 | 8 |  |  |  |  | 30 | return $self; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | # Parses tag to indented data. | 
| 46 |  |  |  |  |  |  | sub indent { | 
| 47 | 10 |  |  | 10 | 1 | 2945 | my ($self, $data, $act_indent, $non_indent) = @_; | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # Undef indent. | 
| 50 | 10 | 100 |  |  |  | 23 | if (! $act_indent) { | 
| 51 | 4 |  |  |  |  | 7 | $act_indent = $EMPTY_STR; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # If non_indent data, than return. | 
| 55 | 10 | 100 |  |  |  | 20 | if ($non_indent) { | 
| 56 | 1 |  |  |  |  | 5 | return $act_indent.$data; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # Check to actual indent maximal length. | 
| 60 | 9 | 100 |  |  |  | 20 | if (string_len($act_indent) > ($self->{'line_size'} | 
| 61 |  |  |  |  |  |  | - string_len($self->{'next_indent'}) - 1)) { | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 2 |  |  |  |  | 9 | err 'Bad actual indent value. Length is greater then '. | 
| 64 |  |  |  |  |  |  | '(\'line_size\' - \'size of next_indent\' - 1).'; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # Splits data. | 
| 68 | 7 |  |  |  |  | 11 | my $first = undef; | 
| 69 | 7 |  |  |  |  | 14 | my $second = $act_indent.$data; | 
| 70 | 7 |  |  |  |  | 12 | my @data; | 
| 71 | 7 |  |  |  |  | 15 | while (string_len($second) >= $self->{'line_size'}) { | 
| 72 | 30 |  |  |  |  | 66 | $first = substr($second, 0, $self->{'line_size'}); | 
| 73 |  |  |  |  |  |  | $second = $act_indent.$self->{'next_indent'}.substr($second, | 
| 74 | 30 |  |  |  |  | 59 | $self->{'line_size'}); | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | # Parsed part of data to @data array. | 
| 77 | 30 |  |  |  |  | 63 | push @data, $first; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | # Add other data to @data array. | 
| 81 | 7 | 100 | 100 |  |  | 29 | if ($second && $second ne $act_indent.$self->{'next_indent'}) { | 
| 82 | 3 |  |  |  |  | 6 | push @data, $second; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # Return as array or one line with output separator between its. | 
| 86 | 7 | 100 |  |  |  | 39 | return wantarray ? @data : join($self->{'output_separator'}, @data); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | 1; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | __END__ |