class CMess::BConv

Convert between bibliographic (and other) encodings.

Constants

DEFAULT_CHARTAB_FILE
ENCODING
VERSION

Attributes

chartab[R]
input[R]
output[R]
source_encoding[R]
target_encoding[R]

Public Class Methods

new(options) click to toggle source
# File lib/cmess/bconv.rb, line 73
def initialize(options)
  @input, @output, _ = CMess.ensure_options!(options,
    :input, :output, :source_encoding, :target_encoding
  )

  @chartab   = self.class.load_chartab(options[:chartab] || DEFAULT_CHARTAB_FILE)
  @encodings = self.class.encodings(@chartab)

  [:source_encoding, :target_encoding].each { |key|
    instance_variable_set("@#{key}", encoding = options[key].upcase)
    instance_variable_set("@have_#{key}", encodings.include?(encoding))
  }
end

Public Instance Methods

convert(*args) click to toggle source
# File lib/cmess/bconv.rb, line 47
def convert(*args)
  new(*args).convert
end
encodings(chartab = DEFAULT_CHARTAB_FILE) click to toggle source
# File lib/cmess/bconv.rb, line 51
def encodings(chartab = DEFAULT_CHARTAB_FILE)
  chartab = load_chartab(chartab)

  chartab[chartab.keys.first].keys.map { |encoding|
    encoding.upcase unless encoding.start_with?('__')
  }.compact.sort
end
load_chartab(chartab) click to toggle source
# File lib/cmess/bconv.rb, line 59
def load_chartab(chartab)
  case chartab
    when Hash
      chartab
    when String
      raise "chartab file not found: #{chartab}" unless File.readable?(chartab)
      SafeYAML.load_file(chartab)
    else
      raise ArgumentError, "invalid chartab of type #{chartab.class}"
  end
end

Private Instance Methods

encode(string, source, target) click to toggle source
# File lib/cmess/bconv.rb, line 131
def encode(string, source, target)
  string.encode(target, source)
rescue Encoding::UndefinedConversionError => err
  warn "ILLEGAL INPUT SEQUENCE: #{err.error_char}"
end
map(char, charmap) click to toggle source
# File lib/cmess/bconv.rb, line 137
def map(char, charmap)
  unless map = charmap[[char]]
    unless map = charmap[[char, c = input.getc]]
      input.ungetc(c) if c
      map = ''
    end
  end

  map
end