Skip to content

Commit

Permalink
fixes #92
Browse files Browse the repository at this point in the history
  • Loading branch information
monken committed Jun 8, 2011
1 parent 0190716 commit f1ea88b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
16 changes: 9 additions & 7 deletions lib/MetaCPAN/Plack/Module.pm
Expand Up @@ -7,13 +7,15 @@ sub type { 'file' }

sub query {
shift;
return { query => { match_all => {} },
filter => {
and => [ { term => { documentation => shift } },
{ term => { status => 'latest' } } ]
},
size => 1,
sort => { date => { reverse => \1 } } };
return { size => 1,
query => { filtered => { query => { match_all => {} },
filter => {
and => [
{ term => { 'documentation' => shift } },
{ term => { 'file.indexed' => \1, } },
{ term => { status => 'latest', } } ]
} } },
sort => [ { 'date' => { order => "desc" } }, { 'mime' => { order => "desc" } } ] };
}


Expand Down
36 changes: 20 additions & 16 deletions lib/MetaCPAN/Plack/Pod.pm
Expand Up @@ -13,9 +13,9 @@ use Try::Tiny;

sub handle {
my ( $self, $req ) = @_;
my $source;
my $path;
my $env = $req->env;
if ( $req->path =~ m/^\/pod\/([^\/]*?)\/?$/ ) {
my $env = $req->env;
$env->{REQUEST_URI} = "/module/$1";
$env->{PATH_INFO} = "/$1";
$env->{SCRIPT_NAME} = "/module";
Expand All @@ -28,29 +28,23 @@ sub handle {
my $file = $hit->{path};
my $release = $hit->{release};
my $author = $hit->{author};
$env->{REQUEST_URI} = $env->{PATH_INFO} =
"/source/$author/$release/$file";
delete $env->{CONTENT_LENGTH};
delete $env->{'psgi.input'};
$source = MetaCPAN::Plack::Source->new(
{ cpan => $self->cpan } )->to_app->($env)->[2];
$path = "/source/$author/$release/$file";
} else {
my $env = $req->env;
my $format = $env->{REQUEST_URI} =~ s/^\/pod\//\/source\//;
$env->{PATH_INFO} = $env->{REQUEST_URI};

$source =
MetaCPAN::Plack::Source->new( { cpan => $self->cpan } )
->to_app->($env)->[2];
($path = $env->{REQUEST_URI}) =~ s/^\/pod\//\/source\//;
}

# prefer .pod over .pm file
(my $pod = $path) =~ s/\.pm$/.pod/i;
my $source = $self->request_source($env, $pod);
$source ||= $self->request_source($env, $path);
if( ref $source eq 'ARRAY') {
return $req->new_response( 404, undef, $source )->finalize;
}
my $content = "";
while ( my $line = $source->getline ) {
$content .= $line;
}

my ($body, $content_type);
my $accept = $req->preferred_content_type || 'text/html';
if($accept eq 'text/plain') {
Expand All @@ -71,6 +65,16 @@ sub handle {
return $res->finalize;
}

sub request_source {
my ($self, $env, $path) = @_;
$env->{REQUEST_URI} = $env->{PATH_INFO} = $path;
delete $env->{CONTENT_LENGTH};
delete $env->{'psgi.input'};
my $res = MetaCPAN::Plack::Source->new(
{ cpan => $self->cpan } )->to_app->($env);
return $res->[2] if($res->[0] == 200);
}

sub build_pod_markdown {
my $self = shift;
my $parser = Pod::Markdown->new;
Expand Down

0 comments on commit f1ea88b

Please sign in to comment.