File Coverage

blib/lib/Indent/Data.pm
Criterion Covered Total %
statement 45 45 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 70 70 100.0


line stmt bran cond sub pod time code
1             package Indent::Data;
2              
3 4     4   122488 use strict;
  4         5  
  4         162  
4 4     4   34 use warnings;
  4         7  
  4         223  
5              
6 4     4   1670 use Class::Utils qw(set_params);
  4         41700  
  4         79  
7 4     4   342 use Error::Pure qw(err);
  4         10  
  4         186  
8 4     4   1867 use Indent::Utils qw(line_size_check string_len);
  4         14  
  4         264  
9 4     4   23 use Readonly;
  4         5  
  4         1834  
10              
11             # Constants.
12             Readonly::Scalar my $EMPTY_STR => q{};
13             Readonly::Scalar my $LINE_SIZE => 79;
14              
15             our $VERSION = 0.09;
16              
17             # Constructor.
18             sub new {
19 11     11 1 575551 my ($class, @params) = @_;
20 11         28 my $self = bless {}, $class;
21              
22             # Options.
23 11         45 $self->{'line_size'} = $LINE_SIZE;
24 11         25 $self->{'next_indent'} = "\t";
25              
26             # Output.
27 11         23 $self->{'output_separator'} = "\n";
28              
29             # Process params.
30 11         72 set_params($self, @params);
31              
32             # 'line_size' check.
33 9         179 line_size_check($self);
34              
35             # Error with 'next_indent' length greater than 'line_size'.
36 9 100       33 if ($self->{'line_size'} <= length $self->{'next_indent'}) {
37 1         6 err "Bad line_size = '$self->{'line_size'}' ".
38             "or length of string '$self->{'next_indent'}'.";
39             }
40              
41             # Object.
42 8         43 return $self;
43             }
44              
45             # Parses tag to indented data.
46             sub indent {
47 10     10 1 4705 my ($self, $data, $act_indent, $non_indent) = @_;
48              
49             # Undef indent.
50 10 100       53 if (! $act_indent) {
51 4         10 $act_indent = $EMPTY_STR;
52             }
53              
54             # If non_indent data, than return.
55 10 100       23 if ($non_indent) {
56 1         5 return $act_indent.$data;
57             }
58              
59             # Check to actual indent maximal length.
60 9 100       25 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         13 my $first = undef;
69 7         15 my $second = $act_indent.$data;
70 7         12 my @data;
71 7         15 while (string_len($second) >= $self->{'line_size'}) {
72 30         57 $first = substr($second, 0, $self->{'line_size'});
73             $second = $act_indent.$self->{'next_indent'}.substr($second,
74 30         60 $self->{'line_size'});
75              
76             # Parsed part of data to @data array.
77 30         71 push @data, $first;
78             }
79              
80             # Add other data to @data array.
81 7 100 100     40 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       56 return wantarray ? @data : join($self->{'output_separator'}, @data);
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding utf8
96              
97             =head1 NAME
98              
99             Indent::Data - Class for data indenting.
100              
101             =head1 SYNOPSIS
102              
103             use Indent::Data;
104              
105             my $obj = Indent::Data->new(%parameters);
106             my $string = $obj->indent($data, [$indent, $non_indent]);
107             my @data = $obj->indent($data, [$indent, $non_indent]);
108              
109             =head1 METHODS
110              
111             =head2 C<new>
112              
113             my $obj = Indent::Data->new(%parameters);
114              
115             Constructor.
116              
117             Returns instance of object.
118              
119             =over 8
120              
121             =item * C<line_size>
122              
123             Sets indent line size value.
124             Default value is 79.
125              
126             =item * C<next_indent>
127              
128             Sets next indent string.
129             Default value is tabelator (\t).
130              
131             =item * C<output_separator>
132              
133             Sets output separator between indented datas for string context.
134             Default value is newline (\n).
135              
136             =back
137              
138             =head2 C<indent>
139              
140             my $string = $obj->indent($data, [$indent, $non_indent]);
141              
142             or
143              
144             my @data = $obj->indent($data, [$indent, $non_indent]);
145              
146             Indent text data to line_size block size.
147             C<$act_indent> - Actual indent string. Will be in each output string.
148             Length of C<$act_indent> variable must be less then ('line_size' - length of 'next_indent' - 1).
149             C<$non_indent> - Is flag for non indenting. Default is 0.
150              
151             Returns string or array of data to print.
152              
153             =head1 ERRORS
154              
155             new():
156             Bad 'line_size' = '%s' or length of string '%s'.
157             Bad actual indent value. Length is greater then ('line_size' - 'size of next_indent' - 1).
158             From Class::Utils::set_params():
159             Unknown parameter '%s'.
160              
161             indent():
162             From Indent::Utils::line_size_check():
163             'line_size' parameter must be a positive number.
164             line_size => %s
165              
166             =head1 EXAMPLE1
167              
168             use strict;
169             use warnings;
170              
171             use Indent::Data;
172              
173             # Indent::Data object.
174             my $i = Indent::Data->new(
175             'line_size' => '10',
176             'next_indent' => ' ',
177             'output_separator' => "|\n",
178             );
179              
180             # Print indented text.
181             print $i->indent('text text text text text text')."|\n";
182              
183             # Output:
184             # text text |
185             # text tex|
186             # t text t|
187             # ext|
188              
189             =head1 EXAMPLE2
190              
191             use strict;
192             use warnings;
193              
194             use Indent::Data;
195              
196             # Indent::Data object.
197             my $i = Indent::Data->new(
198             'line_size' => '10',
199             'next_indent' => ' ',
200             'output_separator' => "|\n",
201             );
202              
203             # Print indented text.
204             print $i->indent('text text text text text text', '<->')."|\n";
205              
206             # Output:
207             # <->text te|
208             # <-> xt te|
209             # <-> xt te|
210             # <-> xt te|
211             # <-> xt te|
212             # <-> xt|
213              
214             =head1 EXAMPLE3
215              
216             use strict;
217             use warnings;
218              
219             use Indent::Data;
220              
221             # Indent::Data object.
222             my $i = Indent::Data->new(
223             'line_size' => '10',
224             'next_indent' => ' ',
225             'output_separator' => "|\n",
226             );
227              
228             # Print indented text.
229             print $i->indent('text text text text text text', '<->', 1)."|\n";
230              
231             # Output:
232             # <->text text text text text text|
233              
234             =head1 DEPENDENCIES
235              
236             L<Class::Utils>,
237             L<Error::Pure>,
238             L<Indent::Utils>,
239             L<Readonly>.
240              
241             =head1 SEE ALSO
242              
243             =over
244              
245             =item L<Indent>
246              
247             Class for indent handling.
248              
249             =item L<Indent::Block>
250              
251             Class for block indenting.
252              
253             =item L<Indent::String>
254              
255             Class for text indenting.
256              
257             =item L<Indent::Utils>
258              
259             Utilities for Indent classes.
260              
261             =item L<Indent::Word>
262              
263             Class for word indenting.
264              
265             =back
266              
267             =head1 REPOSITORY
268              
269             L<https://github.com/michal-josef-spacek/Indent>
270              
271             =head1 AUTHOR
272              
273             Michal Josef Špaček L<mailto:skim@cpan.org>
274              
275             L<http://skim.cz>
276              
277             =head1 LICENSE AND COPYRIGHT
278              
279             © 2005-2024 Michal Josef Špaček
280              
281             BSD 2-Clause License
282              
283             =head1 VERSION
284              
285             0.09
286              
287             =cut