Class: RubyEncode

Inherits:
Object
  • Object
show all
Defined in:
lib/rencode/rencode.rb

Overview

# encoding: UTF-8

Instance Method Summary collapse

Instance Method Details

#REncode(textToCode, cleanLine = true, encodeFrom = "ISO-8859-1", onErrorNil = true, reviewByte = false) ⇒ Object



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
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
113
114
115
116
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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