Skip to content

Commit

Permalink
improved documentation browser to be a little more RESTful
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 21, 2013
1 parent 3aa9dc5 commit 34b6434
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.41 2013-09-22
- Improved documentation browser to be a little more RESTful.
- Fixed flatten to work with older versions of Perl. (jamadam)

4.40 2013-09-21
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -880,6 +880,9 @@ that all stash values with a C<mojo.*> prefix are reserved for internal use.
Generate a portable L<Mojo::URL> object with base for a route, path or URL.
# "http://127.0.0.1:3000/perldoc" if application has been started with Morbo
$c->url_for('/perldoc')->to_abs;
# "/perldoc?foo=bar" if application is deployed under "/"
$c->url_for('/perldoc')->query(foo => 'bar');
Expand Down
37 changes: 20 additions & 17 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -31,27 +31,17 @@ sub register {

# Perldoc browser
return if $conf->{no_perldoc};
my $defaults = {module => 'Mojolicious/Guides', format => 'html'};
my $constraints = [module => qr/[^.]+/, format => [qw(html txt)]];
return $app->routes->any(
'/perldoc/*module' => {module => 'Mojolicious/Guides'} => \&_perldoc);
'/perldoc/:module' => $defaults => $constraints => \&_perldoc);
}

sub _perldoc {
my $self = shift;

# Find module or redirect to CPAN
my $module = $self->param('module');
$module =~ s!/!::!g;
my $path = Pod::Simple::Search->new->find($module, @PATHS);
return $self->redirect_to("http://metacpan.org/module/$module")
unless $path && -r $path;

# Source
my $source = slurp $path;
return $self->render(data => $source, format => 'txt')
if $self->param('source');
sub _html {
my ($self, $src) = @_;

# Rewrite links
my $dom = Mojo::DOM->new(_pod_to_html($source));
my $dom = Mojo::DOM->new(_pod_to_html($src));
my $perldoc = $self->url_for('/perldoc/');
for my $e ($dom->find('a[href]')->each) {
my $attrs = $e->attr;
Expand Down Expand Up @@ -96,7 +86,20 @@ sub _perldoc {
$self->content_for(perldoc => "$dom");
my $template = $self->app->renderer->_bundled('perldoc');
$self->render(inline => $template, title => $title, parts => \@parts);
$self->res->headers->content_type('text/html;charset="UTF-8"');
}

sub _perldoc {
my $self = shift;

# Find module or redirect to CPAN
my $module = $self->param('module');
$module =~ s!/!::!g;
my $path = Pod::Simple::Search->new->find($module, @PATHS);
return $self->redirect_to("http://metacpan.org/module/$module")
unless $path && -r $path;

my $src = slurp $path;
$self->respond_to(txt => {data => $src}, any => sub { _html($self, $src) });
}

sub _pod_to_html {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/templates/perldoc.html.ep
Expand Up @@ -71,7 +71,7 @@
% $path .= "/$part";
%= link_to $part => url_for("/perldoc$path")
% }
(<%= link_to 'source' => url_for->query({source => 1}) %>)
(<%= link_to 'source' => url_for("/perldoc$path.txt") %>)
</div>
<h1><a id="toc">TABLE OF CONTENTS</a></h1>
<ul>
Expand Down
6 changes: 5 additions & 1 deletion t/mojolicious/pod_renderer_lite_app.t
Expand Up @@ -63,7 +63,11 @@ $t->get_ok('/perldoc/Mojolicious')->status_is(200)
->text_like('p', qr/Mojolicious/)->content_like(qr/Sebastian Riedel/);

# Perldoc browser (Mojolicious source)
$t->get_ok('/perldoc/Mojolicious?source=1')->status_is(200)
$t->get_ok('/perldoc/Mojolicious.txt')->status_is(200)
->content_type_is('text/plain;charset=UTF-8')->content_like(qr/\$VERSION/);

# Perldoc browser (negotiated Mojolicious source)
$t->get_ok('/perldoc/Mojolicious' => {Accept => 'text/plain'})->status_is(200)
->content_type_is('text/plain;charset=UTF-8')->content_like(qr/\$VERSION/);

done_testing();
Expand Down

0 comments on commit 34b6434

Please sign in to comment.