class RubyEncode
Public Instance Methods
REncode(textToCode,cleanLine=true,encodeFrom="ISO-8859-1",onErrorNil=true,reviewByte=false)
click to toggle source
# File lib/rencode/rencode.rb, line 4 def REncode(textToCode,cleanLine=true,encodeFrom="ISO-8859-1",onErrorNil=true,reviewByte=false) #Info: ISO-8859-1 is the default character set in most browsers. if cleanLine #Windows / Ubuntu common problems textToCode = textToCode.gsub(/\r/,'') textToCode = textToCode.gsub(/\n/,'') end begin #Normal encoding sanitized = textToCode.force_encoding(encodeFrom).encode!("UTF-8") rescue #CodePoint revision textToCodeCodePoint = "" textToCode.force_encoding(encodeFrom).each_codepoint { |cp| begin case cp when 0 #know problems textToCodeCodePoint = textToCodeCodePoint + ' ' else #puts 'codePoint: ' + cp.to_s + ' ' + cp.chr textToCodeCodePoint = textToCodeCodePoint + cp.chr.encode!("UTF-8") end rescue Exception => e puts 'Error on codepoint: ' + cp.to_s if onErrorNil textToCodeCodePoint = nil break cp end end } #Byte revision (use carefully) reviewByte = false if textToCodeCodePoint == nil if reviewByte textToCodeByte = "" textToCodeCodePoint.force_encoding("UTF-8").each_byte { |bt| case bt when 0 #know problems textToCodeByte = textToCodeByte + ' ' else #puts 'Byte: ' + bt.to_s.force_encoding("UTF-8") + ' ' + bt.chr textToCodeByte = textToCodeByte + bt.chr end } sanitized = textToCodeByte else textToCodeByte = textToCodeCodePoint end end return sanitized.force_encoding("UTF-8") end
REncodeHTML(textToCode,htmlPrint=false)
click to toggle source
# File lib/rencode/rencode.rb, line 61 def REncodeHTML(textToCode,htmlPrint=false) #Info: http://www.w3schools.com/tags/ref_entities.asp if textToCode == nil textToCode = '' else if htmlPrint textToCode = textToCode.gsub('"','"') textToCode = textToCode.gsub("'",''') textToCode = textToCode.gsub('&','&') textToCode = textToCode.gsub('<','<') textToCode = textToCode.gsub('>','>') #textToCode = textToCode.gsub(' ',' ') end textToCode = textToCode.gsub('¡','¡') textToCode = textToCode.gsub('¢','¢') textToCode = textToCode.gsub('£','£') textToCode = textToCode.gsub('¤','¤') textToCode = textToCode.gsub('¥','¥') textToCode = textToCode.gsub('¦','¦') textToCode = textToCode.gsub('§','§') textToCode = textToCode.gsub('¨','¨') textToCode = textToCode.gsub('©','©') textToCode = textToCode.gsub('ª','ª') textToCode = textToCode.gsub('«','«') textToCode = textToCode.gsub('¬','¬') textToCode = textToCode.gsub('','­') textToCode = textToCode.gsub('®','®') textToCode = textToCode.gsub('¯','¯') textToCode = textToCode.gsub('°','°') textToCode = textToCode.gsub('±','±') textToCode = textToCode.gsub('²','²') textToCode = textToCode.gsub('³','³') textToCode = textToCode.gsub('´','´') textToCode = textToCode.gsub('µ','µ') textToCode = textToCode.gsub('¶','¶') textToCode = textToCode.gsub('·','·') textToCode = textToCode.gsub('¸','¸') textToCode = textToCode.gsub('¹','¹') textToCode = textToCode.gsub('º','º') textToCode = textToCode.gsub('»','»') textToCode = textToCode.gsub('¼','¼') textToCode = textToCode.gsub('½','½') textToCode = textToCode.gsub('¾','¾') textToCode = textToCode.gsub('¿','¿') textToCode = textToCode.gsub('×','×') textToCode = textToCode.gsub('÷','÷') textToCode = textToCode.gsub('À','À') textToCode = textToCode.gsub('Á','Á') textToCode = textToCode.gsub('Â','Â') textToCode = textToCode.gsub('Ã','Ã') textToCode = textToCode.gsub('Ä','Ä') textToCode = textToCode.gsub('Å','Å') textToCode = textToCode.gsub('Æ','Æ') textToCode = textToCode.gsub('Ç','Ç') textToCode = textToCode.gsub('È','È') textToCode = textToCode.gsub('É','É') textToCode = textToCode.gsub('Ê','Ê') textToCode = textToCode.gsub('Ë','Ë') textToCode = textToCode.gsub('Ì','Ì') textToCode = textToCode.gsub('Í','Í') textToCode = textToCode.gsub('Î','Î') textToCode = textToCode.gsub('Ï','Ï') textToCode = textToCode.gsub('Ð','Ð') textToCode = textToCode.gsub('Ñ','Ñ') textToCode = textToCode.gsub('Ò','Ò') textToCode = textToCode.gsub('Ó','Ó') textToCode = textToCode.gsub('Ô','Ô') textToCode = textToCode.gsub('Õ','Õ') textToCode = textToCode.gsub('Ö','Ö') textToCode = textToCode.gsub('Ø','Ø') textToCode = textToCode.gsub('Ù','Ù') textToCode = textToCode.gsub('Ú','Ú') textToCode = textToCode.gsub('Û','Û') textToCode = textToCode.gsub('Ü','Ü') textToCode = textToCode.gsub('Ý','Ý') textToCode = textToCode.gsub('Þ','Þ') textToCode = textToCode.gsub('ß','ß') textToCode = textToCode.gsub('à','à') textToCode = textToCode.gsub('á','á') textToCode = textToCode.gsub('â','â') textToCode = textToCode.gsub('ã','ã') textToCode = textToCode.gsub('ä','ä') textToCode = textToCode.gsub('å','å') textToCode = textToCode.gsub('æ','æ') textToCode = textToCode.gsub('ç','ç') textToCode = textToCode.gsub('è','è') textToCode = textToCode.gsub('é','é') textToCode = textToCode.gsub('ê','ê') textToCode = textToCode.gsub('ë','ë') textToCode = textToCode.gsub('ì','ì') textToCode = textToCode.gsub('í','í') textToCode = textToCode.gsub('î','î') textToCode = textToCode.gsub('ï','ï') textToCode = textToCode.gsub('ð','ð') textToCode = textToCode.gsub('ñ','ñ') textToCode = textToCode.gsub('ò','ò') textToCode = textToCode.gsub('ó','ó') textToCode = textToCode.gsub('ô','ô') textToCode = textToCode.gsub('õ','õ') textToCode = textToCode.gsub('ö','ö') textToCode = textToCode.gsub('ø','ø') textToCode = textToCode.gsub('ù','ù') textToCode = textToCode.gsub('ú','ú') textToCode = textToCode.gsub('û','û') textToCode = textToCode.gsub('ü','ü') textToCode = textToCode.gsub('ý','ý') textToCode = textToCode.gsub('þ','þ') textToCode = textToCode.gsub('ÿ','ÿ') end end