Commits

Toby Inkster committed 7160530

URI unescaping

Comments (0)

Files changed (2)

examples/turtle_tokens.pl

 @prefix quux: <quux#>.
 
 <xyz>
+   foo:foo <\U00000061bc>
    foo:bar 123;
    foo:baz "Yeah\"Baby\"Yeah";
    foo:bum quux:quuux.

lib/Syntax/Highlight/RDF.pm

 {
 	use HTML::HTML5::Entities qw/encode_entities/;
 	no strict 'refs';
+	
+	my $unescape = sub
+	{
+		my $u = $_[0];
+		$u =~ s{
+			(  \\  ([\\nrt"]|U[A-Fa-f0-9]{8}|u[A-Fa-f0-9]{4})  )
+		}{
+			$2 eq "\\"    ? "\\" :
+			$2 eq "n"     ? "\n" :
+			$2 eq "r"     ? "\r" :
+			$2 eq "t"     ? "\t" :
+			$2 eq '"'     ? "\"" :
+			chr hex("0x".substr($2, 1))
+		}exg;
+		return $u;
+	};
+	
 	*{Feature    . "::tok"}        = sub { sprintf "%s~", $_[0]->TYPE };
 	*{Token      . "::tok"}        = sub { sprintf "%s[%s]", $_[0]->TYPE, $_[0]->spelling };
 	*{Whitespace . "::tok"}        = sub { $_[0]->TYPE };
 		sprintf "<span class=\"rdf_%s\">%s</span>", lc $_[0]->TYPE, encode_entities($_[0]->spelling)
 	};
 	*{Whitespace . "::TO_HTML"}  = sub { $_[0]->spelling };
-	*{URIRef     . "::uri"}      = sub { my $u = $_[0]->spelling; substr $u, 1, length($u)-2 };  # XXX - unescape
+	*{URIRef     . "::uri"}      = sub { my $u = $_[0]->spelling; $unescape->(substr $u, 1, length($u)-2) };
 	*{CURIE      . "::prefix"}   = sub { (split ":", $_[0]->spelling)[0] };
 	*{CURIE      . "::suffix"}   = sub { (split ":", $_[0]->spelling)[1] };
 	*{PrefixDefinition_Start . "::tok"} = sub {
 sub _pull_uri
 {
 	my $self = shift;
-	$self->_pull_token($1, URIRef)
-		if ${$self->_remaining} =~ m/^(<(?:\\\\|\\>|\\<|[^<>\\]){0,1024}>)/;
+	return $self->_pull_token($1, URIRef)
+		if ${$self->_remaining} =~ m/^(<(?:\\\\|\\.|[^<>\\]){0,1024}>)/;
+	$self->_pull_token("<", Unknown);
 }
 
 sub _pull_curie
 {
 	my $self = shift;
-	$self->_pull_token($1, CURIE)
+	return $self->_pull_token($1, CURIE)
 		if ${$self->_remaining} =~ m/^(([$nameStartChar2][$nameChar]*)?:([$nameStartChar2][$nameChar]*)?)/;
+	$self->_pull_token(substr(${$self->_remaining}, 0, 1), Unknown);
 }
 
 # Same rules as RDF::TrineX::Parser::Pretdsl
 		{
 			$self->_pull_token($matches->[0], Language);
 		}
-		elsif ($matches = $self->_peek(qr/^(#.*)(\r|\n|$)/ims))
+		elsif ($matches = $self->_peek(qr/^(#.*?)(?:\r|\n|$)/is))
 		{
 			$self->_pull_token($matches->[0], Comment);
 		}
 	require URI;
 	
 	# Where the base itself is relative
-	if (not URI->new($base)->scheme)
+	if (!URI->new($relative)->scheme and !URI->new($base)->scheme)
 	{
-		if ($base =~ m{^/})
-		{
-			return "URI"->new_abs(@_)->as_string;
-		}
-		else
-		{
-			return substr("URI"->new_abs(@_)->as_string, 1);
-		}
+		return (
+			$base =~ m{^/}
+				? "URI"->new_abs(@_)->as_string
+				: substr("URI"->new_abs(@_)->as_string, 1)
+		);
 	}
 	
 	"URI"->new_abs(@_)->as_string;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.