File Coverage

lib/Pod/PseudoPod/DOM/TableOfContents.pm
Criterion Covered Total %
statement 27 70 38.5
branch 0 8 0.0
condition n/a
subroutine 9 16 56.2
pod 0 7 0.0
total 36 101 35.6


line stmt bran cond sub pod time code
1             package Pod::PseudoPod::DOM::TableOfContents;
2             # ABSTRACT: table of contents for a PPDOM Corpus
3              
4 1     1   8 use strict;
  1         2  
  1         39  
5 1     1   5 use warnings;
  1         11  
  1         30  
6              
7 1     1   8 use Moose;
  1         4  
  1         8  
8              
9             has 'contents',
10             is => 'ro',
11             default => sub { Pod::PseudoPod::DOM::TableOfContents::TopContents->new };
12              
13             sub add_document
14             {
15 0     0 0   my ($self, $document) = @_;
16 0           my $headings = $document->extract_headings( max_depth => 5 );
17 0           my @stack = ($self->contents);
18 0           my $current_level = 0;
19 0           my $class = 'Pod::PseudoPod::DOM::TableOfContents::Entry';
20 0           my $last_entry;
21              
22 0           for my $heading (@$headings)
23             {
24 0           my $level = $heading->level;
25 0           my $entry = $class->new( heading => $heading );
26              
27 0 0         if ($level == $current_level)
    0          
28             {
29 0           $stack[-1]->add( $entry );
30             }
31             elsif ($level > $current_level)
32             {
33 0           $last_entry->add( $entry );
34 0           push @stack, $last_entry;
35             }
36             else
37             {
38 0           my $diff = $current_level - $level;
39 0           $last_entry = pop @stack for 1 .. $diff;
40 0           $stack[-1]->add( $entry );
41             }
42              
43 0           $last_entry = $entry;
44 0           $current_level = $level;
45             }
46             }
47              
48             sub emit_toc
49             {
50 0     0 0   my $self = shift;
51 0           return "<ul>\n" . $self->contents->emit . "</ul>\n";
52             }
53              
54             __PACKAGE__->meta->make_immutable;
55              
56             package Pod::PseudoPod::DOM::TableOfContents::TopContents;
57              
58 1     1   8167 use strict;
  1         2  
  1         24  
59 1     1   6 use warnings;
  1         2  
  1         38  
60              
61 1     1   10 use Moose;
  1         2  
  1         6  
62              
63             has 'kids', is => 'ro', default => sub { [] };
64              
65             sub add
66             {
67 0     0 0   my ($self, $entry) = @_;
68 0           push @{ $self->kids }, $entry;
  0            
69             }
70              
71             sub emit
72             {
73 0     0 0   my $self = shift;
74 0           my $kids = $self->kids;
75 0 0         return '' unless @$kids;
76              
77 0           my $contents = '';
78 0           for my $kid (@$kids)
79             {
80 0           $contents .= '<h2>' . $kid->heading->get_heading_link . "</h2>\n"
81             . $kid->emit_kids;
82             }
83              
84 0           return $contents;
85             }
86              
87             __PACKAGE__->meta->make_immutable;
88              
89             package Pod::PseudoPod::DOM::TableOfContents::Entry;
90              
91 1     1   7190 use strict;
  1         4  
  1         25  
92 1     1   4 use warnings;
  1         3  
  1         38  
93              
94 1     1   7 use Moose;
  1         1  
  1         5  
95              
96             has 'kids', is => 'ro', default => sub { [] };
97             has 'heading', is => 'ro', required => 1;
98              
99             sub add
100             {
101 0     0 0   my ($self, $entry) = @_;
102 0           push @{ $self->kids }, $entry;
  0            
103             }
104              
105             sub emit_kids
106             {
107 0     0 0   my $self = shift;
108 0           my $kids = $self->kids;
109 0 0         return '' unless @$kids;
110 0           my $contents = "<ul>\n";
111              
112 0           for my $kid (@$kids)
113             {
114 0           $contents .= $kid->emit;
115             }
116              
117 0           return $contents . "\n</ul>\n";
118             }
119              
120             sub emit
121             {
122 0     0 0   my $self = shift;
123 0           my $kids = $self->kids;
124 0           return "<li>" . $self->heading->get_heading_link
125             . $self->emit_kids . "</li>\n";
126             }
127              
128             __PACKAGE__->meta->make_immutable;
129              
130             __END__
131              
132             =pod
133              
134             =encoding UTF-8
135              
136             =head1 NAME
137              
138             Pod::PseudoPod::DOM::TableOfContents - table of contents for a PPDOM Corpus
139              
140             =head1 VERSION
141              
142             version 1.20210620.2004
143              
144             =head1 AUTHOR
145              
146             chromatic <chromatic@wgz.org>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is copyright (c) 2021 by chromatic.
151              
152             This is free software; you can redistribute it and/or modify it under
153             the same terms as the Perl 5 programming language system itself.
154              
155             =cut