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