File Coverage

blib/lib/Pod/Elemental/Document.pm
Criterion Covered Total %
statement 46 56 82.1
branch 5 10 50.0
condition 3 8 37.5
subroutine 9 9 100.0
pod 0 2 0.0
total 63 85 74.1


line stmt bran cond sub pod time code
1             package Pod::Elemental::Document;
2             # ABSTRACT: a pod document
3             $Pod::Elemental::Document::VERSION = '0.103005';
4 12     12   66420 use Moose;
  12         459239  
  12         84  
5             with 'Pod::Elemental::Node';
6              
7 12     12   80348 use Class::Load ();
  12         31  
  12         257  
8 12     12   679 use namespace::autoclean;
  12         7950  
  12         85  
9              
10 12     12   5692 use Pod::Elemental::Element::Generic::Blank;
  12         45  
  12         531  
11 12     12   6508 use String::RewritePrefix;
  12         13329  
  12         91  
12              
13             #pod =head1 OVERVIEW
14             #pod
15             #pod Pod::Elemental::Document is a container for Pod documents. It performs
16             #pod L<Pod::Elemental::Node> but I<not> L<Pod::Elemental::Paragraph>.
17             #pod
18             #pod Documents are used almost exclusively to give a small amount of behavior to
19             #pod arrayrefs of paragraphs, and have few methods of their own.
20             #pod
21             #pod =cut
22              
23             sub _expand_name {
24 32     32   65 my ($self, $name) = @_;
25              
26 32         123 return String::RewritePrefix->rewrite(
27             {
28             '' => 'Pod::Elemental::Element::',
29             '=' => ''
30             },
31             $name,
32             );
33             }
34              
35             sub as_pod_string {
36 4     4 0 1154 my ($self) = @_;
37              
38 4         13 my $str = join q{}, map { $_->as_pod_string } @{ $self->children };
  41         118  
  4         134  
39              
40 4 50       42 $str = "=pod\n\n$str" unless $str =~ /\A=pod\n/;
41 4 50       36 $str .= "=cut\n" unless $str =~ /=cut\n+\z/;
42              
43 4         36 return $str;
44             }
45              
46             sub as_debug_string {
47             return 'Document'
48             }
49              
50             sub _elem_from_lol_entry {
51 16     16   42 my ($self, $entry) = @_;
52 16         41 my ($type, $content, $arg) = @$entry;
53 16   50     63 $arg ||= {};
54              
55 16 100       56 if (! defined $type) {
    50          
56 7   50     34 my $n_class = $self->_expand_name($arg->{class} || 'Generic::Text');
57 7         356 Class::Load::load_class($n_class);
58 7         443 return $n_class->new({ content => "$content\n" });
59             } elsif ($type =~ /\A=(\w+)\z/) {
60 9         26 my $command = $1;
61 9   50     32 my $n_class = $self->_expand_name($arg->{class} || 'Generic::Command');
62 9         504 Class::Load::load_class($n_class);
63 9         596 return $n_class->new({
64             command => $command,
65             content => "$content\n"
66             });
67             } else {
68 0   0     0 my $n_class = $self->_expand_name($arg->{class} || 'Pod5::Region');
69 0         0 Class::Load::load_class($n_class);
70              
71 0         0 my @children;
72              
73 0         0 for my $child (@$content) {
74 0         0 push @children, $self->_elem_from_lol_entry($child);
75             } continue {
76 0         0 my $blank = $self->_expand_name('Generic::Blank');
77 0         0 push @children, $blank->new({ content => "\n" });
78             }
79              
80             pop @children
81 0         0 while $children[-1]->isa('Pod::Elemental::Element::Generic::Blank');
82              
83 0         0 my ($colon, $target) = $type =~ /\A(:)?(.+)\z/;
84              
85 0 0       0 return $n_class->new({
86             format_name => $target,
87             is_pod => $colon ? 1 : 0,
88             content => "\n",
89             children => \@children,
90             })
91             }
92             }
93              
94             sub new_from_lol {
95 1     1 0 191 my ($class, $lol) = @_;
96              
97 1         40 my $self = $class->new;
98              
99 1         2 my @children;
100 1         6 ENTRY: for my $entry (@$lol) {
101 16         39 my $elem = $self->_elem_from_lol_entry($entry);
102 16         37 push @children, $elem;
103             } continue {
104 16         37 my $blank = $self->_expand_name('Generic::Blank');
105 16         1486 push @children, $blank->new({ content => "\n" });
106             }
107              
108 1         3 push @{ $self->children }, @children;
  1         32  
109              
110 1         6 return $self;
111             }
112              
113             __PACKAGE__->meta->make_immutable;
114              
115             1;
116              
117             __END__
118              
119             =pod
120              
121             =encoding UTF-8
122              
123             =head1 NAME
124              
125             Pod::Elemental::Document - a pod document
126              
127             =head1 VERSION
128              
129             version 0.103005
130              
131             =head1 OVERVIEW
132              
133             Pod::Elemental::Document is a container for Pod documents. It performs
134             L<Pod::Elemental::Node> but I<not> L<Pod::Elemental::Paragraph>.
135              
136             Documents are used almost exclusively to give a small amount of behavior to
137             arrayrefs of paragraphs, and have few methods of their own.
138              
139             =head1 AUTHOR
140              
141             Ricardo SIGNES <rjbs@cpan.org>
142              
143             =head1 COPYRIGHT AND LICENSE
144              
145             This software is copyright (c) 2020 by Ricardo SIGNES.
146              
147             This is free software; you can redistribute it and/or modify it under
148             the same terms as the Perl 5 programming language system itself.
149              
150             =cut