lib/CallBackery/Plugin/Doc.pm

filter: Statementstmt 45.8% 50 / 109 Branchbran 0.0% 0 / 14 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 68.4% 13 / 19 Podpod 100% 1 / 1 totaltotal 40.9% 70 / 171 CC 17 SCAR 28.2
CC
17
Cov
40%
__ANON__
42.0
register
30.7
_pod_to_html
20.0
CRAP 16.7
« lib/CallBackery/Config.pm ↑ Summary blib/lib/Mojolicious/Command/generate/callbackery_app/lib/App/GuiPlugin/SongForm.pm »
1 package CallBackery::Plugin::Doc;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # based on Mojolicious::Plugin::PodRenderer
6  
7 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mojo::Util 'url_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Pod::Simple::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Pod::Simple::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Paths
20 1 our @PATHS = map { ($_ , "$_/pods") } @INC;
21  
22 # "This is my first visit to the Galaxy of Terror and I'd like it to be a
23 # pleasant one."
24 1 ▶sub register {
Subroutine register: called CC=7 SCAR=34.2
Pod documented
25 1 my ($self, $app, $conf) = @_;
26 # Config
27 1 ▶ $conf ||= {};
Condition50%1/2
$conf ||= {}
l!l
10
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
28 1 ▶ my $name = $conf->{name} || 'pod';
Condition50%1/2
$conf->{'name'} || 'pod'
l!l
01
MC/DC0%0/1
$conf->{'name'} || 'pod'
$conf->{'name'}
Truth table50%1/2
A
A= $conf->{'name'}
Aresult
11
00
29 1 ▶ my $preprocess = $conf->{preprocess} || 'ep';
Condition50%1/2
$conf->{'preprocess'} || 'ep'
l!l
01
MC/DC0%0/1
$conf->{'preprocess'} || 'ep'
$conf->{'preprocess'}
Truth table50%1/2
A
A= $conf->{'preprocess'}
Aresult
11
00
30 1 ▶ my $index = $conf->{index} || die 'index attribute is required';
Condition50%1/2
$conf->{'index'} || die('index attribute is required')
l!l
10
MC/DC0%0/1
$conf->{'index'} || die('index attribute is required')
$conf->{'index'}
Truth table50%1/2
A
A= $conf->{'index'}
Aresult
11
00
31 1 ▶ my $root = $conf->{root} || die 'root attribute is required';
Condition50%1/2
$conf->{'root'} || die('root attribute is required')
l!l
10
MC/DC0%0/1
$conf->{'root'} || die('root attribute is required')
$conf->{'root'}
Truth table50%1/2
A
A= $conf->{'root'}
Aresult
11
00
32 1 ▶ my $template = $conf->{template} || die 'template attribute is required';
Condition50%1/2
$conf->{'template'} || die('template attribute is required')
l!l
10
MC/DC0%0/1
$conf->{'template'} || die('template attribute is required')
$conf->{'template'}
Truth table50%1/2
A
A= $conf->{'template'}
Aresult
11
00
33  
34 # Add "pod" handler
35 $app->renderer->add_handler(
36 0 ▶ $name => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 0 my ($r, $c, $output, $options) = @_;
38  
39 # Preprocess with ep and then render
40 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
41 0 $$output = _pod_to_html($$output)
42 }
43 1 );
44  
45 # Add "pod_to_html" helper
46 0 ▶ $app->helper(pod_to_html => sub { shift; b(_pod_to_html(@_)) });
Subroutine __ANON__: not called CC=1 SCAR=2.6
47  
48 # Perldoc
49 $app->routes->any(
50 0 ▶ $root.'/*module' => { module => $index } => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
51 0 my $self = shift;
52  
53 # Find module
54 0 my $module = $self->param('module');
55 0 my $html;
56 0 my $cpan = 'http://search.cpan.org/perldoc';
57 0 $module =~ s/\//\:\:/g;
58 0 my $path;
59 0 $path = Pod::Simple::Search->new->find($module, @PATHS);
60 # Redirect to CPAN
61 0 ▶ return $self->redirect_to("$cpan?$module")
Branch: 1 branch
BranchTrueFalseTotal
unless $path and -r $path 0 0 0
Condition0%0/3
$path and -r $path
!ll&&!rl&&r
000
MC/DC0%0/2
$path and -r $path
$path -r $path
Truth table0%0/3
A and B
A= $pathB= -r $path
ABresult
0X0
100
111
62 unless $path && -r $path;
63  
64 # Turn POD into HTML
65 0 my $file = IO::File->new;
66 0 $file->open("< $path");
67 0 $html = _pod_to_html(join '', <$file>);
68  
69 # Rewrite links
70 0 my $dom = Mojo::DOM->new("$html");
71 0 my $perldoc = $self->url_for($root.'/');
72 $dom->find('a[href]')->each(
73 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
74 0 my $attr = shift->attr;
75 0 ▶ if ($attr->{href} =~ /^$cpan/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"href"} =~ /^$cpan/u) 0 0 0
76 0 $attr->{href} =~ s/^$cpan\?/$perldoc/;
77 0 $attr->{href} =~ s/%3A%3A/\//gi;
78 }
79 }
80 0 );
81  
82 # Rewrite code sections for syntax highlighting
83 # $dom->find('pre')->each(
84 # sub {
85 # my $attrs = shift->attrs;
86 # my $class = $attrs->{class};
87 # $attrs->{class} =
88 # defined $class ? "$class prettyprint lang-perl" : 'prettyprint lang-perl';
89 # }
90 # );
91  
92 # Rewrite headers
93 0 my $url = $self->req->url->clone;
94 0 $url =~ s/%2F/\//gi;
95 0 my $toc = Mojo::URL->new->fragment('toc');
96  
97 0 my $sections = [];
98 0 for my $e ($dom->find('h1, h2, h3')->each) {
99 0 ▶ push @$sections, [] if $e->tag eq 'h1' || !@$sections;
Branch: 1 branch
BranchTrueFalseTotal
if $e->tag eq "h1" or not @$sections 0 0 0
Condition0%0/3
$e->tag eq "h1" or not @$sections
l!l&&r!l&&!r
000
MC/DC0%0/2
$e->tag eq "h1" or not @$sections
$e->tag eq "h1" not @$sections
Truth table0%0/3
A or not(B)
A= $e->tag eq "h1"B= not @$sections
ABresult
1X1
011
000
100 0 my $anchor = $e->{id};
101 0 my $link = Mojo::URL->new->fragment($anchor);
102 0 push @{$sections->[-1]}, my $text = $e->all_text, $link;
103 0 my $permalink = $self->link_to('#' => $link, class => 'permalink');
104 0 $e->content($permalink . $self->link_to($text => $toc, id => $anchor));
105 }
106  
107  
108 # Try to find a title
109 0 my $title = 'Perldoc';
110 0 ▶ $dom->find('h1 + p')->first(sub { $title = shift->text });
Subroutine __ANON__: not called CC=1 SCAR=6.9
111  
112 # Combine everything to a proper response
113 0 $self->content_for(perldoc => "$dom");
114 0 $self->content_for(index_link => $root.'/');
115 # $self->app->plugins->run_hook(before_perldoc => $self);
116 0 $self->render(
117 inline => $template,
118 title => $title,
119 sections => $sections
120 );
121 0 $self->res->headers->content_type('text/html;charset="UTF-8"');
122 }
123 1 );
124 1 return;
125 }
126  
127 0 ▶sub _pod_to_html {
Subroutine _pod_to_html: not called CC=4 SCAR=30.0
128 0 my $pod = shift;
129 0 ▶ return unless defined $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod 0 0 0
130  
131 # Block
132 0 ▶ $pod = $pod->() if ref $pod eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pod eq "CODE" 0 0 0
133  
134 # Parser
135 0 my $parser = Pod::Simple::HTML->new;
136 0 $parser->force_title('');
137 0 $parser->html_header_before_title('');
138 0 $parser->html_header_after_title('');
139 0 $parser->html_footer('');
140 0 $parser->index(0);
141  
142 # Parse
143 0 my $output;
144 0 $parser->output_string(\$output);
145 0 eval { $parser->parse_string_document("$pod") };
146 0 ▶ return $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
147  
148 # Filter
149 0 $output =~ s/<a name='___top' class='dummyTopAnchor'\s*?><\/a>\n//g;
150 0 $output =~ s/<a class='u'.*?name=".*?"\s*>(.*?)<\/a>/$1/sg;
151  
152 0 return $output;
153 }
154  
155 1 1;
156  
157 __END__
« lib/CallBackery/Config.pm ↑ Summary blib/lib/Mojolicious/Command/generate/callbackery_app/lib/App/GuiPlugin/SongForm.pm »