Module: BlueFeather::Util

Defined in:
lib/bluefeather.rb

Constant Summary collapse

HTML_ESC =
{
	'&' => '&',
	'"' => '"',
	'<' => '&lt;',
	'>' => '&gt;'
}

Class Method Summary collapse

Class Method Details

.change_kcode(kcode = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/bluefeather.rb', line 188

def change_kcode(kcode = nil)
	if defined?(Encoding) then
		# ruby 1.9 later
		yield
	else
		# ruby 1.8 earlier
		original_kcode = $KCODE
	
		begin
			$KCODE = kcode if kcode
			yield
			
		ensure
			# recover
			$KCODE = original_kcode
		end
	end # if defined?
end

.escape_html(str) ⇒ Object

(Author: Minero Aoki)



173
174
175
176
# File 'lib/bluefeather.rb', line 173

def escape_html(str)
	table = HTML_ESC   # optimize
	str.gsub(/[&"<>]/) {|s| table[s] }
end

.generate_blank_string_io(encoding_base) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/bluefeather.rb', line 178

def generate_blank_string_io(encoding_base)
	io = StringIO.new
	
	if io.respond_to?(:set_encoding) then
		io.set_encoding(encoding_base.encoding)
	end
	
	return io
end

.utf8_bom?(str) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/bluefeather.rb', line 208

def utf8_bom?(str)
	if str.respond_to?(:getbyte) and str.respond_to?(:bytesize) then
		if str.bytesize >= 3 and
		str.getbyte(0) == UTF8_BOM.getbyte(0) and
		str.getbyte(1) == UTF8_BOM.getbyte(1) and
		str.getbyte(2) == UTF8_BOM.getbyte(2) then
			return true
		else
			return false
		end
		
	else
		return(str =~ UTF8_BOM_PATTERN ? true : false)
	end
end