Class: MojiBake::Mapper

Inherits:
Object
  • Object
show all
Extended by:
VersionSupport
Includes:
EncodingSupport, JSONSupport
Defined in:
lib/mojibake.rb

Overview

Supports recovering Mojibake characters to the original text.

Constant Summary

Constants included from EncodingSupport

EncodingSupport::CANDIDATE_CHARS, EncodingSupport::HIGH_ORDER_CHARS, EncodingSupport::INTEREST_CHARS, EncodingSupport::INTEREST_CODEPOINTS, EncodingSupport::ISO8, EncodingSupport::UTF8, EncodingSupport::W252

Constants included from JSONSupport

JSONSupport::JSON_CONFIG

Instance Attribute Summary

Attributes included from EncodingSupport

#map_iso_8859_1, #map_permutations, #map_windows_1252

Instance Method Summary collapse

Methods included from VersionSupport

jruby_version_a, ruby_version_a, ver_to_a

Methods included from EncodingSupport

#char_tree, #codepoints_hex, #hash, #regex_encode, #regexp, #table, #tree_flatten

Methods included from JSONSupport

#config, #create_regexp, #hash, #hash_to_json_object, #json, #regexp

Constructor Details

#initialize(opts = {}) ⇒ Mapper

Returns a new instance of Mapper.



37
38
39
40
# File 'lib/mojibake.rb', line 37

def initialize( opts = {} )
  super()
  opts.map { |k,v| send( k.to_s + '=', v ) }
end

Instance Method Details

#recover(input, recursive = true) ⇒ Object

Recover original characters from input using regexp, recursively.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mojibake.rb', line 43

def recover( input, recursive = true )
  output = input.gsub( regexp ) { |moji| hash[moji] }

  # Only recurse if requested and substituted something (output
  # shorter) in this run.
  if recursive && ( output.length < input.length )
    recover( output )
  else
    output
  end
end