PageRenderTime 28ms CodeModel.GetById 23ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/wwmd/class_extensions/extensions_encoding.rb

http://github.com/miketracy/wwmd
Ruby | 79 lines | 56 code | 13 blank | 10 comment | 3 complexity | db2c3c68fcd6a53941eb9741ca3ed223 MD5 | raw file
 1require 'wwmd/class_extensions/mixins_string_encoding'
 2class String
 3  include WWMD::Encoding
 4
 5  @@he = HTMLEntities.new
 6
 7  # base 64 decode
 8  def b64d
 9    self.unpack("m").first
10  end
11
12  # base 64 encode
13  def b64e
14    [self].pack("m").gsub("\n","")
15  end
16
17  # URI.escape using defaults or passed regexp
18  def escape(reg=nil,unicodify=false)
19    if reg.nil?
20      ret = URI.escape(self)
21    elsif reg.kind_of?(Symbol)
22      case reg
23        when :none; return self
24        when :default; ret =  URI.escape(self)
25        else; ret =  URI.escape(self,WWMD::ESCAPE[reg])
26      end
27    else
28      ret = URI.escape(self,reg)
29    end
30    if unicodify
31      ret.gsub!(/%/,"%u00")
32    end
33    return ret
34  end
35
36  # URI.escape
37  def escape_url(reg=WWMD::ESCAPE[:url])#:nodoc:
38    self.escape(reg)
39  end
40
41  def escape_xss(reg=WWMD::ESCAPE[:xss])#:nodoc:
42    self.escape(reg)
43  end
44
45  def escape_default(reg=WWMD::ESCAPE[:default])
46    self.escape(reg)
47  end
48  # URI.escape all characters in string
49  def escape_all#:nodoc:
50    self.escape(/.*/)
51  end
52
53  # URI.unescape
54  def unescape
55    URI.unescape(self)
56  end
57
58  # html entity encode string
59  #  sym = :basic :named :decimal :hexadecimal
60  def eencode(sym=nil)
61    sym = :named if sym.nil?
62    @@he.encode(self,sym)
63  end
64
65  # decode html entities in string
66  def edecode
67    return @@he.decode(self)
68  end
69
70  # quoted printable
71  def to_qp
72    [self].pack("M")
73  end
74
75  def from_qp
76    self.unpack("M").first
77  end
78
79end