[Hiki-cvs 1468] [1015] * hiki/util.rb (Hiki::Util): dead copy from Ruby 1.8 cgi.rb.

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2009年 8月 30日 (日) 22:42:34 JST


Revision: 1015
          http://sourceforge.jp/projects/hiki/svn/view?view=rev&revision=1015
Author:   okkez
Date:     2009-08-30 22:42:34 +0900 (Sun, 30 Aug 2009)

Log Message:
-----------
* hiki/util.rb (Hiki::Util): dead copy from Ruby 1.8 cgi.rb.

Hiki::Util#escape, Hiki::Util#unescape, Hiki::Util#unescapeHTML,
Hiki::Util#escapeHTML.

Modified Paths:
--------------
    hiki/trunk/ChangeLog
    hiki/trunk/hiki/util.rb

Modified: hiki/trunk/ChangeLog
===================================================================
--- hiki/trunk/ChangeLog	2009-08-28 14:17:15 UTC (rev 1014)
+++ hiki/trunk/ChangeLog	2009-08-30 13:42:34 UTC (rev 1015)
@@ -1,3 +1,9 @@
+2009-08-30  okkez  <okkez****@gmail*****>
+
+	* hiki/util.rb (Hiki::Util): dead copy from Ruby 1.8 cgi.rb.
+	Hiki::Util#escape, Hiki::Util#unescape, Hiki::Util#unescapeHTML,
+	Hiki::Util#escapeHTML.
+
 2009-08-28  okkez  <okkez****@gmail*****>
 
 	* hiki/config.rb (Hiki::Config#initialize): improved performance.

Modified: hiki/trunk/hiki/util.rb
===================================================================
--- hiki/trunk/hiki/util.rb	2009-08-28 14:17:15 UTC (rev 1014)
+++ hiki/trunk/hiki/util.rb	2009-08-30 13:42:34 UTC (rev 1015)
@@ -54,8 +54,77 @@
   class PluginException < Exception; end
 
   module Util
-    include ERB::Util
 
+    # dead copy from cgi.rb (Ruby1.8)
+    # URL-encode a string.
+    #   url_encoded_string = escape("'Stop!' said Fred")
+    #      # => "%27Stop%21%27+said+Fred"
+    def escape(string)
+      string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
+        '%' + $1.unpack('H2' * $1.size).join('%').upcase
+      end.tr(' ', '+')
+    end
+
+    # dead copy from cgi.rb (Ruby1.8)
+    # URL-decode a string.
+    #   string = unescape("%27Stop%21%27+said+Fred")
+    #      # => "'Stop!' said Fred"
+    def unescape(string)
+      string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
+        [$1.delete('%')].pack('H*')
+      end
+    end
+
+    # dead copy from cgi.rb (Ruby1.8)
+    # Escape special characters in HTML, namely &\"<>
+    #   escapeHTML('Usage: foo "bar" <baz>')
+    #      # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
+    def escapeHTML(string)
+      string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
+    end
+
+    # dead copy from cgi.rb (Ruby1.8)
+    # Unescape a string that has been HTML-escaped
+    #   unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
+    #      # => "Usage: foo \"bar\" <baz>"
+    def unescapeHTML(string)
+      string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/n) do
+        match = $1.dup
+        case match
+        when 'amp'                 then '&'
+        when 'quot'                then '"'
+        when 'gt'                  then '>'
+        when 'lt'                  then '<'
+        when /\A#0*(\d+)\z/n       then
+          if Integer($1) < 256
+            Integer($1).chr
+          else
+            if Integer($1) < 65536 and $KCODE[0] == ?U
+              [Integer($1)].pack("U")
+            else
+              "&##{$1};"
+            end
+          end
+        when /\A#x([0-9a-f]+)\z/ni then
+          if $1.hex < 256
+            $1.hex.chr
+          else
+            if $1.hex < 65536 and $KCODE[0] == ?U
+              [$1.hex].pack("U")
+            else
+              "&#x#{$1};"
+            end
+          end
+        else
+          "&#{match};"
+        end
+      end
+    end
+
+    alias escape_html escapeHTML
+    alias h escapeHTML
+    alias unescape_html unescapeHTML
+
     def plugin_error(method, e)
       msg = "<strong>#{e.class} (#{h(e.message)}): #{h(method)}</strong><br>"
       msg << "<strong>#{e.backtrace.join("<br>\n")}</strong>" if****@conf*****_debug




Hiki-cvs メーリングリストの案内
Back to archive index