Skip to content

Commit

Permalink
let the renderer handle bundled templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 31, 2012
1 parent e904f08 commit 79d2731
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 40 deletions.
7 changes: 3 additions & 4 deletions lib/Mojo/Parameters.pm
Expand Up @@ -6,7 +6,6 @@ use overload
fallback => 1;

use Mojo::Util qw(decode encode url_escape url_unescape);
use Mojo::URL;

has charset => 'UTF-8';
has pair_separator => '&';
Expand Down Expand Up @@ -168,7 +167,7 @@ sub to_string {
my $charset = $self->charset;
if (defined(my $string = $self->{string})) {
$string = encode $charset, $string if $charset;
return url_escape $string, "^$Mojo::URL::UNRESERVED!\$&'()*+,;=%:@/?";
return url_escape $string, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?';
}

# Build pairs
Expand All @@ -180,11 +179,11 @@ sub to_string {

# Escape and replace whitespace with "+"
$name = encode $charset, $name if $charset;
$name = url_escape $name, "^$Mojo::URL::UNRESERVED!\$'()*,%:@/?";
$name = url_escape $name, '^A-Za-z0-9\-._~!$\'()*,%:@/?';
$name =~ s/\%20/\+/g;
if ($value) {
$value = encode $charset, $value if $charset;
$value = url_escape $value, "^$Mojo::URL::UNRESERVED!\$'()*,%:@/?";
$value = url_escape $value, '^A-Za-z0-9\-._~!$\'()*,%:@/?';
$value =~ s/\%20/\+/g;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Path.pm
Expand Up @@ -6,7 +6,6 @@ use overload
fallback => 1;

use Mojo::Util qw(encode url_escape url_unescape);
use Mojo::URL;

has [qw(leading_slash trailing_slash)];
has parts => sub { [] };
Expand Down Expand Up @@ -99,7 +98,7 @@ sub to_string {
my $self = shift;

# Escape
my $chars = "^$Mojo::URL::UNRESERVED$Mojo::URL::SUBDELIM:@";
my $chars = '^A-Za-z0-9\-._~!$&\'()*+,;=:@';
my @parts = map { url_escape(encode('UTF-8', $_), $chars) } @{$self->parts};

# Format
Expand Down
8 changes: 2 additions & 6 deletions lib/Mojo/URL.pm
Expand Up @@ -12,10 +12,6 @@ use Mojo::Util qw(punycode_decode punycode_encode url_escape url_unescape);
has [qw(fragment host port scheme userinfo)];
has base => sub { Mojo::URL->new };

# Characters (RFC 3986)
our $UNRESERVED = 'A-Za-z0-9\-._~';
our $SUBDELIM = '!$&\'()*+,;=';

# "Homer, it's easy to criticize.
# Fun, too."
sub new { shift->SUPER::new->parse(@_) }
Expand All @@ -39,7 +35,7 @@ sub authority {

# Format
if (my $userinfo = $self->userinfo) {
$authority .= url_escape($userinfo, "^$UNRESERVED$SUBDELIM\:") . '@';
$authority .= url_escape($userinfo, '^A-Za-z0-9\-._~!$&\'()*+,;=\:') . '@';
}
$authority .= lc($self->ihost || '');
if (my $port = $self->port) { $authority .= ":$port" }
Expand Down Expand Up @@ -225,7 +221,7 @@ sub to_string {

# Fragment
if (my $fragment = $self->fragment) {
$url .= '#' . url_escape $fragment, "^$UNRESERVED$SUBDELIM%:@/?";
$url .= '#' . url_escape $fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?';
}

return $url;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -214,7 +214,7 @@ sub _multipart {
if (ref $values eq 'HASH') {
$filename = delete $values->{filename} || $name;
$filename = encode $encoding, $filename if $encoding;
$filename = url_escape $filename, "^$Mojo::URL::UNRESERVED";
$filename = url_escape $filename, '^A-Za-z0-9\-._~';
push @parts, $part->asset(delete $values->{file});
$headers->from_hash($values);
}
Expand All @@ -230,7 +230,7 @@ sub _multipart {

# Content-Disposition
$name = encode $encoding, $name if $encoding;
$name = url_escape $name, "^$Mojo::URL::UNRESERVED";
$name = url_escape $name, '^A-Za-z0-9\-._~';
my $disposition = qq{form-data; name="$name"};
$disposition .= qq{; filename="$filename"} if $filename;
$headers->content_disposition($disposition);
Expand Down
30 changes: 12 additions & 18 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -5,7 +5,6 @@ use Carp ();
use Mojo::ByteStream;
use Mojo::Cookie::Response;
use Mojo::Exception;
use Mojo::Home;
use Mojo::Transaction::HTTP;
use Mojo::URL;
use Mojo::Util;
Expand All @@ -20,15 +19,6 @@ has match => sub {
};
has tx => sub { Mojo::Transaction::HTTP->new };

# Bundled templates
our $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');
my $EXCEPTION = $H->slurp_rel_file('exception.html.ep');
my $DEV_EXCEPTION = $H->slurp_rel_file('exception.development.html.ep');
my $NOT_FOUND = $H->slurp_rel_file('not_found.html.ep');
my $DEV_NOT_FOUND = $H->slurp_rel_file('not_found.development.html.ep');

# Reserved stash values
my %RESERVED = map { $_ => 1 } (
qw(action app cb controller data extends format handler json layout),
Expand Down Expand Up @@ -256,16 +246,18 @@ sub render_exception {
grep { !/^mojo\./ and defined $stash->{$_} } keys %$stash;

# Render with fallbacks
my $mode = $app->mode;
my $options = {
my $mode = $app->mode;
my $renderer = $app->renderer;
my $options = {
exception => $e,
snapshot => \%snapshot,
template => "exception.$mode",
format => $stash->{format} || $app->renderer->default_format,
format => $stash->{format} || $renderer->default_format,
handler => undef,
status => 500
};
my $inline = $mode eq 'development' ? $DEV_EXCEPTION : $EXCEPTION;
my $inline = $renderer->_bundled(
$mode eq 'development' ? 'exception.development' : 'exception');
return if $self->_fallbacks($options, 'exception', $inline);
$self->_fallbacks({%$options, format => 'html'}, 'exception', $inline);
}
Expand All @@ -282,12 +274,14 @@ sub render_not_found {
my $self = shift;

# Render with fallbacks
my $app = $self->app;
my $mode = $app->mode;
my $format = $self->stash->{format} || $app->renderer->default_format;
my $app = $self->app;
my $mode = $app->mode;
my $renderer = $app->renderer;
my $format = $self->stash->{format} || $renderer->default_format;
my $options
= {template => "not_found.$mode", format => $format, status => 404};
my $inline = $mode eq 'development' ? $DEV_NOT_FOUND : $NOT_FOUND;
my $inline = $renderer->_bundled(
$mode eq 'development' ? 'not_found.development' : 'not_found');
return if $self->_fallbacks($options, 'not_found', $inline);
$self->_fallbacks({%$options, format => 'html'}, 'not_found', $inline);
}
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -11,17 +11,15 @@ use Pod::Simple::Search;
# Paths
my @PATHS = map { $_, "$_/pods" } @INC;

# Bundled files
my $PERLDOC = $Mojolicious::Controller::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) = @_;

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

Expand All @@ -36,6 +34,7 @@ sub register {
$app->helper(pod_to_html => sub { shift; b(_pod_to_html(@_)) });

# Perldoc
my $template = $renderer->_bundled('perldoc');
return $app->routes->any(
'/perldoc/*module' => {module => 'Mojolicious/Guides'} => sub {
my $self = shift;
Expand Down Expand Up @@ -105,7 +104,7 @@ sub register {

# Combine everything to a proper response
$self->content_for(perldoc => "$dom");
$self->render(inline => $PERLDOC, title => $title, parts => \@parts);
$self->render(inline => $template, title => $title, parts => \@parts);
$self->res->headers->content_type('text/html;charset="UTF-8"');
}
) unless $conf->{no_perldoc};
Expand Down
8 changes: 8 additions & 0 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -16,6 +16,12 @@ has encoding => 'UTF-8';
has [qw(handlers helpers)] => sub { {} };
has paths => sub { [] };

# Bundled templates
my $HOME = Mojo::Home->new;
$HOME->parse(
$HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
my %TEMPLATES = map { $_ => $HOME->slurp_rel_file($_) } @{$HOME->list_files};

# "This is not how Xmas is supposed to be.
# In my day Xmas was about bringing people together,
# not blowing them apart."
Expand Down Expand Up @@ -176,6 +182,8 @@ sub template_path {
return catfile($self->paths->[0], split '/', $name);
}

sub _bundled { $TEMPLATES{"@{[pop]}.html.ep"} }

sub _detect_handler {
my ($self, $options) = @_;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/templates/exception.development.html.ep
Expand Up @@ -98,7 +98,7 @@
% end
</head>
<body onload="prettyPrint()">
%= include inline => $Mojolicious::Controller::MOJOBAR
%= include inline => app->renderer->_bundled('mojobar')
<div id="wrapperlicious">
<div id="nothing" class="box spaced"></div>
% my $cv = begin
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/templates/not_found.development.html.ep
Expand Up @@ -74,7 +74,7 @@
% end
</head>
<body onload="prettyPrint()">
%= include inline => $Mojolicious::Controller::MOJOBAR
%= include inline => app->renderer->_bundled('mojobar')
<div id="wrapperlicious">
<div id="routes">
<h1>Page not found... yet!</h1>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/templates/perldoc.html.ep
Expand Up @@ -61,7 +61,7 @@
% end
</head>
<body onload="prettyPrint()">
%= include inline => $Mojolicious::Controller::MOJOBAR
%= include inline => app->renderer->_bundled('mojobar')
% my $link = begin
%= link_to shift, shift, class => "mojoscroll"
% end
Expand Down

0 comments on commit 79d2731

Please sign in to comment.