lib/CallBackery/Plugin/Doc.pm

filter: Statementstmt 93.4% 100 / 107 Branchbran 55.5% 10 / 18 Conditioncond 50.0% 9 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 88.8% 16 / 18 Podpod 100% 1 / 1 totaltotal 79.6% 137 / 172 CC 19 SCAR 15.3
CC
19
Cov
83%
__ANON__
8.2
register
7.8
__ANON__
6.0
CRAP 4.6
« blib/lib/CallBackery/GuiPlugin/AbstractCardlist.pm ↑ Summary lib/CallBackery.pm »
1 package CallBackery::Plugin::Doc;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # based on Mojolicious::Plugin::PodRenderer
6  
7 11 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Mojo::Util 'url_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Pod::Simple::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Pod::Simple::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Paths
19 11 our @PATHS = map { ($_ , "$_/pods") } @INC;
20  
21 # "This is my first visit to the Galaxy of Terror and I'd like it to be a
22 # pleasant one."
23 12 ▶sub register {
Subroutine register: called CC=7 SCAR=20.5
Pod documented
24 12 my ($self, $app, $conf) = @_;
25 # Config
26 12 ▶ $conf ||= {};
Condition50%1/2
$conf ||= {}
l!l
120
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
27 12 ▶ my $name = $conf->{name} || 'pod';
Condition50%1/2
$conf->{'name'} || 'pod'
l!l
012
MC/DC0%0/1
$conf->{'name'} || 'pod'
$conf->{'name'}
Truth table50%1/2
A
A= $conf->{'name'}
Aresult
11
00
28 12 ▶ my $preprocess = $conf->{preprocess} || 'ep';
Condition50%1/2
$conf->{'preprocess'} || 'ep'
l!l
012
MC/DC0%0/1
$conf->{'preprocess'} || 'ep'
$conf->{'preprocess'}
Truth table50%1/2
A
A= $conf->{'preprocess'}
Aresult
11
00
29 12 ▶ my $index = $conf->{index} || die 'index attribute is required';
Condition50%1/2
$conf->{'index'} || die('index attribute is required')
l!l
120
MC/DC0%0/1
$conf->{'index'} || die('index attribute is required')
$conf->{'index'}
Truth table50%1/2
A
A= $conf->{'index'}
Aresult
11
00
30 12 ▶ my $root = $conf->{root} || die 'root attribute is required';
Condition50%1/2
$conf->{'root'} || die('root attribute is required')
l!l
120
MC/DC0%0/1
$conf->{'root'} || die('root attribute is required')
$conf->{'root'}
Truth table50%1/2
A
A= $conf->{'root'}
Aresult
11
00
31 12 ▶ my $template = $conf->{template} || die 'template attribute is required';
Condition50%1/2
$conf->{'template'} || die('template attribute is required')
l!l
120
MC/DC0%0/1
$conf->{'template'} || die('template attribute is required')
$conf->{'template'}
Truth table50%1/2
A
A= $conf->{'template'}
Aresult
11
00
32  
33 # Add "pod" handler
34 $app->renderer->add_handler(
35 0 ▶ $name => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
36 0 my ($r, $c, $output, $options) = @_;
37  
38 # Preprocess with ep and then render
39 0 ▶ return unless $r->handlers->{$preprocess}->($r, $c, $output, $options);
Branch: 1 branch
BranchTrueFalseTotal
unless $r->handlers->{$preprocess}->($r, $c, $output, $options) 0 0 0
40 0 $$output = _pod_to_html($$output)
41 }
42 12 );
43  
44 # Add "pod_to_html" helper
45 0 ▶ $app->helper(pod_to_html => sub { shift; b(_pod_to_html(@_)) });
Subroutine __ANON__: not called CC=1 SCAR=2.6
46  
47 # Perldoc
48 $app->routes->any(
49 10 ▶ $root.'/*module' => { module => $index } => sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
50 10 my $self = shift;
51  
52 # Find module
53 10 my $module = $self->param('module');
54 10 my $html;
55 10 my $cpan = 'http://search.cpan.org/perldoc';
56 10 $module =~ s/\//\:\:/g;
57  
58 # Only ever look up plain module names. Pod::Simple::Search->find splits
59 # the name on '::' and rejoins the parts with File::Spec->catfile, so a
60 # name containing '..' would walk out of @INC and render the POD of any
61 # readable file. Answer 404 without echoing the rejected name back.
62 10 ▶ return $self->reply->not_found
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\A\w+(?:::\w+)*\z/u 4 6 10
63 unless $module =~ /\A\w+(?:::\w+)*\z/;
64  
65 6 my $path;
66 6 $path = Pod::Simple::Search->new->find($module, @PATHS);
67 # Redirect to CPAN
68 6 ▶ return $self->redirect_to("$cpan?$module")
Branch: 1 branch
BranchTrueFalseTotal
unless $path and -r $path 0 6 6
Condition33%1/3
$path and -r $path
!ll&&!rl&&r
006
MC/DC0%0/2
$path and -r $path
$path -r $path
Truth table33%1/3
A and B
A= $pathB= -r $path
ABresult
0X0
100
111
69 unless $path && -r $path;
70  
71 # Turn POD into HTML. Three argument open, so that a path can never be
72 # read as a shell pipe and leading or trailing whitespace is not
73 # stripped. The open is also checked, as -r above only proves the file
74 # was readable a moment ago.
75 6 ▶ open my $file, '<', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<", $path or return $self->reply->not_found 6 0 6
76 or return $self->reply->not_found;
77 6 $html = _pod_to_html(join '', <$file>);
78 6 close $file;
79  
80 # Rewrite links
81 6 my $dom = Mojo::DOM->new("$html");
82 6 my $perldoc = $self->url_for($root.'/');
83 $dom->find('a[href]')->each(
84 68 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
85 68 my $attr = shift->attr;
86 68 ▶ if ($attr->{href} =~ /^$cpan/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"href"} =~ /^$cpan/u) 0 68 68
87 0 $attr->{href} =~ s/^$cpan\?/$perldoc/;
88 0 $attr->{href} =~ s/%3A%3A/\//gi;
89 }
90 }
91 6 );
92  
93 # Rewrite code sections for syntax highlighting
94 # $dom->find('pre')->each(
95 # sub {
96 # my $attrs = shift->attrs;
97 # my $class = $attrs->{class};
98 # $attrs->{class} =
99 # defined $class ? "$class prettyprint lang-perl" : 'prettyprint lang-perl';
100 # }
101 # );
102  
103 # Rewrite headers
104 6 my $url = $self->req->url->clone;
105 6 $url =~ s/%2F/\//gi;
106 6 my $toc = Mojo::URL->new->fragment('toc');
107  
108 6 my $sections = [];
109 6 for my $e ($dom->find('h1, h2, h3')->each) {
110 62 ▶ push @$sections, [] if $e->tag eq 'h1' || !@$sections;
Branch: 1 branch
BranchTrueFalseTotal
if $e->tag eq "h1" or not @$sections 48 14 62
Condition66%2/3
$e->tag eq "h1" or not @$sections
l!l&&r!l&&!r
48014
MC/DC50%1/2
$e->tag eq "h1" or not @$sections
$e->tag eq "h1" not @$sections
Truth table66%2/3
A or not(B)
A= $e->tag eq "h1"B= not @$sections
ABresult
1X1
011
000
111 62 my $anchor = $e->{id};
112 62 my $link = Mojo::URL->new->fragment($anchor);
113 62 push @{$sections->[-1]}, my $text = $e->all_text, $link;
114 62 my $permalink = $self->link_to('#' => $link, class => 'permalink');
115 62 $e->content($permalink . $self->link_to($text => $toc, id => $anchor));
116 }
117  
118  
119 # Try to find a title
120 6 my $title = 'Perldoc';
121 6 ▶ $dom->find('h1 + p')->first(sub { $title = shift->text });
Subroutine __ANON__: called CC=1 SCAR=0.0
122  
123 # Combine everything to a proper response
124 6 $self->content_for(perldoc => "$dom");
125 6 $self->content_for(index_link => $root.'/');
126 # $self->app->plugins->run_hook(before_perldoc => $self);
127 6 $self->render(
128 inline => $template,
129 title => $title,
130 sections => $sections
131 );
132 6 $self->res->headers->content_type('text/html;charset="UTF-8"');
133 }
134 12 );
135 12 return;
136 }
137  
138 6 ▶sub _pod_to_html {
Subroutine _pod_to_html: called CC=4 SCAR=14.0
139 6 my $pod = shift;
140 6 ▶ return unless defined $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod 0 6 6
141  
142 # Block
143 6 ▶ $pod = $pod->() if ref $pod eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pod eq "CODE" 0 6 6
144  
145 # Parser
146 6 my $parser = Pod::Simple::HTML->new;
147 6 $parser->force_title('');
148 6 $parser->html_header_before_title('');
149 6 $parser->html_header_after_title('');
150 6 $parser->html_footer('');
151 6 $parser->index(0);
152  
153 # Parse
154 6 my $output;
155 6 $parser->output_string(\$output);
156 6 eval { $parser->parse_string_document("$pod") };
157 6 ▶ return $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
158  
159 # Filter
160 6 $output =~ s/<a name='___top' class='dummyTopAnchor'\s*?><\/a>\n//g;
161 6 $output =~ s/<a class='u'.*?name=".*?"\s*>(.*?)<\/a>/$1/sg;
162  
163 6 return $output;
164 }
165  
166 11 1;
167  
168 __END__
« blib/lib/CallBackery/GuiPlugin/AbstractCardlist.pm ↑ Summary lib/CallBackery.pm »