Gantry/Utils/PODViewer.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 153 CC 9 SCAR 32.8
CC
9
Cov
0%
do_main
72.0
_collect_pm_files
6.0
namespace
2.0
CRAP 26.7
1 package Gantry::Utils::PODViewer;
2  
3 use strict;
4 use Gantry qw/-TemplateEngine=TT/;
5  
6 our @ISA = ( 'Gantry' );
7  
8 use Gantry::Utils::HTML;
9 use Pod::POM::View::HTML;
10 use Pod::POM;
11 use File::Find;
12 use Pod::Pdf;
13 our $app_rootp = '';
14  
15 #-------------------------------------------------
16 # $self->namespace();
17 #-------------------------------------------------
18 sub namespace {
19 return 'podviewer';
20 }
21  
22 #-------------------------------------------------
23 # $self->init( $self );
24 #-------------------------------------------------
25 sub init {
26 my( $self, $r ) = @_;
27
28 $self->SUPER::init( $r );
29
30 $self->{__POD_DIR__} = $self->fish_config( 'pod_dir' );
31 $app_rootp = $self->app_rootp;
32  
33 } # end init
34  
35 #-------------------------------------------------
36 # $self->do_main( $file);
37 #-------------------------------------------------
38 sub do_main {
39 my( $self, $file ) = @_;
40
41 my %p = $self->get_param_hash;
42 $file ||= $p{file};
43
44 my $module = $file;
45
46 my $DO_PDF = 0;
47
48 if ( $file =~ /\.pdf$/ ) {
49 $file =~ s/\.pdf$//;
50 $DO_PDF=1;
51 }
52
53 $self->stash->view->template( 'pod.tt' );
54 $self->stash->view->title( $file );
55
56 my @path = split( /\//, $self->{__POD_DIR__} );
57 my $base_module = $path[-1];
58  
59 my $p = Pod::POM->new();
60
61 $file = '' if $file eq $base_module;
62 $file =~ s/::/\//g;
63 $file = '/' . $file if $file;
64
65 my $base_module_data;
66 { local $/ = undef;
67 local *FILE;
68 open FILE, "<$$self{__POD_DIR__}.pm"
69 or die "unable to open $base_module";
70 $base_module_data = <FILE>;
71 close FILE
72 }
73
74 my( $base_module_version )
75 = ( $base_module_data =~ /\$VERSION\s*=\s*'?"?([0-9\.]+)/is );
76
77 if ( $DO_PDF ) {
78 $self->template_disable( 1 );
79 $self->content_type( 'application/pdf' );
80 my $f = ( $self->{__POD_DIR__} . "${file}.pm" );
81
82 my $pdf;
83 eval {
84 $pdf = pod2pdf( '--paper=usletter', $f );
85
86 };
87 if ( $@ ) {
88 $self->content_type( 'text/plain' );
89 return( $@ );
90 }
91
92 return( $pdf );
93  
94 }
95
96 my $pom;
97 if ( -e $self->{__POD_DIR__} . "${file}.pm" ) {
98
99 $pom = $p->parse_file(
100 ( $self->{__POD_DIR__} . "${file}.pm" )
101 ) or die "$!";
102
103 }
104 elsif ( -e $self->{__POD_DIR__} . "${file}.pod" ) {
105  
106 $pom = $p->parse_file(
107 ( $self->{__POD_DIR__} . "${file}.pod" )
108 ) or die "$!";
109  
110 }
111 else {
112 die "unknown module";
113 }
114
115 my $location = $self->location;
116
117 my $d = My::View->print($pom);
118 $d =~ s/<\/?body.*?>//ig;
119 $d =~ s/<\/?html>//ig;
120 #$d =~ s/(${base_module}::)((\w+)?(::\w+)*)/<a href="$location\/main\/$2">$1$2<\/a>/g;
121 #$d =~ s/(${base_module}\(3\))/<a href="$location\/main\/$base_module">$1<\/a>/g;
122 $d =~ s/<h1>(\w+)\s*<\/h1>/<h1><a style="text-decoration: none" name="$1">$1<\/a><\/h1>/g;
123  
124 my @headings;
125 foreach my $sec ( $pom->head1() ) {
126 my $stitle = $sec->title;
127 $stitle =~ s/\s+/&nbsp;/g;
128 push( @headings,
129 ( "<a href=\"#" . $sec->title . "\">$stitle</a> " ) );
130 }
131
132 my @pm_files = _collect_pm_files( $self->{__POD_DIR__} );
133
134 $self->stash->view->data( {
135 base_module_version => $base_module_version,
136 base_module => $base_module,
137 module_name => $module,
138 files => \@pm_files,
139 headings => \@headings,
140 html => $d
141 }
142 );
143  
144 } # end do_main
145  
146 #-------------------------------------------------
147 # $self->collect_pm_files( $dir );
148 #-------------------------------------------------
149 sub _collect_pm_files {
150 my( $dir ) = @_;
151
152 my @files;
153
154 find({
155 wanted => sub {
156 my $file = $File::Find::name;
157 $file =~ s/$dir//;
158 push( @files, $file ) if $_ =~ /(\.pm|\.pod)$/;
159 },
160 follow => 1
161 },
162 $dir
163 );
164
165 return( sort( @files ) );
166 }
167  
168 #-------------------------------------------------
169 # PACKAGE My::View
170 #-------------------------------------------------
171 package My::View;
172 use base qw( Pod::POM::View::HTML );
173  
174 #-------------------------------------------------
175 # view_head1
176 #-------------------------------------------------
177 sub view_head1 {
178 my ($self, $item) = @_;
179
180 return(
181 '<a name="', $item->title->present($self), '"></a>',
182 '<h1>',
183 $item->title->present($self),
184 "</h1>\n",
185 $item->content->present($self)
186 );
187 }
188  
189 #-------------------------------------------------
190 # view_head2
191 #-------------------------------------------------
192 sub view_head2 {
193 my ($self, $item) = @_;
194
195 return(
196 '<a name="', $item->title->present($self), '"></a>',
197 '<h2>',
198 $item->title->present($self),
199 "</h2>\n",
200 $item->content->present($self)
201 );
202 }
203  
204 sub view_seq_link_transform_path {
205 my($self, $page) = @_;
206  
207 # right now the default transform doesn't check whether the link
208 # is not dead (i.e. whether there is a corresponding file.
209 # therefore we don't link L<>'s other than L<http://>
210 # subclass to change the default (and of course add validation)
211  
212 # this is the minimal transformation that will be required if enabled
213 # $page = "$page.html";
214 # $page =~ s|::|/|g;
215 #print "page $page\n";
216 $page =~ s/^\w+\:\://;
217 return( $app_rootp . "/main/" . $page );
218 }
219  
220  
221 1;
222  
223 =head1 NAME
224  
225 Gantry::Utils::PODViewer - PODViewer application
226  
227 =head1 SYNOPSIS
228  
229 This module is a POD viewing application. The module expects only the path
230 to an installed Perl module.
231  
232 =head2 In mod_perl
233  
234 If the deployment method is mod_perl:
235  
236 <Perl>
237 use Gantry::Utils::PODViewer qw/-Engine=MP20 -TemplateEngine=TT
238 -PluginNamespace=podviewer/;
239 </Perl>
240  
241 <Location /pod/gantry>
242 PerlSetVar pod_dir '/usr/lib/perl5/site_perl/5.8.5/Gantry'
243 PerlSetVar app_rootp '/pod/gantry'
244 PerlSetVar template_wrapper 'pod_wrapper.tt'
245 PerlSetVar css_rootp '/style'
246 PerlSetVar img_rootp '/images'
247  
248 SetHandler perl-script
249 PerlHandler Gantry::Utils::PODViewer
250  
251 </Location>
252  
253 =head2 In CGI
254  
255 On the other hand, if the deployment method is CGI:
256  
257 #!/usr/local/bin/perl
258 use strict;
259  
260 use CGI::Carp qw(fatalsToBrowser);
261 use Gantry::Utils::PODViewer qw( -Engine=CGI -TemplateEngine=TT );
262 use Gantry::Engine::CGI;
263  
264 my $cgi = Gantry::Engine::CGI->new( {
265 locations => {
266 '/' => 'Gantry::Utils::PODViewer',
267 },
268 config => {
269 pod_dir => '/home/gantry/perl/lib/lib/Gantry',
270 app_rootp => '/cgi-bin/gantry.cgi',
271 root => '/home/gantry/templates',
272 template_wrapper => 'pod_wrapper.tt',
273 }
274 } );
275  
276 $cgi->dispatch;
277  
278 =head1 METHODS
279  
280 =over 4
281  
282 =item do_main
283  
284 =item init
285  
286 =item namespace
287  
288 =back
289  
290  
291 =head1 AUTHOR
292  
293 Tim Keefer <tkeefer@gmail.com>
294  
295 =head1 COPYRIGHT and LICENSE
296  
297 Copyright (c) 2005-6, Tim Keefer.
298  
299 This library is free software; you can redistribute it and/or modify
300 it under the same terms as Perl itself, either Perl version 5.8.6 or,
301 at your option, any later version of Perl 5 you may have available.
302  
303 =cut
304  
305  
306