module Ara2yml

Constants

VERSION

Public Instance Methods

convert(input, output) click to toggle source
# File lib/ara2yml.rb, line 21
def convert(input, output)
  tempfile = extract_database(input)
  translations = extract_translations(tempfile.path)
  tempfile.unlink
  dump_nice_yaml(translations, output)
end
dump_nice_yaml(translations, filename) click to toggle source
# File lib/ara2yml.rb, line 65
def dump_nice_yaml(translations, filename)
  File.open(filename, 'w') { |f|
    f.write "---\n"
    translations.each { |e|
      first_key = true
      if e
        f.write '- '
        e.each { |k, v|
          f.write '  ' unless first_key
          f.write ":#{k}: |-\n"
          f.write "    #{v}\n"
          first_key = false
        }
      else
        f.write "-\n"
      end
    }
  }
end
extract_database(ara_filename) click to toggle source
# File lib/ara2yml.rb, line 28
def extract_database(ara_filename)
  tempfile = Tempfile.new('ara2yml')
  tempfile.close
  Zip::File.open(ara_filename) do |zip_file|
    entry = zip_file.entries[0]
    $stderr.puts "Extracting #{entry.name} from #{ara_filename} into #{tempfile.path}"
    entry.extract(tempfile.path) { true }
  end
  tempfile
end
extract_translations(db_filename) click to toggle source
# File lib/ara2yml.rb, line 39
def extract_translations(db_filename)
  db = Mdb.open(db_filename)
  language_names = db['LanguageNames'].each_with_object({}) { |e, a|
    a[e[:Id]] = e[:LName].to_sym
  }
  languages = db['Languages'].each_with_object({}) { |e, a|
    a[e[:Id]] = language_names[e[:Id_LanguageName]]
  }
  result = db['Paragraphs'].each_with_object([]) { |e, a|
    if (text = e[:XmlData])
      start = text.index('<text>') + 6
      finish = text.index('</text>')
      text = text[start...finish]
    end
    (a[Integer(e[:PIndex])] ||= {})[languages[e[:Id_Language]]] = text
  }
  result.each_with_index { |v, k|
    if !v || v.values.all?(&:nil?)
      result.delete_at(k)
    elsif v.values.all? { |s| s.strip.length == 0 }
      result[k] = nil
    end
  }
  result
end