File Coverage

blib/lib/Pod/Elemental/Element/Pod5/Region.pm
Criterion Covered Total %
statement 43 43 100.0
branch 16 18 88.8
condition 6 9 66.6
subroutine 10 10 100.0
pod 0 2 0.0
total 75 82 91.4


line stmt bran cond sub pod time code
1             package Pod::Elemental::Element::Pod5::Region 0.103006;
2             # ABSTRACT: a region of Pod (this role likely to be removed)
3              
4 11     11   74 use Moose;
  11         20  
  11         77  
5             with qw(
6             Pod::Elemental::Paragraph
7             Pod::Elemental::Node
8             Pod::Elemental::Command
9             );
10              
11             #pod =head1 WARNING
12             #pod
13             #pod This class is somewhat sketchy and may be refactored somewhat in the future,
14             #pod specifically to refactor its similarities to
15             #pod L<Pod::Elemental::Element::Nested>.
16             #pod
17             #pod =head1 OVERVIEW
18             #pod
19             #pod A Pod5::Region element represents a region marked by a C<=for> command or a
20             #pod pair of C<=begin> and C<=end> commands. It may have content of its own as well
21             #pod as child paragraphs.
22             #pod
23             #pod Its C<as_pod_string> method will emit either a C<=begin/=end>-enclosed string
24             #pod or a C<=for> command, based on whichever is permissible.
25             #pod
26             #pod =cut
27              
28 11     11   65151 use Pod::Elemental::Types qw(FormatName);
  11         37  
  11         96  
29 11     11   10634 use MooseX::Types::Moose qw(Bool);
  11         27  
  11         133  
30              
31             #pod =attr format_name
32             #pod
33             #pod This is the format to which the region was targeted.
34             #pod
35             #pod B<Note!> The format name should I<not> include the leading colon to indicate a
36             #pod pod paragraph. For that, see C<L</is_pod>>.
37             #pod
38             #pod =cut
39              
40             has format_name => (is => 'ro', isa => FormatName, required => 1);
41              
42             #pod =attr is_pod
43             #pod
44             #pod If true, this region contains pod (ordinary or verbatim) paragraphs, as opposed
45             #pod to data paragraphs. This will generally result from the document originating
46             #pod in a C<=begin> block with a colon-prefixed target identifier:
47             #pod
48             #pod =begin :html
49             #pod
50             #pod This is still a verbatim paragraph.
51             #pod
52             #pod =end :html
53             #pod
54             #pod =cut
55              
56             has is_pod => (is => 'ro', isa => Bool, required => 1, default => 1);
57              
58 10     10 0 246 sub command { 'begin' }
59 9     9 0 274 sub closing_command { 'end' }
60              
61             sub _display_as_for {
62 10     10   17 my ($self) = @_;
63              
64             # Everything after "=for target" becomes the lone child paragraph, so there
65             # is nowhere to put the (technically illegal) content. -- rjbs, 2009-11-24
66 10 100       290 return if $self->content =~ /\S/;
67              
68             # We can't have more than one paragraph, because there'd be a blank, so we
69             # couldn't round trip. -- rjbs, 2009-11-24
70 8 100       15 return if @{ $self->children } != 1;
  8         197  
71              
72 4         94 my $child = $self->children->[0];
73              
74 4 100       105 return if $child->content =~ m{^\s*$}m;
75              
76 2         5 my $base = 'Pod::Elemental::Element::Pod5::';
77 2 100 66     52 return 1 if $self->is_pod and $child->isa("${base}Ordinary");
78 1 50 33     25 return 1 if ! $self->is_pod and $child->isa("${base}Data");
79              
80 1         2 return;
81             }
82              
83             sub as_pod_string {
84             my ($self) = @_;
85              
86             my $string;
87              
88             if ($self->_display_as_for) {
89             $string = $self->__as_pod_string_for($self);
90             } else {
91             $string = $self->__as_pod_string_begin($self);
92             }
93              
94             $string =~ s/\n*\z//g;
95              
96             return $string;
97             }
98              
99             sub __as_pod_string_begin {
100 9     9   16 my ($self) = @_;
101              
102 9         256 my $content = $self->content;
103 9 100       267 my $colon = $self->is_pod ? ':' : '';
104              
105 9 100       26 my $string = sprintf "=%s %s%s\n",
106             $self->command,
107             $colon . $self->format_name,
108             ($content =~ /\S/ ? " $content\n" : "\n");
109              
110 9         18 $string .= join(q{}, map { $_->as_pod_string } @{ $self->children });
  20         123  
  9         217  
111              
112             $string .= "\n\n"
113 9 100 100     19 if @{ $self->children }
  9         220  
114             and $self->children->[-1]->isa( 'Pod::Elemental::Element::Pod5::Data');
115             # Pod5::$self->is_pod; # XXX: HACK!! -- rjbs, 2009-10-21
116              
117 9         30 $string .= sprintf "=%s %s",
118             $self->closing_command,
119             $colon . $self->format_name;
120              
121 9         25 return $string;
122             }
123              
124             sub __as_pod_string_for {
125 1     1   3 my ($self) = @_;
126              
127 1         27 my $content = $self->content;
128 1 50       25 my $colon = $self->is_pod ? ':' : '';
129              
130 1         26 my $string = sprintf "=for %s %s",
131             $colon . $self->format_name,
132             $self->children->[0]->as_pod_string;
133              
134 1         3 return $string;
135             }
136              
137             sub as_debug_string {
138             my ($self) = @_;
139              
140             my $colon = $self->is_pod ? ':' : '';
141              
142             my $string = sprintf "=%s %s",
143             $self->command,
144             $colon . $self->format_name;
145              
146             return $string;
147             }
148              
149             with 'Pod::Elemental::Autoblank';
150             with 'Pod::Elemental::Autochomp';
151              
152             # BEGIN Autochomp Replacement
153 11     11   52831 use Pod::Elemental::Types qw(ChompedString);
  11         26  
  11         49  
