Module: HBLText::Setter
- Defined in:
- lib/hbl_text/setter.rb
Class Method Summary collapse
-
.convert_encoding(str, from_charset = nil) ⇒ Object
——————————————————————————— convert_encoding ———————————————————————————.
-
.get_charset(str) ⇒ Object
——————————————————————————— get_charset ———————————————————————————.
-
.get_charset_from_tag(str) ⇒ Object
——————————————————————————— get_charset_from_tag ———————————————————————————.
-
.to_utf8(arg, from_charset = nil) ⇒ Object
——————————————————————————— to_utf8 ———————————————————————————.
Class Method Details
.convert_encoding(str, from_charset = nil) ⇒ Object
convert_encoding
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/hbl_text/setter.rb', line 54 def convert_encoding( str, from_charset=nil ) encoding = from_charset || get_charset( str ) if encoding.present? return str.encode( 'UTF-8', encoding, :invalid => :replace, :undef => :replace, :replace => "?" ) else return str.encode( 'UTF-8', :invalid => :replace, :undef => :replace, :replace => "?" ) end #if end |
.get_charset(str) ⇒ Object
get_charset
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/hbl_text/setter.rb', line 76 def get_charset( str ) ########################################################### # work on local copy # ########################################################### str = str.dup ########################################################### # if string is valid return immediately # ########################################################### if str.force_encoding("UTF-8").valid_encoding? return "UTF-8" end ########################################################### # now, try the most often string encodings # ########################################################### encodings = %w(Windows-1252 Windows-1251 ISO-8859-1 Macintosh) encodings.each do |enc| return enc if str.force_encoding( enc ).valid_encoding? end ########################################################### # if we are here, then some other encoding is used # # Encoding list: run through all available encodings # ########################################################### Encoding.list.each do |enc| return enc if str.force_encoding( enc ).valid_encoding? end ########################################################### # this code is only reached if everything else failed # ########################################################### # here we give up and set to no encoding return nil end |
.get_charset_from_tag(str) ⇒ Object
get_charset_from_tag
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/hbl_text/setter.rb', line 117 def get_charset_from_tag( str ) # create HTML from fragment html = Nokogiri::HTML::fragment(to_utf8(str)) # create Nokokiris XPath expression to match 'meta', 'charset', 'content-type' # and 'content' in a case insensitive way values = [] downcased_name = "translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='meta'" downcased_chrs = "translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='charset'" downcased_attr = "translate(@http-equiv, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='content-type'" downcased_getr = "translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='content'" # case insensitive fetch <meta charset="whatever"> values << html.xpath("./*[#{downcased_name}]/@*[#{downcased_chrs}]").last.try(:value) # case insensitive fetch <meta http-equiv="Content-Type" content="anything; charset=whatever>" value = html.xpath("./*[#{downcased_name}][#{downcased_attr}]/@*[#{downcased_getr}]").last.try(:value) value = value.to_s.split(/;/). select{|s| s =~ /\s*(?<=charset=).*/}. map{|s| s.match(/\s*(?<=charset=)(.*)/)}. compact.last.try(:[],1) values << value values.compact.last end |
.to_utf8(arg, from_charset = nil) ⇒ Object
to_utf8
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hbl_text/setter.rb', line 34 def to_utf8(arg, from_charset=nil) case arg when String convert_encoding(arg, from_charset) when Array arg.map do |val| to_utf8(val, from_charset) end when Hash arg.map do |key, val| [key, to_utf8(val, from_charset)] end.to_h else arg end end |