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;
2             # ABSTRACT: a region of Pod (this role likely to be removed)
3             $Pod::Elemental::Element::Pod5::Region::VERSION = '0.103005';
4 11     11   88 use Moose;
  11         30  
  11         86  
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   74313 use Pod::Elemental::Types qw(FormatName);
  11         29  
  11         107  
29 11     11   12418 use MooseX::Types::Moose qw(Bool);
  11         28  
  11         120  
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 325 sub command { 'begin' }
59 9     9 0 283 sub closing_command { 'end' }
60              
61             sub _display_as_for {
62 10     10   25 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       338 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       16 return if @{ $self->children } != 1;
  8         243  
71              
72 4         114 my $child = $self->children->[0];
73              
74 4 100       125 return if $child->content =~ m{^\s*$}m;
75              
76 2         8 my $base = 'Pod::Elemental::Element::Pod5::';
77 2 100 66     73 return 1 if $self->is_pod and $child->isa("${base}Ordinary");
78 1 50 33     33 return 1 if ! $self->is_pod and $child->isa("${base}Data");
79              
80 1         4 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   25 my ($self) = @_;
101              
102 9         297 my $content = $self->content;
103 9 100       306 my $colon = $self->is_pod ? ':' : '';
104              
105 9 100       32 my $string = sprintf "=%s %s%s\n",
106             $self->command,
107             $colon . $self->format_name,
108             ($content =~ /\S/ ? " $content\n" : "\n");
109              
110 9         23 $string .= join(q{}, map { $_->as_pod_string } @{ $self->children });
  20         73  
  9         279  
111              
112             $string .= "\n\n"
113 9 100 100     20 if @{ $self->children }
  9         278  
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         28 $string .= sprintf "=%s %s",
118             $self->closing_command,
119             $colon . $self->format_name;
120              
121 9         27 return $string;
122             }
123              
124             sub __as_pod_string_for {
125 1     1   3 my ($self) = @_;
126              
127 1         33 my $content = $self->content;
128 1 50       31 my $colon = $self->is_pod ? ':' : '';
129              
130 1         32 my $string = sprintf "=for %s %s",
131             $colon . $self->format_name,
132             $self->children->[0]->as_pod_string;
133              
134 1         4 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   62297 use Pod::Elemental::Types qw(ChompedString);
  11         33  
  11         58  
154             has '+content' => (coerce => 1, isa => ChompedString);
155             # END Autochomp Replacement
156              
157             __PACKAGE__->meta->make_immutable;
158 11     11   12638 no Moose;
  11         37  
  11         74  
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.103005
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 ATTRIBUTES
185              
186             =head2 format_name
187              
188             This is the format to which the region was targeted.
189              
190             B<Note!> The format name should I<not> include the leading colon to indicate a
191             pod paragraph. For that, see C<L</is_pod>>.
192              
193             =head2 is_pod
194              
195             If true, this region contains pod (ordinary or verbatim) paragraphs, as opposed
196             to data paragraphs. This will generally result from the document originating
197             in a C<=begin> block with a colon-prefixed target identifier:
198              
199             =begin :html
200              
201             This is still a verbatim paragraph.
202              
203             =end :html
204              
205             =head1 WARNING
206              
207             This class is somewhat sketchy and may be refactored somewhat in the future,
208             specifically to refactor its similarities to
209             L<Pod::Elemental::Element::Nested>.
210              
211             =head1 AUTHOR
212              
213             Ricardo SIGNES <rjbs@cpan.org>
214              
215             =head1 COPYRIGHT AND LICENSE
216              
217             This software is copyright (c) 2020 by Ricardo SIGNES.
218              
219             This is free software; you can redistribute it and/or modify it under
220             the same terms as the Perl 5 programming language system itself.
221              
222             =cut