File: cgi.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
autoload(:CGI, 'cgi') # in case you want to use html_unescape

module Innate

  # Shortcuts to some CGI methods

  module Helper
    module CGI
      module_function

      # Shortcut for Rack::Utils.escape
      #
      # @param [#to_s] input
      # @return [String] URI-encoded representation of +input+
      def url_encode(input)
        Rack::Utils.escape(input.to_s)
      end
      alias u url_encode

      # Shortcut for Rack::Utils.unescape
      #
      # @param [#to_s] input
      # @return [String] URI-decoded representation of +input+
      def url_decode(input)
        Rack::Utils.unescape(input.to_s)
      end

      # Shortcut for Rack::Utils.escape_html
      #
      # @param [#to_s] input
      # @return [String]
      def html_escape(input)
        Rack::Utils.escape_html(input.to_s)
      end

      # Shortcut for CGI.unescapeHTML
      #
      # @param [#to_s] input
      # @return [String]
      def html_unescape(input)
        ::CGI.unescapeHTML(input.to_s)
      end

      # safely escape all HTML and code
      def html_and_code_escape(input)
        Rack::Utils.escape_html(input.to_s).gsub(/#([{@$]@?)/, '#\1')
      end
      alias h html_and_code_escape

      # aliases are ignored by module_function...
      module_function :u, :h
    end
  end
end