POD.pm

filter: Statementstmt 40.0% 86 / 215 Branchbran 4.1% 2 / 48 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 77.1% 27 / 35 Podpod 16.6% 1 / 6 totaltotal 34.2% 116 / 339 CC 39 SCAR 38.8
CC
39
Cov
31%
module
132.0
_toc_to_json
110.0
modules
72.0
CRAP 48.4
↑ Summary POD/POM/View.pm »
1 #
2 # This file is part of Catalyst-Controller-POD
3 #
4 # This software is Copyright (c) 2011 by Moritz Onken.
5 #
6 # This is free software, licensed under:
7 #
8 # The (three-clause) BSD License
9 #
10 package Catalyst::Controller::POD;
11 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $Catalyst::Controller::POD::VERSION = '1.0.0';
13 }
14 # ABSTRACT: Serves PODs right from your Catalyst application
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use File::ShareDir qw( dist_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Pod::Simple::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Pod::POM;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use Catalyst::Controller::POD::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 ▶use base "Catalyst::Controller";
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 __PACKAGE__->mk_accessors(qw(_dist_dir inc namespaces self dir show_home_tab initial_module home_tab_content expanded_module_tree));
33  
34 3 __PACKAGE__->config(
35 self => 1,
36 namespaces => ["*"],
37 initial_module => "",
38 show_home_tab => 1,
39 expanded_module_tree => 0,
40 home_tab_content => <<HTML,
41 <div style="width:500px; margin:50px" class='x-box-blue' id='move-me'>
42 <div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>
43 <div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc">
44 <h3 style="margin-bottom:5px;">Search the CPAN</h3>
45 <input type="text" name="search" id="search" class="x-form-text" style='font-size: 20px; height: 31px'/>
46 <div style="padding-top:4px;">Type at least three characters</div>
47 </div></div></div>
48 <div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>
49 </div>
50 HTML
51 );
52  
53 0 ▶sub search : Local {
Subroutine search: not called CC=4 SCAR=30.0
54 0 my ( $self, $c ) = @_;
55 0 my $k = $c->req->param("value");
56 0 my $s = $c->req->param("start");
57 0 my $url = new URI("http://search.cpan.org/search");
58 0 $url->query_form_hash(
59 query => $k,
60 mode => "module",
61 n => 50,
62 format => "xml",
63 s => $s
64 );
65 0 my $ua = new LWP::UserAgent;
66 0 $ua->timeout(15);
67 0 ▶ $c->log->debug("get url ".$url->canonical) if($c->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
68 0 my $response = $ua->get($url);
69 0 my $xml = $response->content;
70 0 my $data;
71 0 eval{ $data = XMLin($xml, keyattr => [] )};
72 0 ▶ if(@$) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$) 0 0 0
73 0 $c->res->body("[]");
74 0 return;
75 }
76 0 my $output = {count => $data->{matches}};
77 0 ▶ while(my($k,$v) = each %{$output->{module}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$output{"module"};}) 0 0 0
78
79 }
80 0 $c->res->body(encode_json($data));
81 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83  
84 0 ▶sub module : Local {
Subroutine module: not called CC=11 SCAR=48.8
Pod undocumented
85 0 my ( $self, $c, $module ) = @_;
86 0 ▶ my $search = Pod::Simple::Search->new->inc( $self->inc || 0 );
Condition0%0/2
$self->inc || 0
l!l
00
MC/DC0%0/1
$self->inc || 0
$self->inc
Truth table0%0/2
A
A= $self->inc
Aresult
11
00
87 0 push( @{ $self->{dirs} }, $c->path_to('lib')->stringify )
88 0 ▶ if ( $self->{self} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"self"} 0 0 0
89 my $name2path =
90 0 $search->limit_glob($module)->survey( @{ $self->{dirs} } );
91 0 my $view = "Catalyst::Controller::POD::POM::View";
92 0 Pod::POM->default_view($view);
93 0 my $parser = Pod::POM->new( warn => 0 );
94 0 $view->_root( $self->_root($c) );
95 0 $view->_module($module);
96 0 my $pom;
97  
98 0 ▶ if ( $name2path->{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name2path->{$module}) { } 0 0 0
99 0 ▶ $c->log->debug("Getting POD from local store") if($c->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
100 0 $view->_toc( _get_toc( $name2path->{$module} ) );
101 0 ▶ $pom = $parser->parse_file( $name2path->{$module} )
Condition0%0/2
$parser->parse_file($name2path->{$module}) || die($parser->error, "\n")
l!l
00
MC/DC0%0/1
$parser->parse_file($name2path->{$module}) || die($parser->error, "\n")
$parser->parse_file($name2path->{$module})
Truth table0%0/2
A
A= $parser->parse_file($name2path->{$module})
Aresult
11
00
102 || die $parser->error(), "\n";
103 } else {
104 0 ▶ $c->log->debug("Getting POD from CPAN") if($c->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
105 0 my $html = get( "http://search.cpan.org/perldoc?" . $module );
106 0 my $source;
107 0 ▶ if($html && $html =~ /.*<a href="(.*?)">Source<\/a>.*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html and $html =~ m[.*<a href="(.*?)">Source</a>.*]) { } 0 0 0
Condition0%0/3
$html and $html =~ m[.*<a href="(.*?)">Source</a>.*]
!ll&&!rl&&r
000
MC/DC0%0/2
$html and $html =~ m[.*<a href="(.*?)">Source</a>.*]
$html $html =~ m[.*<a href="(.*?)">Source</a>.*]
Truth table0%0/3
A and B
A= $htmlB= $html =~ m[.*<a href="(.*?)">Source</a>.*]
ABresult
0X0
100
111
108 0 $html =~ s/.*<a href="(.*?)">Source<\/a>.*/$1/s;
109 0 ▶ $c->log->debug("Get source from http://search.cpan.org" . $html) if($c->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
110 0 $source = get( "http://search.cpan.org" . $html );
111 } else {
112 0 $source = "=head1 ERROR\n\nThis module could not be found.";
113 }
114 0 $view->_toc( _get_toc( $source ) );
115 0 ▶ $pom = $parser->parse_text($source)
Condition0%0/2
$parser->parse_text($source) || die($parser->error, "\n")
l!l
00
MC/DC0%0/1
$parser->parse_text($source) || die($parser->error, "\n")
$parser->parse_text($source)
Truth table0%0/2
A
A= $parser->parse_text($source)
Aresult
11
00
116 || die $parser->error(), "\n";
117 }
118 0 Pod::POM->default_view("Catalyst::Controller::POD::POM::View");
119 0 $c->res->body( "$pom" );
120 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 0 ▶sub _get_toc {
Subroutine _get_toc: not called CC=1 SCAR=6.9
123 0 my $source = shift;
124 0 my $toc;
125 0 my $parser = Pod::POM->new( warn => 0 );
126 0 my $view = "Pod::POM::View::TOC";
127 0 Pod::POM->default_view($view);
128 0 my $pom = $parser->parse($source);
129 0 $toc = $view->print($pom);
130 0 return encode_json( _toc_to_json( [], split( /\n/, $toc ) ) );
131 }
132  
133 0 ▶sub _toc_to_json {
Subroutine _toc_to_json: not called CC=10 SCAR=47.0
134 0 my $tree = shift;
135 0 my @sections = @_;
136 0 my @uniq = uniq( map { ( split(/\t/) )[0] } @sections );
137 0 foreach my $root (@uniq) {
138 0 ▶ next unless ($root);
Branch: 1 branch
BranchTrueFalseTotal
unless $root 0 0 0
139 0 push( @{$tree}, { text => $root } );
140 0 my ( @children, $start );
141 0 for (@sections) {
142 0 ▶ if ( $_ =~ /^\Q$root\E$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_ =~ /^\Q$root\E$/) { } 0 0 0
elsif ($start and $_ =~ /^\t(.*)$/) { } 0 0 0
elsif ($start and $_ =~ /^[^\t]+/) { } 0 0 0
143 0 $start = 1;
144 ▶ } elsif ( $start && $_ =~ /^\t(.*)$/ ) {
Condition0%0/3
$start and $_ =~ /^\t(.*)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$start and $_ =~ /^\t(.*)$/
$start $_ =~ /^\t(.*)$/
Truth table0%0/3
A and B
A= $startB= $_ =~ /^\t(.*)$/
ABresult
0X0
100
111
145 0 push( @children, $1 );
146 ▶ } elsif ( $start && $_ =~ /^[^\t]+/ ) {
Condition0%0/3
$start and $_ =~ /^[^\t]+/
!ll&&!rl&&r
000
MC/DC0%0/2
$start and $_ =~ /^[^\t]+/
$start $_ =~ /^[^\t]+/
Truth table0%0/3
A and B
A= $startB= $_ =~ /^[^\t]+/
ABresult
0X0
100
111
147 0 last;
148 }
149 }
150 0 ▶ unless (@children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@children) 0 0 0
151 0 $tree->[-1]->{leaf} = \1;
152 0 next;
153 }
154 0 $tree->[-1]->{children} = [];
155 $tree->[-1]->{children} =
156 0 _toc_to_json( $tree->[-1]->{children}, @children );
157 }
158 0 return $tree;
159 }
160  
161 0 ▶sub modules : Local {
Subroutine modules: not called CC=8 SCAR=42.8
Pod undocumented
162 0 my ( $self, $c, $find ) = @_;
163 0 ▶ my $search = Pod::Simple::Search->new->inc( $self->{inc} || 0 );
Condition0%0/2
$self->{'inc'} || 0
l!l
00
MC/DC0%0/1
$self->{'inc'} || 0
$self->{'inc'}
Truth table0%0/2
A
A= $self->{'inc'}
Aresult
11
00
164 0 push( @{ $self->{dirs} }, $c->path_to('lib')->stringify )
165 0 ▶ if ( $self->{self} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"self"} 0 0 0
166 0 my $name2path = {};
167  
168 0 for ( @{ $self->{namespaces} } ) {
169 my $found =
170 Pod::Simple::Search->new->inc( $self->{inc} || 0 )
171 0 ▶ ->limit_glob($_)->survey( @{ $self->{dirs} } );
Condition0%0/2
$self->{'inc'} || 0
l!l
00
MC/DC0%0/1
$self->{'inc'} || 0
$self->{'inc'}
Truth table0%0/2
A
A= $self->{'inc'}
Aresult
11
00
172 0 %{$name2path} = (
173 0 %{$name2path}, %{$found}
174 );
175 }
176
177 0 my @modules;
178 0 ▶ while ( my ( $k, $v ) = each %$name2path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$name2path) 0 0 0
179 0 ▶ next if($find && $k !~ /\Q$find\E/ig);
Branch: 1 branch
BranchTrueFalseTotal
if $find and not $k =~ /\Q$find\E/gi 0 0 0
Condition0%0/3
$find and not $k =~ /\Q$find\E/gi
!ll&&!rl&&r
000
MC/DC0%0/2
$find and not $k =~ /\Q$find\E/gi
$find not $k =~ /\Q$find\E/gi
Truth table0%0/3
A and not(B)
A= $findB= not $k =~ /\Q$find\E/gi
ABresult
0X0
100
111
180 0 push( @modules, $k );
181 }
182 0 @modules = sort @modules;
183 0 my $json = _build_module_tree( [], "", @modules );
184 0 $c->res->body( encode_json($json) );
185 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 0 ▶sub _build_module_tree : Private {
Subroutine _build_module_tree: not called CC=6 SCAR=37.4
188 0 my $tree = shift;
189 0 my $stack = shift;
190 0 my @modules = @_;
191 0 my @uniq = uniq( map { ( split(/::/) )[0] } @modules );
192 0 foreach my $root (@uniq) {
193 0 ▶ my $name = $stack ? $stack . "::" . $root : $root;
Branch: 1 branch
BranchTrueFalseTotal
$stack ? : 0 0 0
194 0 push( @{$tree}, { text => $root, name => $name } );
195 0 my @children;
196 0 for (@modules) {
197 0 ▶ if ( $_ =~ /^$root\:\:(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^$root\:\:(.*)$/) 0 0 0
198 0 push( @children, $1 );
199 }
200 }
201 0 ▶ unless (@children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@children) 0 0 0
202 0 $tree->[-1]->{leaf} = \1;
203 0 next;
204 }
205 0 $tree->[-1]->{children} = [];
206 $tree->[-1]->{children} =
207 0 _build_module_tree( $tree->[-1]->{children}, $name, @children );
208 }
209 0 return $tree;
210 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 2 ▶sub _root {
Subroutine _root: called CC=1 SCAR=0.0
213 2 my ( $self, $c ) = @_;
214 2 my $index = $c->uri_for( __PACKAGE__->config->{path} );
215  
216 #$index =~ s/\/index//g;
217 2 return $index;
218 }
219  
220 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
221 2 my $class = shift;
222 2 my $self = $class->next::method(@_);
223 2 my $file = Path::Class::File->new( 'share', 'docs.js' );
224 2 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 2 2
225 0 $file = Path::Class::File->new(
226 dist_file( 'Catalyst-Controller-POD', 'docs.js' ) );
227 } unless(-e $file);
228 2 $self->_dist_dir( $file->dir );
229 2 return $self;
230 }
231  
232  
233 2 ▶sub index : Path : Args(0) {
Subroutine index: called CC=1 SCAR=0.0
Pod undocumented
234 2 my ( $self, $c ) = @_;
235 2 $c->res->content_type('text/html; charset=utf-8');
236 2 $c->response->body(
237 Catalyst::Controller::POD::Template->get(
238 $self->_root($c) . "/static"
239 )
240 );
241 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 0 ▶sub get_home_tab_content : Path("home_tab_content") {
Subroutine get_home_tab_content: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my ( $self, $c ) = @_;
245 0 $c->response->body($self->home_tab_content);
246 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
247  
248 2 ▶sub static : Path("static") {
Subroutine static: called CC=4 SCAR=23.4
Pod undocumented
249 2 my ( $self, $c, @file ) = @_;
250 2 my $file = File::Spec->catfile($self->_dist_dir, @file);
251 2 ▶ if ( $file[-1] eq "docs.js" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file[-1] eq 'docs.js') { } 0 2 2
252 0 my $data;
253 0 eval { $data = read_file( $file ) };
254 0 _replace_template_vars(\$data, "root", $self->_root($c));
255 0 _replace_template_vars(\$data, "initial_module", $self->initial_module);
256 0 ▶ _replace_template_vars(\$data, "show_home_tab", $self->show_home_tab ? "true" : "false");
Branch: 1 branch
BranchTrueFalseTotal
$self->show_home_tab ? : 0 0 0
257 0 ▶ _replace_template_vars(\$data, "expand_module_tree_on_load", $self->expanded_module_tree ? "true" : "false");
Branch: 1 branch
BranchTrueFalseTotal
$self->expanded_module_tree ? : 0 0 0
258 0 $c->res->content_type('application/json');
259 0 $c->response->body($data);
260 } else {
261 2 $c->serve_static_file($file);
262 }
263 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
264  
265 # A poor man's template module.
266 0 ▶sub _replace_template_vars {
Subroutine _replace_template_vars: not called CC=1 SCAR=6.9
267 0 my ($data_ref, $var_name, $var_val) = @_;
268 0 $$data_ref =~ s/\[% $var_name %\]/$var_val/g;
269 }
270  
271 3 1;
272  
273  
274  
275 =pod
276  
277 =head1 NAME
278  
279 Catalyst::Controller::POD - Serves PODs right from your Catalyst application
280  
281 =head1 VERSION
282  
283 version 1.0.0
284  
285 =head1 SYNOPSIS
286  
287 Create a new controller and paste this code:
288  
289 package MyApp::Controller::YourNewController; # <-- Change this to your controller
290
291 use strict;
292 use warnings;
293 use base 'Catalyst::Controller::POD';
294 __PACKAGE__->config(
295 inc => 1,
296 namespaces => [qw(Catalyst::Manual*)],
297 self => 1,
298 dirs => [qw()]
299 );
300 1;
301  
302 =head1 DESCRIPTION
303  
304 This is a catalyst controller which serves PODs. It allows you to browse through your local
305 repository of modules. On the front page of this controller is a search box
306 which uses CPAN's xml interface to retrieve the results. If you click on one of them
307 the POD is displayed in this application.
308  
309 Cross links in PODs are resolved and pop up as a new tab. If the module you clicked on is
310 not installed this controller fetches the source code from CPAN and creates the pod locally.
311 There is also a TOC which is always visible and scrolls the current POD to the selected section.
312  
313 It is written using a JavaScript framework called ExtJS (L<http://www.extjs.com>) which
314 generate beautiful and intuitive interfaces.
315  
316 Have a look at L<http://cpan.org/authors/id/P/PE/PERLER/pod-images/pod-encyclopedia-01.png>.
317  
318 B<< L<Catalyst::Plugin::Static::Simple> is required and has to be loaded. >>
319  
320 =head1 CONFIGURATION
321  
322 =over
323  
324 =item dirs (Arrayref)
325  
326 Search for modules in these directories.
327  
328 Defaults to C<[]>.
329  
330 =item expanded_module_tree (Boolean)
331  
332 Expand the module browser tree on initial page load.
333  
334 Defaults to C<1>
335  
336 =item home_tab_content (String)
337  
338 HTML to be displayed in the Home tab.
339  
340 Defaults to the existing CPAN search box.
341  
342 =item inc (Boolean)
343  
344 Search for modules in @INC. Set it to 1 or 0.
345  
346 Defaults to C<0>.
347  
348 =item initial_module (String)
349  
350 If this option is specified, a tab displaying the perldoc for the given module
351 will be opened on load. Handy if you wish to disable the home tab and specify
352 a specific module's perldoc as the initial page a user sees.
353  
354 Defaults to C<"">
355  
356 =item namespaces (Arrayref)
357  
358 Filter by namespaces. See L<Pod::Simple::Search> C<limit_glob> for syntax.
359  
360 Defaults to C<["*"]>
361  
362 =item self (Boolean)
363  
364 Search for modules in C<< $c->path_to( 'lib' ) >>.
365  
366 Defaults to C<1>.
367  
368 =item show_home_tab (Boolean)
369  
370 Show or hide the home tab.
371  
372 Defaults to C<1>
373  
374 =head1 NOTICE
375  
376 This module works fine for most PODs but there are a few which do not get rendered properly.
377 Please report any bug you find. See L</BUGS>.
378  
379 Have a look at L<Pod::Browser> which is a catalyst application running this controller. You
380 can use it as a stand-alone POD server.
381  
382 =head1 TODO
383  
384 Write more tests!
385  
386 =head1 CONTRIBUTORS
387  
388 Tristan Pratt
389  
390 =cut
391  
392 =head1 AUTHOR
393  
394 Moritz Onken
395  
396 =head1 COPYRIGHT AND LICENSE
397  
398 This software is Copyright (c) 2011 by Moritz Onken.
399  
400 This is free software, licensed under:
401  
402 The (three-clause) BSD License
403  
404 =cut
405  
406  
407 __END__
↑ Summary POD/POM/View.pm »