File Coverage

blib/lib/Indent/String.pm
Criterion Covered Total %
statement 15 47 31.9
branch 0 14 0.0
condition 0 15 0.0
subroutine 5 7 71.4
pod 2 2 100.0
total 22 85 25.8


line stmt bran cond sub pod time code
1             package Indent::String;
2              
3 2     2   162519 use strict;
  2         5  
  2         82  
4 2     2   17 use warnings;
  2         4  
  2         134  
5              
6 2     2   1224 use Class::Utils qw(set_params);
  2         33746  
  2         52  
7 2     2   1361 use Indent::Utils qw(line_size_check);
  2         8  
  2         171  
8 2     2   14 use Readonly;
  2         6  
  2         1446  
9              
10             # Constants.
11             Readonly::Scalar my $EMPTY_STR => q{};
12             Readonly::Scalar my $LINE_SIZE => 79;
13              
14             our $VERSION = 0.09;
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__
106              
107             =pod
108              
109             =encoding utf8
110              
111             =head1 NAME
112              
113             Indent::String - Class for text indenting.
114              
115             =head1 SYNOPSIS
116              
117             use Indent::String;
118              
119             my $obj = Indent::String->new(%parameters);
120             my $string = $obj->indent('text text text');
121             my @data = $obj->indent('text text text');
122              
123             =head1 METHODS
124              
125             =head2 C<new>
126              
127             my $obj = Indent::String->new(%parameters);
128              
129             Constructor.
130              
131             Returns instance of object.
132              
133             =over 8
134              
135             =item * C<line_size>
136              
137             Sets indent line size value.
138             Default value is 79.
139              
140             =item * C<next_indent>
141              
142             Sets output separator between indented datas for string context.
143             Default value is "\t" (tabelator).
144              
145             =item * C<output_separator>
146              
147             Output separator between data in scalar context.
148             Default value is "\n" (new line).
149              
150             =back
151              
152             =head2 C<indent>
153              
154             my $string = $obj->indent('text text text');
155              
156             or
157              
158             my @data = $obj->indent('text text text');
159              
160             Indent text by words to line_size block size.
161              
162             C<$act_indent> - Actual indent string. Will be in each output string.
163             C<$non_indent> - Is flag for non indenting. Default is 0.
164              
165             Returns string or array of data to print.
166              
167             =head1 ERRORS
168              
169             new():
170             From Class::Utils::set_params():
171             Unknown parameter '%s'.
172             From Indent::Utils::line_size_check():
173             'line_size' parameter must be a positive number.
174             line_size => %s
175              
176             =head1 EXAMPLE
177              
178             use strict;
179             use warnings;
180              
181             use Indent::String;
182              
183             # Object.
184             my $i = Indent::String->new(
185             'line_size' => 20,
186             );
187              
188             # Indent.
189             print $i->indent(join(' ', ('text') x 7))."\n";
190              
191             # Output:
192             # text text text text
193             # <--tab->text text text
194              
195             =head1 DEPENDENCIES
196              
197             L<Class::Utils>,
198             L<Indent::Utils>,
199             L<Readonly>.
200              
201             =head1 SEE ALSO
202              
203             =over
204              
205             =item L<Indent>
206              
207             Class for indent handling.
208              
209             =item L<Indent::Block>
210              
211             Class for block indenting.
212              
213             =item L<Indent::Data>
214              
215             Class for data indenting.
216              
217             =item L<Indent::Utils>
218              
219             Utilities for Indent classes.
220              
221             =item L<Indent::Word>
222              
223             Class for word indenting.
224              
225             =back
226              
227             =head1 REPOSITORY
228              
229             L<https://github.com/michal-josef-spacek/Indent>
230              
231             =head1 AUTHOR
232              
233             Michal Josef Špaček L<mailto:skim@cpan.org>
234              
235             L<http://skim.cz>
236              
237             =head1 LICENSE AND COPYRIGHT
238              
239             © 2005-2024 Michal Josef Špaček
240              
241             BSD 2-Clause License
242              
243             =head1 VERSION
244              
245             0.09
246              
247             =cut