Class: Asciify

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

Defined Under Namespace

Classes: HTMLEntities, Mapping

Constant Summary collapse

Intermediate =
"UCS-4"
PackFormat =
"N*"

Instance Method Summary collapse

Constructor Details

#initialize(replacement = "?", target = "ASCII", source = "UTF-8") ⇒ Asciify

Returns a new instance of Asciify.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/asciify.rb', line 65

def initialize(replacement = "?", target = "ASCII", source = "UTF-8")
  @from_input_enc = Iconv.new(Intermediate, source)
  @to_output_enc = Iconv.new(target, Intermediate)

  if String === replacement
    r = @from_input_enc.iconv(replacement).unpack(PackFormat)
    @mapping = Hash.new(r)
  else
    @mapping = replacement
  end
end

Instance Method Details

#convert(str) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/asciify.rb', line 77

def convert(str)
  u16s = @from_input_enc.iconv(str)

  s = u16s.unpack(PackFormat).collect { |codepoint|
    codepoint < 128 ? codepoint : @mapping[codepoint]
  }.flatten.compact.pack(PackFormat)

  return @to_output_enc.iconv(s)
end