File Coverage

lib/Pod/PseudoPod/Book/Command/buildchapters.pm
Criterion Covered Total %
statement 15 82 18.2
branch 0 12 0.0
condition n/a
subroutine 5 15 33.3
pod 1 10 10.0
total 21 119 17.6


line stmt bran cond sub pod time code
1             package Pod::PseudoPod::Book::Command::buildchapters;
2             # ABSTRACT: command module for C<ppbook buildchapters>
3              
4 2     2   13538 use strict;
  2         7  
  2         75  
5 2     2   13 use warnings;
  2         23  
  2         129  
6              
7 2     2   14 use parent 'Pod::PseudoPod::Book::Command';
  2         5  
  2         23  
8              
9 2     2   1413 use autodie;
  2         30811  
  2         11  
10 2     2   15154 use File::Spec::Functions qw( catfile catdir splitpath );
  2         6  
  2         2767  
11              
12             sub execute
13             {
14 0     0 1   my $self = shift;
15 0           my $conf = $self->config;
16              
17 0 0         return $self->process_chapters unless $conf->{book}{build_chapters};
18 0           return $self->weave_chapters;
19             }
20              
21             sub weave_chapters
22             {
23 0     0 0   my ($self, $opt, $args) = @_;
24              
25 0           my $sections_href = $self->get_section_list;
26 0 0         die "No sections\n" unless keys %$sections_href;
27              
28 0           for my $chapter ($self->get_chapter_list)
29             {
30 0           my $text = process_chapter( $chapter, $sections_href );
31 0           $self->write_chapter( $chapter, $text );
32             }
33              
34 0 0         return unless keys %$sections_href;
35              
36 0           die "Scenes missing from chapters:", join "\n\t", '', keys %$sections_href;
37             }
38              
39             sub process_chapters
40             {
41 0     0 0   my ($self, $opt, $args) = @_;
42 0           my $conf = $self->config;
43 0           my $dir = $conf->{layout}{subchapter_directory};
44 0           my $glob_path = catfile( $dir, '*.pod' );
45              
46 0           for my $chapter ( glob( $glob_path ) )
47             {
48 0           my $text = read_file( $chapter );
49 0           $self->write_chapter( $chapter, $text );
50             }
51             }
52              
53             sub get_chapter_list
54             {
55 0     0 0   my $self = shift;
56 0           my $conf = $self->config;
57 0           my $dir = $conf->{layout}{subchapter_directory};
58 0           my $chapname = $conf->{layout}{chapter_name_prefix};
59 0           my $glob_path = catfile( $dir, $chapname . '_*.pod' );
60 0           return glob( $glob_path );
61             }
62              
63             sub get_section_list
64             {
65 0     0 0   my $self = shift;
66 0           my $conf = $self->config;
67 0           my $dir = $conf->{layout}{subchapter_directory};
68 0           my $chapter_prefix = $conf->{layout}{chapter_name_prefix};
69 0           my $sections_path = catfile( $dir, '*.pod' );
70              
71 0           my %sections;
72              
73 0           for my $section (glob( $sections_path ))
74             {
75 0 0         next if $section =~ /_\d+.pod$/;
76 0           my $anchor = get_anchor( $section );
77 0           $sections{ $anchor } = $section;
78             }
79              
80 0           return \%sections;
81             }
82              
83             sub get_anchor
84             {
85 0     0 0   my $path = shift;
86              
87 0           open my $fh, '<:utf8', $path;
88              
89 0           while (<$fh>)
90             {
91 0 0         next unless /Z<(\w*)>/;
92 0           return $1;
93             }
94              
95 0           die "No anchor for file '$path'\n";
96             }
97              
98             sub process_chapter
99             {
100 0     0 0   my ($path, $sections_href) = @_;
101 0           my $text = read_file( $path );
102              
103 0           $text =~ s/^L<(\w+)>/insert_section( $sections_href, $1, $path )/emg;
  0            
104              
105 0           $text =~ s/(=head1 .*)\n\n=head2 \*{3}/$1/g;
106 0           return $text;
107             }
108              
109             sub read_file
110             {
111 0     0 0   my $path = shift;
112 0           open my $fh, '<:utf8', $path;
113              
114 0           return scalar do { local $/; <$fh>; };
  0            
  0            
115             }
116              
117             sub insert_section
118             {
119 0     0 0   my ($sections_href, $name, $chapter) = @_;
120              
121             die "Unknown section '$name' in '$chapter'\n"
122 0 0         unless exists $sections_href->{ $1 };
123              
124 0           my $text = read_file( $sections_href->{ $1 } );
125 0           delete $sections_href->{ $1 };
126 0           return $text;
127             }
128              
129             sub write_chapter
130             {
131 0     0 0   my ($self, $path, $text) = @_;
132 0           my $conf = $self->config;
133 0           my $chapter_build_dir = $conf->{layout}{chapter_build_directory};
134 0           my $name = ( splitpath $path )[-1];
135 0           my $chapter_dir = catdir( 'build', $chapter_build_dir );
136 0           my $chapter_path = catfile( $chapter_dir, $name );
137              
138 0           open my $fh, '>:utf8', $chapter_path;
139              
140 0           print {$fh} $text;
  0            
141             }
142              
143             1;
144              
145             __END__
146              
147             =pod
148              
149             =encoding UTF-8
150              
151             =head1 NAME
152              
153             Pod::PseudoPod::Book::Command::buildchapters - command module for C<ppbook buildchapters>
154              
155             =head1 VERSION
156              
157             version 1.20210620.2051
158              
159             =head1 AUTHOR
160              
161             chromatic <chromatic@wgz.org>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2011 by chromatic.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut