File Coverage

blib/lib/Perinci/To/Doc/Role/Section/AddTextLines.pm
Criterion Covered Total %
statement 8 64 12.5
branch 0 24 0.0
condition 0 34 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 12 127 9.4


line stmt bran cond sub pod time code
1             package Perinci::To::Doc::Role::Section::AddTextLines;
2              
3             our $DATE = '2021-02-21'; # DATE
4             our $VERSION = '0.875'; # VERSION
5              
6 1     1   890 use 5.010;
  1         4  
7 1     1   6 use Log::ger;
  1         2  
  1         11  
8 1     1   238 use Moo::Role;
  1         13  
  1         14  
9              
10             requires 'doc_lines';
11             requires 'doc_indent_level';
12             requires 'doc_indent_str';
13             has doc_wrap => (is => 'rw', default => sub {1});
14              
15             sub add_doc_lines {
16 0     0 1   my $self = shift;
17 0           my $opts;
18 0 0         if (ref($_[0]) eq 'HASH') { $opts = shift }
  0            
19 0   0       $opts //= {};
20              
21 0 0         my @lines = map { $_ . (/\n\z/s ? "" : "\n") }
22 0 0         map {/\n/ ? split /\n/ : $_} @_;
  0            
23              
24             # debug
25             #my @c = caller(2);
26             #$c[1] =~ s!.+/!!;
27             #@lines = map {"[from $c[1]:$c[2]]$_"} @ lines;
28              
29 0           my $indent = $self->doc_indent_str x $self->doc_indent_level;
30 0   0       my $wrap = $opts->{wrap} // $self->doc_wrap;
31              
32 0 0         if ($wrap) {
33 0           require Text::Wrap;
34              
35             # split into paragraphs, merge each paragraph text into a single line
36             # first
37 0           my @para;
38 0           my $i = 0;
39 0           my ($start, $type);
40 0           $type = '';
41             #$log->warnf("lines=%s", \@lines);
42 0           for (@lines) {
43 0 0 0       if (/^\s*$/) {
    0 0        
44 0 0 0       if (defined($start) && $type ne 'blank') {
45 0           push @para, [$type, [@lines[$start..$i-1]]];
46 0           undef $start;
47             }
48 0   0       $start //= $i;
49 0           $type = 'blank';
50             } elsif (/^\s{4,}\S+/ && (!$i || $type eq 'verbatim' ||
51             (@para && $para[-1][0] eq 'blank'))) {
52 0 0 0       if (defined($start) && $type ne 'verbatim') {
53 0           push @para, [$type, [@lines[$start..$i-1]]];
54 0           undef $start;
55             }
56 0   0       $start //= $i;
57 0           $type = 'verbatim';
58             } else {
59 0 0 0       if (defined($start) && $type ne 'normal') {
60 0           push @para, [$type, [@lines[$start..$i-1]]];
61 0           undef $start;
62             }
63 0   0       $start //= $i;
64 0           $type = 'normal';
65             }
66             #$log->warnf("i=%d, lines=%s, start=%s, type=%s",
67             # $i, $_, $start, $type);
68 0           $i++;
69             }
70 0 0 0       if (@para && $para[-1][0] eq $type) {
71 0           push @{ $para[-1][1] }, [$type, [@lines[$start..$i-1]]];
  0            
72             } else {
73 0           push @para, [$type, [@lines[$start..$i-1]]];
74             }
75             #$log->warnf("para=%s", \@para);
76              
77 0           for my $para (@para) {
78 0 0         if ($para->[0] eq 'blank') {
79 0           push @{$self->doc_lines}, @{$para->[1]};
  0            
  0            
80             } else {
81 0 0         if ($para->[0] eq 'normal') {
82 0           for (@{$para->[1]}) {
  0            
83 0           s/\n/ /g;
84             }
85 0           $para->[1] = [join("", @{$para->[1]}) . "\n"];
  0            
86             }
87             #$log->warnf("para=%s", $para);
88 0   0       local $Text::Wrap::columns = $ENV{COLUMNS} // 80;
89 0           push @{$self->doc_lines},
90 0           Text::Wrap::wrap($indent, $indent, @{$para->[1]});
  0            
91             }
92             }
93             } else {
94 0           push @{$self->doc_lines},
95 0           map {"$indent$_"} @lines;
  0            
96             }
97             }
98              
99             1;
100              
101             # ABSTRACT: Provide add_doc_lines() to add text with optional text wrapping
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =head1 NAME
110              
111             Perinci::To::Doc::Role::Section::AddTextLines - Provide add_doc_lines() to add text with optional text wrapping
112              
113             =head1 VERSION
114              
115             This document describes version 0.875 of Perinci::To::Doc::Role::Section::AddTextLines (from Perl distribution Perinci-To-Doc), released on 2021-02-21.
116              
117             =head1 DESCRIPTION
118              
119             This role provides C<add_doc_lines()> which can add optionally wrapped text to
120             C<doc_lines>.
121              
122             The default column width for wrapping is from C<COLUMNS> environment variable,
123             or 80.
124              
125             =head1 REQUIRES
126              
127             These methods are provided by, e.g. L<Perinci::To::Doc::Role::Section>.
128              
129             =head2 $o->doc_lines()
130              
131             =head2 $o->doc_indent_level()
132              
133             =head2 $o->doc_lines_str()
134              
135             =head1 ATTRIBUTES
136              
137             =head2 doc_wrap => BOOL (default: 1)
138              
139             Whether to do text wrapping.
140              
141             =head1 METHODS
142              
143             =head2 $o->add_doc_lines([\%opts, ]@lines)
144              
145             Add lines of text, optionally wrapping each line if wrapping is enabled.
146              
147             Available options:
148              
149             =over 4
150              
151             =item * wrap => BOOL
152              
153             Whether to enable wrapping. Default is from the C<doc_wrap> attribute.
154              
155             =back
156              
157             =head1 ENVIRONMENT
158              
159             =head2 COLUMNS => INT
160              
161             Used to set column width.
162              
163             =head1 HOMEPAGE
164              
165             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-To-Doc>.
166              
167             =head1 SOURCE
168              
169             Source repository is at L<https://github.com/perlancar/perl-Perinci-To-Doc>.
170              
171             =head1 BUGS
172              
173             Please report any bugs or feature requests on the bugtracker website L<https://github.com/perlancar/perl-Perinci-To-Doc/issues>
174              
175             When submitting a bug or request, please include a test-file or a
176             patch to an existing test-file that illustrates the bug or desired
177             feature.
178              
179             =head1 SEE ALSO
180              
181             L<Perinci::To::Doc::Role::Section>
182              
183             =head1 AUTHOR
184              
185             perlancar <perlancar@cpan.org>
186              
187             =head1 COPYRIGHT AND LICENSE
188              
189             This software is copyright (c) 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013 by perlancar@cpan.org.
190              
191             This is free software; you can redistribute it and/or modify it under
192             the same terms as the Perl 5 programming language system itself.
193              
194             =cut