class Translatomatic::ResourceFile::Subtitle

Subtitle resource file. requires 'titlekit' gem

Public Class Methods

enabled?() click to toggle source

(see Base.enabled?)

# File lib/translatomatic/resource_file/subtitle.rb, line 12
def self.enabled?
  @enabled ||= begin
    require 'titlekit'
    true
  rescue LoadError
    false
  end
end
extensions() click to toggle source

(see Base.extensions)

# File lib/translatomatic/resource_file/subtitle.rb, line 7
def self.extensions
  %w[srt ass ssa]
end

Public Instance Methods

save(target = path, options = {}) click to toggle source

(see Base#save)

# File lib/translatomatic/resource_file/subtitle.rb, line 33
def save(target = path, options = {})
  add_created_by unless options[:no_created_by] || created_by?
  export(target)
end
set(key, value) click to toggle source

(see Base#set)

Calls superclass method Translatomatic::ResourceFile::Base#set
# File lib/translatomatic/resource_file/subtitle.rb, line 22
def set(key, value)
  super(key, value)

  if @subtitle_map.include?(key)
    @subtitle_map[key][:lines] = value.to_s
  else
    add_subtitle(lines: value) unless value.blank?
  end
end

Private Instance Methods

add_created_by() click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 59
def add_created_by
  # TODO
end
add_subtitle(subtitle = {}) click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 63
def add_subtitle(subtitle = {})
  key = "key#{@keynum}"
  subtitle[:id] ||= @keynum
  subtitle[:start] ||= @keynum * 10
  subtitle[:end] ||= @keynum * 10 + 5
  process_metadata(key, subtitle)
  @keynum += 1
  @subtitle_map[key] = subtitle
  @subtitles << subtitle
end
export(target, _options = {}) click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 95
def export(target, _options = {})
  content = import_export_class(target).export(@subtitles) || ''
  content = content.gsub(/[\r\n]+\Z/, '') + "\n"
  target.write(content)
end
find_gap(min_length) click to toggle source

Find the first gap in subtitles with a minimum length in seconds. @return [Array] [start, end] Start and end times of the gap

# File lib/translatomatic/resource_file/subtitle.rb, line 76
def find_gap(min_length)
  last = 0
  @subtitles.each do |subtitle|
    return [last, subtitle.start] if subtitle.start - last >= min_length
    last = subtitle.end
  end
  [last, -1]
end
import(path) click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 89
def import(path)
  contents = read_contents(path)
  return [] if contents.blank?
  import_export_class(path).import(contents)
end
import_export_class(path) click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 101
def import_export_class(path)
  class_name = path.extname.sub(/^\./, '').upcase
  Titlekit.const_get(class_name)
end
init() click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 40
def init
  @subtitle_map = {}
  @subtitles = []
  @keynum = 1
end
init_properties() click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 85
def init_properties
  @properties = @subtitle_map.transform_values { |i| i[:lines] }
end
load() click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 46
def load
  @metadata.reset
  subtitles = import(@path)
  subtitles.each { |i| add_subtitle(i) }
  init_properties
end
process_metadata(key, subtitle) click to toggle source
# File lib/translatomatic/resource_file/subtitle.rb, line 53
def process_metadata(key, subtitle)
  lines = subtitle[:lines] || ''
  context = @metadata.parse_comment(lines)
  @metadata.assign_key(key) unless context.present?
end