/usr/share/perl5/Mojolicious/Plugin/PODRenderer.pm is in libmojolicious-perl 2.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | package Mojolicious::Plugin::PODRenderer;
use Mojo::Base 'Mojolicious::Plugin';
use IO::File;
use Mojo::Asset::File;
use Mojo::ByteStream 'b';
use Mojo::DOM;
use Mojo::Home;
use Mojo::Util 'url_escape';
use Pod::Simple::HTML;
use Pod::Simple::Search;
# Paths
our @PATHS = map { $_, "$_/pods" } @INC;
# Bundled files
my $H = Mojo::Home->new;
$H->parse($H->parse($H->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
our $MOJOBAR = $H->slurp_rel_file('mojobar.html.ep');
our $PERLDOC = $H->slurp_rel_file('perldoc.html.ep');
# "This is my first visit to the Galaxy of Terror and I'd like it to be a
# pleasant one."
sub register {
my ($self, $app, $conf) = @_;
# Config
$conf ||= {};
my $name = $conf->{name} || 'pod';
my $preprocess = $conf->{preprocess} || 'ep';
# Add "pod" handler
$app->renderer->add_handler(
$name => sub {
my ($r, $c, $output, $options) = @_;
# Preprocess with ep and then render
$$output = _pod_to_html($$output)
if $r->handlers->{$preprocess}->($r, $c, $output, $options);
}
);
# Add "pod_to_html" helper
$app->helper(pod_to_html => sub { shift; b(_pod_to_html(@_)) });
# Perldoc
$app->routes->any(
'/perldoc/*module' => {module => 'Mojolicious/Guides'} => sub {
my $self = shift;
# Find module
my $module = $self->param('module');
$module =~ s|/|\:\:|g;
my $path = Pod::Simple::Search->new->find($module, @PATHS);
# Redirect to CPAN
return $self->redirect_to("http://metacpan.org/module/$module")
unless $path && -r $path;
# Turn POD into HTML
my $file = IO::File->new("< $path");
my $html = _pod_to_html(join '', <$file>);
# Rewrite links
my $dom = Mojo::DOM->new("$html");
my $perldoc = $self->url_for('/perldoc/');
$dom->find('a[href]')->each(
sub {
my $attrs = shift->attrs;
$attrs->{href} =~ s|%3A%3A|/|gi
if $attrs->{href}
=~ s|^http\://search\.cpan\.org/perldoc\?|$perldoc|;
}
);
# Rewrite code sections for syntax highlighting
$dom->find('pre')->each(
sub {
my $attrs = shift->attrs;
my $class = $attrs->{class};
$attrs->{class} =
defined $class ? "$class prettyprint" : 'prettyprint';
}
);
# Rewrite headers
my $url = $self->req->url->clone;
$url =~ s|%2F|/|gi;
my $sections = [];
$dom->find('h1, h2, h3')->each(
sub {
my $tag = shift;
my $text = $tag->all_text;
my $anchor = $text;
$anchor =~ s/\s+/_/g;
$anchor = url_escape $anchor, 'A-Za-z0-9_';
$anchor =~ s/\%//g;
push @$sections, [] if $tag->type eq 'h1' || !@$sections;
push @{$sections->[-1]}, $text, $url->fragment($anchor)->to_abs;
$tag->replace_content(
$self->link_to(
$text => $url->fragment('toc')->to_abs,
class => 'mojoscroll',
id => $anchor
)
);
}
);
# Try to find a title
my $title = 'Perldoc';
$dom->find('h1 + p')->first(sub { $title = shift->text });
# Combine everything to a proper response
$self->content_for(mojobar => $self->include(inline => $MOJOBAR));
$self->content_for(perldoc => "$dom");
$self->app->plugins->emit_hook(before_perldoc => $self);
$self->render(
inline => $PERLDOC,
title => $title,
sections => $sections
);
$self->res->headers->content_type('text/html;charset="UTF-8"');
}
) unless $conf->{no_perldoc};
}
sub _pod_to_html {
my $pod = shift;
return unless defined $pod;
# Block
$pod = $pod->() if ref $pod eq 'CODE';
# Parser
my $parser = Pod::Simple::HTML->new;
$parser->force_title('');
$parser->html_header_before_title('');
$parser->html_header_after_title('');
$parser->html_footer('');
# Parse
my $output;
$parser->output_string(\$output);
eval { $parser->parse_string_document("$pod") };
return $@ if $@;
# Filter
$output =~ s|<a name='___top' class='dummyTopAnchor'\s*?></a>\n||g;
$output =~ s|<a class='u'.*?name=".*?"\s*>(.*?)</a>|$1|sg;
return $output;
}
1;
__END__
=head1 NAME
Mojolicious::Plugin::PODRenderer - POD renderer plugin
=head1 SYNOPSIS
# Mojolicious
$self->plugin('PODRenderer');
$self->plugin(PODRenderer => {name => 'foo'});
$self->plugin(PODRenderer => {preprocess => 'epl'});
$self->render('some_template', handler => 'pod');
%= pod_to_html "=head1 TEST\n\nC<123>"
# Mojolicious::Lite
plugin 'PODRenderer';
plugin PODRenderer => {name => 'foo'};
plugin PODRenderer => {preprocess => 'epl'};
$self->render('some_template', handler => 'pod');
%= pod_to_html "=head1 TEST\n\nC<123>"
=head1 DESCRIPTION
L<Mojolicious::Plugin::PODRenderer> is a renderer for true Perl hackers,
rawr!
=head1 OPTIONS
L<Mojolicious::Plugin::PODRenderer> supports the following options.
=head2 C<name>
# Mojolicious::Lite
plugin PODRenderer => {name => 'foo'};
Handler name.
=head2 C<no_perldoc>
# Mojolicious::Lite
plugin PODRenderer => {no_perldoc => 1};
Disable perldoc browser.
Note that this option is EXPERIMENTAL and might change without warning!
=head2 C<preprocess>
# Mojolicious::Lite
plugin PODRenderer => {preprocess => 'epl'};
Handler name of preprocessor.
=head1 HELPERS
L<Mojolicious::Plugin::PODRenderer> implements the following helpers.
=head2 C<pod_to_html>
%= pod_to_html '=head2 lalala'
<%= pod_to_html begin %>=head2 lalala<% end %>
Render POD to HTML.
=head1 METHODS
L<Mojolicious::Plugin::PODRenderer> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<register>
$plugin->register;
Register renderer in L<Mojolicious> application.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|