154             has '+content' => (coerce => 1, isa => ChompedString);
155             # END Autochomp Replacement
156              
157             __PACKAGE__->meta->make_immutable;
158 11     11   10927 no Moose;
  11         26  
  11         60  
159             1;
160              
161             __END__
162              
163             =pod
164              
165             =encoding UTF-8
166              
167             =head1 NAME
168              
169             Pod::Elemental::Element::Pod5::Region - a region of Pod (this role likely to be removed)
170              
171             =head1 VERSION
172              
173             version 0.103006
174              
175             =head1 OVERVIEW
176              
177             A Pod5::Region element represents a region marked by a C<=for> command or a
178             pair of C<=begin> and C<=end> commands. It may have content of its own as well
179             as child paragraphs.
180              
181             Its C<as_pod_string> method will emit either a C<=begin/=end>-enclosed string
182             or a C<=for> command, based on whichever is permissible.
183              
184             =head1 PERL VERSION
185              
186             This library should run on perls released even a long time ago. It should work
187             on any version of perl released in the last five years.
188              
189             Although it may work on older versions of perl, no guarantee is made that the
190             minimum required version will not be increased. The version may be increased
191             for any reason, and there is no promise that patches will be accepted to lower
192             the minimum required perl.
193              
194             =head1 ATTRIBUTES
195              
196             =head2 format_name
197              
198             This is the format to which the region was targeted.
199              
200             B<Note!> The format name should I<not> include the leading colon to indicate a
201             pod paragraph. For that, see C<L</is_pod>>.
202              
203             =head2 is_pod
204              
205             If true, this region contains pod (ordinary or verbatim) paragraphs, as opposed
206             to data paragraphs. This will generally result from the document originating
207             in a C<=begin> block with a colon-prefixed target identifier:
208              
209             =begin :html
210              
211             This is still a verbatim paragraph.
212              
213             =end :html
214              
215             =head1 WARNING
216              
217             This class is somewhat sketchy and may be refactored somewhat in the future,
218             specifically to refactor its similarities to
219             L<Pod::Elemental::Element::Nested>.
220              
221             =head1 AUTHOR
222              
223             Ricardo SIGNES <cpan@semiotic.systems>
224              
225             =head1 COPYRIGHT AND LICENSE
226              
227             This software is copyright (c) 2022 by Ricardo SIGNES.
228              
229             This is free software; you can redistribute it and/or modify it under
230             the same terms as the Perl 5 programming language system itself.
231              
232             =cut