| 1 | package Mojolicious::Plugin::PODRenderer; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::Asset::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mojo::ByteStream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Mojo::DOM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::File 'path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | #~ use Mojo::Util 'deprecated'; | ||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Pod::Simple::XHTML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Pod::Simple::Search; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 1 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #~ deprecated 'Mojolicious::Plugin::PODRenderer is DEPRECATED'; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | 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'}
| |||||||||||||||||||||||||||||
| 19 | $app->renderer->add_handler( | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $conf->{name} || 'pod' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 21 | 0 | my ($renderer, $c, $output, $options) = @_; | |||||||||||||||||||||||||||
| 22 | 0 | $renderer->handlers->{$preprocess}($renderer, $c, $output, $options); | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | $$output = _pod_to_html($$output) if defined $$output; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'name'} || 'pod'
MC/DC0%0/1
$conf->{'name'} || 'pod'
$conf->{'name'}
Truth table50%1/2
A
A= $conf->{'name'}
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $app->helper( | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | pod_to_html => sub { shift; Mojo::ByteStream->new(_pod_to_html(@_)) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Perldoc browser | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | return undef if $conf->{no_perldoc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | my $defaults = {module => 'Mojolicious/Guides'}; | |||||||||||||||||||||||||||
| 33 | 0 | return $app->routes->any( | |||||||||||||||||||||||||||
| 34 | '/perldoc/:module' => $defaults => [module => qr/[^.]+/] => \&_perldoc); | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 17 | ▶ | sub _indentation { | ||||||||||||||||||||||||||
|
Subroutine
_indentation: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 17 | (sort map {/^(\s+)/} @{shift()})[0]; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _html { | ||||||||||||||||||||||||||
|
Subroutine
_html: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($c, $src) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Rewrite links | ||||||||||||||||||||||||||||
| 45 | 0 | my $dom = Mojo::DOM->new(_pod_to_html($src)); | |||||||||||||||||||||||||||
| 46 | 0 | my $perldoc = $c->url_for('/perldoc/'); | |||||||||||||||||||||||||||
| 47 | $_->{href} =~ s!^https://metacpan\.org/pod/!$perldoc! | ||||||||||||||||||||||||||||
| 48 | and $_->{href} =~ s!::!/!gi | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | for $dom->find('a[href]')->map('attr')->each; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Rewrite code blocks for syntax highlighting and correct indentation | ||||||||||||||||||||||||||||
| 52 | 0 | for my $e ($dom->find('pre > code')->each) { | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | next if (my $str = $e->content) =~ /^\s*(?:\$|Usage:)\s+/m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | next unless $str =~ /[\$\@\%]\w|->\w|^use\s+\w/m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | my $attrs = $e->attr; | |||||||||||||||||||||||||||
| 56 | 0 | my $class = $attrs->{class}; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | $attrs->{class} = defined $class ? "$class prettyprint" : 'prettyprint'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Rewrite headers | ||||||||||||||||||||||||||||
| 61 | 0 | my $toc = Mojo::URL->new->fragment('toc'); | |||||||||||||||||||||||||||
| 62 | 0 | my @parts; | |||||||||||||||||||||||||||
| 63 | 0 | for my $e ($dom->find('h1, h2, h3, h4')->each) { | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | push @parts, [] if $e->tag eq 'h1' || !@parts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$e->tag eq "h1" or not @parts
MC/DC0%0/2
$e->tag eq "h1" or not @parts
$e->tag eq "h1"
not @parts
Truth table0%0/3
A or not(B)
A= $e->tag eq "h1"B= not @parts
| |||||||||||||||||||||||||||||
| 66 | 0 | my $link = Mojo::URL->new->fragment($e->{id}); | |||||||||||||||||||||||||||
| 67 | 0 | push @{$parts[-1]}, my $text = $e->all_text, $link; | |||||||||||||||||||||||||||
| 68 | 0 | my $permalink = $c->link_to('#' => $link, class => 'permalink'); | |||||||||||||||||||||||||||
| 69 | 0 | $e->content($permalink . $c->link_to($text => $toc)); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Try to find a title | ||||||||||||||||||||||||||||
| 73 | 0 | my $title = 'Perldoc'; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | $dom->find('h1 + p')->first(sub { $title = shift->text }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Combine everything to a proper response | ||||||||||||||||||||||||||||
| 77 | 0 | $c->content_for(perldoc => "$dom"); | |||||||||||||||||||||||||||
| 78 | 0 | $c->render('mojo/perldoc', title => $title, parts => \@parts); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub _perldoc { | ||||||||||||||||||||||||||
|
Subroutine
_perldoc: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 82 | 0 | my $c = shift; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Find module or redirect to CPAN | ||||||||||||||||||||||||||||
| 85 | 0 | my $module = join '::', split('/', $c->param('module')); | |||||||||||||||||||||||||||
| 86 | 0 | $c->stash(cpan => "https://metacpan.org/pod/$module"); | |||||||||||||||||||||||||||
| 87 | my $path | ||||||||||||||||||||||||||||
| 88 | 0 | = Pod::Simple::Search->new->find($module, map { $_, "$_/pods" } @INC); | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | return $c->redirect_to($c->stash('cpan')) unless $path && -r $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$path and -r $path
MC/DC0%0/2
$path and -r $path
$path
-r $path
Truth table0%0/3
A and B
A= $pathB= -r $path
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $src = path($path)->slurp; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | $c->respond_to(txt => {data => $src}, html => sub { _html($c, $src) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub _pod_to_html { | ||||||||||||||||||||||||||
|
Subroutine
_pod_to_html: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | return '' unless defined(my $pod = ref $_[0] eq 'CODE' ? shift->() : shift); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | my $parser = Pod::Simple::XHTML->new; | |||||||||||||||||||||||||||
| 99 | 1 | $parser->perldoc_url_prefix('https://metacpan.org/pod/'); | |||||||||||||||||||||||||||
| 100 | 1 | $parser->$_('') for qw(html_header html_footer); | |||||||||||||||||||||||||||
| 101 | 1 | $parser->strip_verbatim_indent(\&_indentation); | |||||||||||||||||||||||||||
| 102 | 1 | $parser->output_string(\(my $output)); | |||||||||||||||||||||||||||
| 103 | 1 | ▶ | return $@ unless eval { $parser->parse_string_document("$pod"); 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | return $output; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =encoding utf8 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 NAME | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Mojolicious::Plugin::PODRenderer - POD renderer plugin | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Mojolicious (with documentation browser under "/perldoc") | ||||||||||||||||||||||||||||
| 119 | my $route = $app->plugin('PODRenderer'); | ||||||||||||||||||||||||||||
| 120 | my $route = $app->plugin(PODRenderer => {name => 'foo'}); | ||||||||||||||||||||||||||||
| 121 | my $route = $app->plugin(PODRenderer => {preprocess => 'epl'}); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Mojolicious::Lite (with documentation browser under "/perldoc") | ||||||||||||||||||||||||||||
| 124 | my $route = plugin 'PODRenderer'; | ||||||||||||||||||||||||||||
| 125 | my $route = plugin PODRenderer => {name => 'foo'}; | ||||||||||||||||||||||||||||
| 126 | my $route = plugin PODRenderer => {preprocess => 'epl'}; | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Without documentation browser | ||||||||||||||||||||||||||||
| 129 | plugin PODRenderer => {no_perldoc => 1}; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # foo.html.ep | ||||||||||||||||||||||||||||
| 132 | %= pod_to_html "=head1 TEST\n\nC<123>" | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # foo.html.pod | ||||||||||||||||||||||||||||
| 135 | =head1 <%= uc 'test' %> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | L<Mojolicious::Plugin::PODRenderer> is a renderer for true Perl hackers, rawr! | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | The code of this plugin is a good example for learning to build new plugins, | ||||||||||||||||||||||||||||
| 142 | you're welcome to fork it. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available | ||||||||||||||||||||||||||||
| 145 | by default. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | L<Mojolicious::Plugin::PODRenderer> supports the following options. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 name | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 154 | plugin PODRenderer => {name => 'foo'}; | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Handler name, defaults to C<pod>. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 no_perldoc | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 161 | plugin PODRenderer => {no_perldoc => 1}; | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Disable L<Mojolicious::Guides> documentation browser that will otherwise be | ||||||||||||||||||||||||||||
| 164 | available under C</perldoc>. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 preprocess | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Mojolicious::Lite | ||||||||||||||||||||||||||||
| 169 | plugin PODRenderer => {preprocess => 'epl'}; | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Name of handler used to preprocess POD, defaults to C<ep>. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 HELPERS | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | L<Mojolicious::Plugin::PODRenderer> implements the following helpers. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 pod_to_html | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | %= pod_to_html '=head2 lalala' | ||||||||||||||||||||||||||||
| 180 | <%= pod_to_html begin %>=head2 lalala<% end %> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Render POD to HTML without preprocessing. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 METHODS | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | L<Mojolicious::Plugin::PODRenderer> inherits all methods from | ||||||||||||||||||||||||||||
| 187 | L<Mojolicious::Plugin> and implements the following new ones. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 register | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | my $route = $plugin->register(Mojolicious->new); | ||||||||||||||||||||||||||||
| 192 | my $route = $plugin->register(Mojolicious->new, {name => 'foo'}); | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Register renderer and helper in L<Mojolicious> application. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||