Created
October 11, 2012 10:01
-
-
Save ktat/3871366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Router/Simple/Route.pm b/lib/Router/Simple/Route.pm | |
index 6c5ba4a..2e664f1 100644 | |
--- a/lib/Router/Simple/Route.pm | |
+++ b/lib/Router/Simple/Route.pm | |
@@ -33,7 +33,7 @@ sub new { | |
} | |
$row->{pattern} = $pattern; | |
- | |
+ my $pattern_re_capture = $pattern; | |
# compile pattern | |
my @capture; | |
$row->{pattern_re} = do { | |
@@ -64,6 +64,28 @@ sub new { | |
qr{^$pattern$}; | |
} | |
}; | |
+ $row->{pattern_re_capture} = do { | |
+ if (ref $pattern_re_capture) { | |
+ $row->{_regexp_capture} = 1; | |
+ $pattern_re_capture; | |
+ } else { | |
+ $pattern_re_capture =~ s! | |
+ \{((?:\{[0-9,]+\}|[^{}]+)+)\} | # /blog/{year:\d{4}} | |
+ :([A-Za-z0-9_]+) | # /blog/:year | |
+ (\*) | # /blog/*/* | |
+ ([^{:*]+) # normal string | |
+ ! | |
+ if ($1 or $2) { | |
+ '([^/]+)'; | |
+ } elsif ($3) { | |
+ "(.+)"; | |
+ } else { | |
+ quotemeta($4); | |
+ } | |
+ !gex; | |
+ qr{^$pattern_re_capture$}; | |
+ } | |
+ }; | |
$row->{capture} = \@capture; | |
$row->{dest} ||= +{}; | |
@@ -83,7 +105,8 @@ sub match { | |
return undef; | |
} | |
} | |
- if (my @captured = ($env->{PATH_INFO} =~ $self->{pattern_re})) { | |
+ if ($env->{PATH_INFO} =~ $self->{pattern_re}) { | |
+ my @captured = ($env->{PATH_INFO} =~ $self->{pattern_re_capture}); | |
my %args; | |
my @splat; | |
if ($self->{_regexp_capture}) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment