| 1 | package CallBackery::Plugin::Doc; | ||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # based on Mojolicious::Plugin::PodRenderer | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use File::Basename 'dirname'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Mojo::Asset::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Mojo::ByteStream 'b'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use Mojo::DOM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use Mojo::Util 'url_escape'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use Pod::Simple::HTML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use Pod::Simple::Search; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Paths | ||||||||||||||||||||||||||||
| 19 | 12 | 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 | 13 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=7 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 13 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||
| 25 | # Config | ||||||||||||||||||||||||||||
| 26 | 13 | ▶ | $conf ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf ||= {}
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
| |||||||||||||||||||||||||||||
| 27 | 13 | ▶ | my $name = $conf->{name} || 'pod'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'name'} || 'pod'
MC/DC0%0/1
$conf->{'name'} || 'pod'
$conf->{'name'}
Truth table50%1/2
A
A= $conf->{'name'}
| |||||||||||||||||||||||||||||
| 28 | 13 | ▶ | my $preprocess = $conf->{preprocess} || 'ep'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'preprocess'} || 'ep'
MC/DC0%0/1
$conf->{'preprocess'} || 'ep'
$conf->{'preprocess'}
Truth table50%1/2
A
A= $conf->{'preprocess'}
| |||||||||||||||||||||||||||||
| 29 | 13 | ▶ | my $index = $conf->{index} || die 'index attribute is required'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'index'} || die('index attribute is required')
MC/DC0%0/1
$conf->{'index'} || die('index attribute is required')
$conf->{'index'}
Truth table50%1/2
A
A= $conf->{'index'}
| |||||||||||||||||||||||||||||
| 30 | 13 | ▶ | my $root = $conf->{root} || die 'root attribute is required'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'root'} || die('root attribute is required')
MC/DC0%0/1
$conf->{'root'} || die('root attribute is required')
$conf->{'root'}
Truth table50%1/2
A
A= $conf->{'root'}
| |||||||||||||||||||||||||||||
| 31 | 13 | ▶ | my $template = $conf->{template} || die 'template attribute is required'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'template'} || die('template attribute is required')
MC/DC0%0/1
$conf->{'template'} || die('template attribute is required')
$conf->{'template'}
Truth table50%1/2
A
A= $conf->{'template'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 0 | $$output = _pod_to_html($$output) | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 13 | ); | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$path and -r $path
MC/DC0%0/2
$path and -r $path
$path
-r $path
Truth table33%1/3
A and B
A= $pathB= -r $path
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$e->tag eq "h1" or not @$sections
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
| |||||||||||||||||||||||||||||
| 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 | 13 | ); | |||||||||||||||||||||||||||
| 135 | 13 | 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
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Block | ||||||||||||||||||||||||||||
| 143 | 6 | ▶ | $pod = $pod->() if ref $pod eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 12 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||