Permalink
Browse files

Let URI.pm do url-escaping for us

It follows the spec and handles unicode correctly.
  • Loading branch information...
1 parent 2977e49 commit 18eec5fff814c366d92afc8ba8d820b9defa9c08 @flussence flussence committed Jun 26, 2012
Showing with 13 additions and 44 deletions.
  1. +1 −1 META.info
  2. +12 −2 lib/Pod/To/HTML.pm
  3. +0 −41 lib/Text/Escape.pm
View
@@ -2,6 +2,6 @@
"name" : "Pod::To::HTML",
"version" : "*",
"description" : "Convert Perl 6 Pod to HTML",
- "depends" : [],
+ "depends" : ["URI"],
"source-url" : "git://github.com/perl6/Pod-To-HTML.git"
}
View
@@ -1,5 +1,5 @@
class Pod::To::HTML;
-use Text::Escape;
+use URI::Escape;
method render($pod) {
pod2html($pod)
@@ -18,6 +18,16 @@ my @footnotes;
sub Debug(Callable $) { } # Disable debug code
#sub Debug(Callable $c) { $c() } # Enable debug code
+sub escape_html(Str $str) returns Str {
+ return $str unless $str ~~ /<[&<>"']>/;
+
+ $str.subst(q{&}, q{&amp;}, :g)\
+ .subst(q{<}, q{&lt;}, :g)\
+ .subst(q{>}, q{&gt;}, :g)\
+ .subst(q{"}, q{&quot;}, :g)\
+ .subst(q{'}, q{&#39;}, :g);
+}
+
#= Converts a Pod tree to a HTML document.
sub pod2html($pod) is export returns Str {
@body.push: node2html($pod);
@@ -253,7 +263,7 @@ multi sub node2html(Pod::Heading $node) returns Str {
Debug { say "Heading node2html called for {$node.perl}" };
my $lvl = min($node.level, 6); #= HTML only has 6 levels of numbered headings
my %escaped = (
- uri => escape_uri(node2rawtext($node.content)),
+ uri => uri_escape(node2rawtext($node.content)),
html => node2inline($node.content),
);
@indexes.push: Pair.new(key => $lvl, value => %escaped);
View
@@ -1,41 +0,0 @@
-use v6;
-module Text::Escape;
-
-sub escape($str as Str, Str $how) is export {
- given $how.lc {
- when 'none' { $str }
- when 'html' { escape_html($str) }
- when 'uri' | 'url' { escape_uri($str) }
- default { fail "Don't know how to escape format $how yet" }
- }
-}
-
-sub escape_html(Str $str) returns Str is export {
- return $str unless $str ~~ /<[&<>"']>/;
-
- $str.subst(q{&}, q{&amp;}, :g)\
- .subst(q{<}, q{&lt;}, :g)\
- .subst(q{>}, q{&gt;}, :g)\
- .subst(q{"}, q{&quot;}, :g)\
- .subst(q{'}, q{&#39;}, :g);
-}
-
-sub escape_uri(Str $str) returns Str is export {
- return $str unless $str ~~ /<-[a..z A..Z 0..9 \-_.!~*'()]>/;
-
- my Str $allowed = 'abcdefghijklmnopqrstuvwxyz'
- ~ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- ~ '0123456789'
- ~ q{-_.!~*'()};
-
- return [~] $str.comb.map(-> $char {
- given $char {
- when q{ } { q{+} }
- when defined $allowed.index($char) { $char }
- # TODO: each char should be UTF-8 encoded, then its bytes %-encoded
- default { q{%} ~ ord($char).fmt('%x') }
- }
- });
-}
-
-# vim:ft=perl6

0 comments on commit 18eec5f

Please sign in to comment.