<commit_before>package Mojolicious::Plugin::PodRenderer;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

use IO::File;
use Mojo::ByteStream 'b';
use Mojo::Command;
use Mojo::DOM;

# Core module since Perl 5.9.3, so it might not always be present
BEGIN {
    die <<'EOF' unless eval { require Pod::Simple::HTML; 1 } }
Module "Pod::Simple" not present in this version of Perl.
Please install it manually or upgrade Perl to at least version 5.10.
EOF

use Pod::Simple::Search;

# Perldoc template
our $PERLDOC = Mojo::Command->new->get_data('perldoc.html.ep', __PACKAGE__);

# 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';
    my $prefix     = $conf->{prefix}     || 'perldoc';

    # Add "pod" handler
    $app->renderer->add_handler(
        $name => sub {
            my ($r, $c, $output, $options) = @_;

            # Preprocess with ep and then render
            $$output = $self->_pod_to_html($$output)
              if $r->handler->{$preprocess}->($r, $c, $output, $options);
        }
    );

    # Add "pod_to_html" helper
    $app->helper(pod_to_html => sub { shift; b($self->_pod_to_html(@_)) });

    # Perldoc
    $app->routes->any(
        $prefix => sub {
            my $self = shift;

            # Module
            my $module = $self->req->url->query->params->[0]
              || 'Mojolicious::Lite';

            # Path
            my $path = Pod::Simple::Search->new->find($module);

            # Redirect to CPAN
            my $cpan = 'http://search.cpan.org/perldoc';
            return $self->redirect_to(
                "$cpan?" . $self->req->url->query->to_string)
              unless $path && -r $path;

            # POD
            my $file = IO::File->new;
            $file->open("< $path");
            my $dom =
              Mojo::DOM->new->parse($self->pod_to_html(join '', <$file>));
            $dom->find('a[href]')->each(
                sub {
                    my $attrs = shift->attrs;
                    if ($attrs->{href} =~ /^$cpan/) {
                        my $url = $self->url_for("/$prefix");
                        $attrs->{href} =~ s/^$cpan/$url/;
                    }
                }
            );
            $dom->find('pre')->each(
                sub {
                    my $attrs = shift->attrs;<commit_msg>changed default module<commit_after>package Mojolicious::Plugin::PodRenderer;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

use IO::File;
use Mojo::ByteStream 'b';
use Mojo::Command;
use Mojo::DOM;

# Core module since Perl 5.9.3, so it might not always be present
BEGIN {
    die <<'EOF' unless eval { require Pod::Simple::HTML; 1 } }
Module "Pod::Simple" not present in this version of Perl.
Please install it manually or upgrade Perl to at least version 5.10.
EOF

use Pod::Simple::Search;

# Perldoc template
our $PERLDOC = Mojo::Command->new->get_data('perldoc.html.ep', __PACKAGE__);

# 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';
    my $prefix     = $conf->{prefix}     || 'perldoc';

    # Add "pod" handler
    $app->renderer->add_handler(
        $name => sub {
            my ($r, $c, $output, $options) = @_;

            # Preprocess with ep and then render
            $$output = $self->_pod_to_html($$output)
              if $r->handler->{$preprocess}->($r, $c, $output, $options);
        }
    );

    # Add "pod_to_html" helper
    $app->helper(pod_to_html => sub { shift; b($self->_pod_to_html(@_)) });

    # Perldoc
    $app->routes->any(
        $prefix => sub {
            my $self = shift;

            # Module
            my $module = $self->req->url->query->params->[0]
              || 'Mojolicious::Guides';

            # Path
            my $path = Pod::Simple::Search->new->find($module);

            # Redirect to CPAN
            my $cpan = 'http://search.cpan.org/perldoc';
            return $self->redirect_to(
                "$cpan?" . $self->req->url->query->to_string)
              unless $path && -r $path;

            # POD
            my $file = IO::File->new;
            $file->open("< $path");
            my $dom =
              Mojo::DOM->new->parse($self->pod_to_html(join '', <$file>));
            $dom->find('a[href]')->each(
                sub {
                    my $attrs = shift->attrs;
                    if ($attrs->{href} =~ /^$cpan/) {
                        my $url = $self->url_for("/$prefix");
                        $attrs->{href} =~ s/^$cpan/$url/;
                    }
                }
            );
            $dom->find('pre')->each(
                sub {
                    my $attrs = shift->attrs;
{'fraction_non_alphanumeric': 0.16742709779805595, 'fraction_numerical': 0.0031739734179726245, 'mean_word_length': 1.7995558023320377, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3383014', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1573, 'n_words': 393}