class Sanzang::Command::Translate

This class provides a command for simple translation of one file or text. Input and output text can be read from either stdin and stdout, or from files. For mass translation of texts, see Sanzang::Command::Batch.

Attributes

name[R]

Name of the command

Public Class Methods

new() click to toggle source

Create a new instance of the Translate class.

# File lib/sanzang/command/translate.rb, line 30
def initialize
  @name = "sanzang translate"
  @encoding = Sanzang::Platform.data_encoding
  @infile = nil
  @outfile = nil
  @verbose = false
end

Public Instance Methods

run(args) click to toggle source

Run the translate command with the given arguments. The parameter args would typically be an array of command options and parameters. Calling this with the “-h” or “–help” option will print full usage information necessary for running this command.

# File lib/sanzang/command/translate.rb, line 43
def run(args)
  parser = option_parser
  parser.parse!(args)

  if args.length != 1
    $stderr.puts parser
    return 1
  end

  translator = nil
  File.open(args[0], "rb", encoding: @encoding) do |table_file|
    table = Sanzang::TranslationTable.new(table_file.read)
    translator = Sanzang::Translator.new(table)
  end

  begin
    fin = @infile ? File.open(@infile, "rb") : $stdin
    fin.binmode.set_encoding(@encoding)
    fout = @outfile ? File.open(@outfile, "wb") : $stdout
    fout.binmode.set_encoding(@encoding)
    translator.translate_io(fin, fout)
  ensure
    if defined?(fin) and fin.class == File
      fin.close if not fin.closed?
    end
    if defined?(fout) and fout.class == File
      fout.close if not fout.closed?
    end
  end

  return 0
rescue SystemExit => err
  return err.status
rescue Interrupt
  puts
  return 0
rescue Errno::EPIPE
  return 0
rescue Exception => err
  if @verbose
    $stderr.puts err.backtrace
  end
  $stderr.puts "#{@name.split[0]}: #{err.inspect}"
  return 1
end

Private Instance Methods

option_parser() click to toggle source

An OptionParser for the command

# File lib/sanzang/command/translate.rb, line 97
def option_parser
  OptionParser.new do |op|
    op.banner = "Usage: #{@name} [options] table\n"

    op.banner << "\nTranslate text using simple table rules. Input text "
    op.banner << "is read from STDIN by\ndefault, and the output is "
    op.banner << "written to STDOUT by default. The translation table "
    op.banner << "\nfile is specified as a parameter.\n"

    op.banner << "\nOptions:\n"

    op.on("-h", "--help", "show this help message and exit") do |v|
      puts op
      exit 0
    end
    op.on("-E", "--encoding=ENC", "set data encoding to ENC") do |v|
      @encoding = Encoding.find(v)
    end
    op.on("-L", "--list-encodings", "list possible encodings") do |v|
      Sanzang::Platform.valid_encodings.each {|e| puts e.to_s }
      exit 0
    end
    op.on("-i", "--infile=FILE", "read input text from FILE") do |v|
      @infile = v
    end
    op.on("-o", "--outfile=FILE", "write output text to FILE") do |v|
      @outfile = v
    end
    op.on("-v", "--verbose", "verbose mode for debugging") do |v|
      @verbose = true
    end
  end
end