| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Indent::String; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 74267 | use strict; | 
|  | 2 |  |  |  |  | 18 |  | 
|  | 2 |  |  |  |  | 59 |  | 
| 4 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 67 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 1004 | use Class::Utils qw(set_params); | 
|  | 2 |  |  |  |  | 60431 |  | 
|  | 2 |  |  |  |  | 36 |  | 
| 7 | 2 |  |  | 2 |  | 1056 | use Indent::Utils qw(line_size_check); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 106 |  | 
| 8 | 2 |  |  | 2 |  | 14 | use Readonly; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 959 |  | 
| 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 | 0 |  |  | 0 | 1 |  | my ($class, @params) = @_; | 
| 19 | 0 |  |  |  |  |  | my $self = bless {}, $class; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # Options. | 
| 22 | 0 |  |  |  |  |  | $self->{'line_size'} = $LINE_SIZE; | 
| 23 | 0 |  |  |  |  |  | $self->{'next_indent'} = "\t"; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # Output. | 
| 26 | 0 |  |  |  |  |  | $self->{'output_separator'} = "\n"; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # Process params. | 
| 29 | 0 |  |  |  |  |  | set_params($self, @params); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # 'line_size' check. | 
| 32 | 0 |  |  |  |  |  | line_size_check($self); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # Object. | 
| 35 | 0 |  |  |  |  |  | return $self; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | # Indent text by words to line_size block size. | 
| 39 |  |  |  |  |  |  | sub indent { | 
| 40 | 0 |  |  | 0 | 1 |  | my ($self, $data, $act_indent, $non_indent) = @_; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # 'indent' initialization. | 
| 43 | 0 | 0 |  |  |  |  | if (! defined $act_indent) { | 
| 44 | 0 |  |  |  |  |  | $act_indent = $EMPTY_STR; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # If non_indent data, than return. | 
| 48 | 0 | 0 |  |  |  |  | if ($non_indent) { | 
| 49 | 0 |  |  |  |  |  | return $act_indent.$data; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | my ($first, $second) = (undef, $act_indent.$data); | 
| 53 | 0 |  |  |  |  |  | my $last_second_length = 0; | 
| 54 | 0 |  |  |  |  |  | my @data; | 
| 55 | 0 |  |  |  |  |  | my $one = 1; | 
| 56 | 0 |  | 0 |  |  |  | while (length $second >= $self->{'line_size'} | 
|  |  |  | 0 |  |  |  |  | 
| 57 |  |  |  |  |  |  | && $second =~ /^\s*\S+\s+/ms | 
| 58 |  |  |  |  |  |  | && $last_second_length != length $second) { | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # Last length of non-parsed part of data. | 
| 61 | 0 |  |  |  |  |  | $last_second_length = length $second; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # Parse to indent length. | 
| 64 | 0 |  |  |  |  |  | ($first, my $tmp) = $second | 
| 65 |  |  |  |  |  |  | =~ /^(.{0,$self->{'line_size'}})\s+(.*)$/msx; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # If string is non-breakable in indent length, than parse to | 
| 68 |  |  |  |  |  |  | # blank char. | 
| 69 | 0 | 0 | 0 |  |  |  | if (! $first || length $first < length $act_indent | 
|  |  |  | 0 |  |  |  |  | 
| 70 |  |  |  |  |  |  | || $first =~ /^$act_indent\s*$/ms) { | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | ($first, $tmp) = $second | 
| 73 |  |  |  |  |  |  | =~ /^($act_indent\s*[^\s]+?)\s(.*)$/msx; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | # If parsing is right. | 
| 77 | 0 | 0 |  |  |  |  | if ($tmp) { | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # Non-parsed part of data. | 
| 80 | 0 |  |  |  |  |  | $second = $tmp; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | # Add next_indent to string. | 
| 83 | 0 | 0 |  |  |  |  | if ($one == 1) { | 
| 84 | 0 |  |  |  |  |  | $act_indent .= $self->{'next_indent'}; | 
| 85 |  |  |  |  |  |  | } | 
| 86 | 0 |  |  |  |  |  | $one = 0; | 
| 87 | 0 |  |  |  |  |  | $second = $act_indent.$second; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # Parsed part of data to @data array. | 
| 90 | 0 |  |  |  |  |  | push @data, $first; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # Add other data to @data array. | 
| 95 | 0 | 0 | 0 |  |  |  | if ($second || $second !~ /^\s*$/ms) { | 
| 96 | 0 |  |  |  |  |  | push @data, $second; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # Return as array or one line with output separator between its. | 
| 100 | 0 | 0 |  |  |  |  | return wantarray ? @data : join($self->{'output_separator'}, @data); | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |