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