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('<','&#60;')
                        textToCode = textToCode.gsub('>','&#62;')
                        #textToCode = textToCode.gsub(' ','&#160;')
                end
                
                textToCode = textToCode.gsub('¡','&#161;')
                textToCode = textToCode.gsub('¢','&#162;')
                textToCode = textToCode.gsub('£','&#163;')
                textToCode = textToCode.gsub('¤','&#164;')
                textToCode = textToCode.gsub('¥','&#165;')
                textToCode = textToCode.gsub('¦','&#166;')
                textToCode = textToCode.gsub('§','&#167;')
                textToCode = textToCode.gsub('¨','&#168;')
                textToCode = textToCode.gsub('©','&#169;')
                textToCode = textToCode.gsub('ª','&#170;')
                textToCode = textToCode.gsub('«','&#171;')
                textToCode = textToCode.gsub('¬','&#172;')
                textToCode = textToCode.gsub('­','&#173;')
                textToCode = textToCode.gsub('®','&#174;')
                textToCode = textToCode.gsub('¯','&#175;')
                textToCode = textToCode.gsub('°','&#176;')
                textToCode = textToCode.gsub('±','&#177;')
                textToCode = textToCode.gsub('²','&#178;')
                textToCode = textToCode.gsub('³','&#179;')
                textToCode = textToCode.gsub('´','&#180;')
                textToCode = textToCode.gsub('µ','&#181;')
                textToCode = textToCode.gsub('¶','&#182;')
                textToCode = textToCode.gsub('·','&#183;')
                textToCode = textToCode.gsub('¸','&#184;')
                textToCode = textToCode.gsub('¹','&#185;')
                textToCode = textToCode.gsub('º','&#186;')
                textToCode = textToCode.gsub('»','&#187;')
                textToCode = textToCode.gsub('¼','&#188;')
                textToCode = textToCode.gsub('½','&#189;')
                textToCode = textToCode.gsub('¾','&#190;')
                textToCode = textToCode.gsub('¿','&#191;')
                textToCode = textToCode.gsub('×','&#215;')
                textToCode = textToCode.gsub('÷','&#247;')


                textToCode = textToCode.gsub('À','&#192;')
                textToCode = textToCode.gsub('Á','&#193;')
                textToCode = textToCode.gsub('Â','&#194;')
                textToCode = textToCode.gsub('Ã','&#195;')
                textToCode = textToCode.gsub('Ä','&#196;')
                textToCode = textToCode.gsub('Å','&#197;')
                textToCode = textToCode.gsub('Æ','&#198;')
                textToCode = textToCode.gsub('Ç','&#199;')
                textToCode = textToCode.gsub('È','&#200;')
                textToCode = textToCode.gsub('É','&#201;')
                textToCode = textToCode.gsub('Ê','&#202;')
                textToCode = textToCode.gsub('Ë','&#203;')
                textToCode = textToCode.gsub('Ì','&#204;')
                textToCode = textToCode.gsub('Í','&#205;')
                textToCode = textToCode.gsub('Î','&#206;')
                textToCode = textToCode.gsub('Ï','&#207;')
                textToCode = textToCode.gsub('Ð','&#208;')
                textToCode = textToCode.gsub('Ñ','&#209;')
                textToCode = textToCode.gsub('Ò','&#210;')
                textToCode = textToCode.gsub('Ó','&#211;')
                textToCode = textToCode.gsub('Ô','&#212;')
                textToCode = textToCode.gsub('Õ','&#213;')
                textToCode = textToCode.gsub('Ö','&#214;')
                textToCode = textToCode.gsub('Ø','&#216;')
                textToCode = textToCode.gsub('Ù','&#217;')
                textToCode = textToCode.gsub('Ú','&#218;')
                textToCode = textToCode.gsub('Û','&#219;')
                textToCode = textToCode.gsub('Ü','&#220;')
                textToCode = textToCode.gsub('Ý','&#221;')
                textToCode = textToCode.gsub('Þ','&#222;')
                textToCode = textToCode.gsub('ß','&#223;')
                textToCode = textToCode.gsub('à','&#224;')
                textToCode = textToCode.gsub('á','&#225;')
                textToCode = textToCode.gsub('â','&#226;')
                textToCode = textToCode.gsub('ã','&#227;')
                textToCode = textToCode.gsub('ä','&#228;')
                textToCode = textToCode.gsub('å','&#229;')
                textToCode = textToCode.gsub('æ','&#230;')
                textToCode = textToCode.gsub('ç','&#231;')
                textToCode = textToCode.gsub('è','&#232;')
                textToCode = textToCode.gsub('é','&#233;')
                textToCode = textToCode.gsub('ê','&#234;')
                textToCode = textToCode.gsub('ë','&#235;')
                textToCode = textToCode.gsub('ì','&#236;')
                textToCode = textToCode.gsub('í','&#237;')
                textToCode = textToCode.gsub('î','&#238;')
                textToCode = textToCode.gsub('ï','&#239;')
                textToCode = textToCode.gsub('ð','&#240;')
                textToCode = textToCode.gsub('ñ','&#241;')
                textToCode = textToCode.gsub('ò','&#242;')
                textToCode = textToCode.gsub('ó','&#243;')
                textToCode = textToCode.gsub('ô','&#244;')
                textToCode = textToCode.gsub('õ','&#245;')
                textToCode = textToCode.gsub('ö','&#246;')
                textToCode = textToCode.gsub('ø','&#248;')
                textToCode = textToCode.gsub('ù','&#249;')
                textToCode = textToCode.gsub('ú','&#250;')
                textToCode = textToCode.gsub('û','&#251;')
                textToCode = textToCode.gsub('ü','&#252;')
                textToCode = textToCode.gsub('ý','&#253;')
                textToCode = textToCode.gsub('þ','&#254;')
                textToCode = textToCode.gsub('ÿ','&#255;')
        end 
end