File Coverage

blib/lib/Perinci/To/Doc/Role/Section.pm
Criterion Covered Total %
statement 42 63 66.6
branch 13 22 59.0
condition 4 12 33.3
subroutine 6 9 66.6
pod 6 6 100.0
total 71 112 63.3


line stmt bran cond sub pod time code
1              
2             use 5.010;
3 1     1   6677 use Log::ger;
  1         3  
4 1     1   4 use Moo::Role;
  1         1  
  1         4  
5 1     1   144  
  1         2  
  1         5  
6             has doc_sections => (is=>'rw');
7             has doc_lines => (is => 'rw'); # store final result, array
8             has doc_indent_level => (is => 'rw');
9             has doc_indent_str => (is => 'rw', default => sub{" "}); # indent characters
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, $name, $before) = @_;
17             my $ss = $self->doc_sections;
18 3     3 1 1069 return unless $ss;
19 3         7 my $i = 0;
20 3 50       6 my $added;
21 3         3 while ($i < @$ss && defined($before)) {
22 3         5 if ($ss->[$i] eq $before) {
23 3   66     13 my $pos = $i;
24 8 100       12 splice @$ss, $pos, 0, $name;
25 2         2 $added++;
26 2         5 last;
27 2         3 }
28 2         3 $i++;
29             }
30 6         14 unshift @$ss, $name unless $added;
31             }
32 3 100       9  
33             my ($self, $name, $after) = @_;
34             my $ss = $self->doc_sections;
35             return unless $ss;
36 3     3 1 1234 my $i = 0;
37 3         7 my $added;
38 3 50       6 while ($i < @$ss && defined($after)) {
39 3         3 if ($ss->[$i] eq $after) {
40 3         4 my $pos = $i+1;
41 3   66     10 splice @$ss, $pos, 0, $name;
42 11 100       18 $added++;
43 2         3 last;
44 2         5 }
45 2         9 $i++;
46 2         3 }
47             push @$ss, $name unless $added;
48 9         17 }
49              
50 3 100       9 my ($self, $name) = @_;
51             my $ss = $self->doc_sections;
52             return unless $ss;
53             my $i = 0;
54 3     3 1 1191 while ($i < @$ss) {
55 3         7 if ($ss->[$i] eq $name) {
56 3 50       5 splice @$ss, $i, 1;
57 3         4 } else {
58 3         6 $i++;
59 6 100       12 }
60 2         5 }
61             }
62 4         7  
63             my ($self, $n) = @_;
64             $n //= 1;
65             $self->{doc_indent_level} += $n;
66             }
67              
68 0     0 1   my ($self, $n) = @_;
69 0   0       $n //= 1;
70 0           $self->{doc_indent_level} -= $n;
71             die "BUG: Negative doc indent level" unless $self->{doc_indent_level} >=0;
72             }
73              
74 0     0 1   my ($self, %opts) = @_;
75 0   0       log_trace("-> gen_doc(opts=%s)", \%opts);
76 0            
77 0 0         $self->doc_lines([]);
78             $self->doc_indent_level(0);
79              
80             $self->before_gen_doc(%opts) if $self->can("before_gen_doc");
81 0     0 1    
82 0           for my $s (@{ $self->doc_sections // [] }) {
83             my $meth = "gen_doc_section_$s";
84 0           log_trace("=> $meth(%s)", \%opts);
85 0           $self->$meth(%opts);
86             }
87 0 0          
88             $self->after_gen_doc(%opts) if $self->can("after_gen_doc");
89 0   0        
  0            
90 0           log_trace("<- gen_doc()");
91 0           join("", @{ $self->doc_lines });
92 0           }
93              
94             1;
95 0 0         # ABSTRACT: Role for class that generates documentation with sections
96              
97 0            
98 0           =pod
  0            
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             Perinci::To::Doc::Role::Section - Role for class that generates documentation with sections
105              
106             =head1 VERSION
107              
108             This document describes version 0.879 of Perinci::To::Doc::Role::Section (from Perl distribution Perinci-To-Doc), released on 2022-05-14.
109              
110             =head1 DESCRIPTION
111              
112             This is a role for classes that produce documentation with sections. This role
113             provides a workflow for parsing and generating sections, regulating indentation,
114             and a C<gen_doc()> method.
115              
116             To generate documentation, first you provide a list of section names in
117             C<doc_sections>. Then you run C<gen_doc()>, which will call
118             C<gen_doc_section_SECTION()> method for each section consecutively, which is
119             supposed to append lines of text to C<doc_lines>. Finally all the added lines is
120             concatenated together and returned by C<gen_doc()>.
121              
122             =head1 ATTRIBUTES
123              
124             =head2 doc_sections => ARRAY
125              
126             Should be set to the names of available sections.
127              
128             =head2 doc_lines => ARRAY
129              
130             =head2 doc_indent_level => INT
131              
132             =head2 doc_indent_str => STR (default ' ' (two spaces))
133              
134             Character(s) used for indent.
135              
136             =head1 METHODS
137              
138             =head2 add_doc_section_before($name, $anchor)
139              
140             =head2 add_doc_section_after($name, $anchor)
141              
142             =head2 delete_doc_section($name)
143              
144             =head2 inc_doc_indent([N])
145              
146             =head2 dec_doc_indent([N])
147              
148             =head2 gen_doc() => STR
149              
150             Generate documentation.
151              
152             The method will first initialize C<doc_lines> to an empty array C<[]> and
153             C<doc_indent_level> to 0.
154              
155             It will then call C<before_gen_doc> if the hook method exists, to allow class to
156             do stuffs prior to document generation. L<Perinci::To::Text> uses this, for
157             example, to retrieve metadata from Riap server.
158              
159             Then, as described in L</"DESCRIPTION">, for each section listed in
160             C<doc_sections> it will call C<gen_doc_section_SECTION>.
161              
162             After that, it will call C<after_gen_doc> if the hook method exists, to allow
163             class to do stuffs after document generation.
164              
165             Lastly, it returns concatenated C<doc_lines>.
166              
167             =head1 HOMEPAGE
168              
169             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-To-Doc>.
170              
171             =head1 SOURCE
172              
173             Source repository is at L<https://github.com/perlancar/perl-Perinci-To-Doc>.
174              
175             =head1 SEE ALSO
176              
177             This role is used, among others, by: C<Perinci::To::*> modules.
178              
179             L<Perinci::To::Doc::Role::Section::AddTextLines> which provides C<add_doc_lines>
180             to add text with optional text wrapping.
181              
182             =head1 AUTHOR
183              
184             perlancar <perlancar@cpan.org>
185              
186             =head1 CONTRIBUTING
187              
188              
189             To contribute, you can send patches by email/via RT, or send pull requests on
190             GitHub.
191              
192             Most of the time, you don't need to build the distribution yourself. You can
193             simply modify the code, then test via:
194              
195             % prove -l
196              
197             If you want to build the distribution (e.g. to try to install it locally on your
198             system), you can install L<Dist::Zilla>,
199             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
200             Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
201             beyond that are considered a bug and can be reported to me.
202              
203             =head1 COPYRIGHT AND LICENSE
204              
205             This software is copyright (c) 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013 by perlancar <perlancar@cpan.org>.
206              
207             This is free software; you can redistribute it and/or modify it under
208             the same terms as the Perl 5 programming language system itself.
209              
210             =head1 BUGS
211              
212             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-To-Doc>
213              
214             When submitting a bug or request, please include a test-file or a
215             patch to an existing test-file that illustrates the bug or desired
216             feature.
217              
218             =cut