Module: GuardHelpers::SrtHelper::ClassMethods

Defined in:
lib/guard_helpers/srt_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_all_subttObject



38
39
40
41
42
43
44
45
46
# File 'lib/guard_helpers/srt_helper.rb', line 38

def build_all_subtt
  Sub.delete_all
  Ln.delete_all
  ::Find.find("subtitle_template") do |p|
    if test(?f, p)
      handle_subtitle_template(p)
    end
  end
end

#format_subtitle_file(file_path) ⇒ Object



81
82
83
84
85
86
# File 'lib/guard_helpers/srt_helper.rb', line 81

def format_subtitle_file(file_path)
  ctn = RoFile.readlines(file_path).map do |line|
    line.gsub(%r{^\s+}, "")
  end.join("\n")
  ::RoFile.write(file_path, ctn)
end

#handle_line(l) ⇒ Object



88
89
90
91
92
93
# File 'lib/guard_helpers/srt_helper.rb', line 88

def handle_line(l)
  Out.out "l:#{l}   file:#{File.basename __FILE__} line:#{__LINE__}", "red" if $RODEBUG
  r = l.match(%r{(?<time>\d{2,6})(?<content>.*)})
  Out.out "r:#{r}   file:#{File.basename __FILE__} line:#{__LINE__}", "red" if $RODEBUG
  return [Subtitle.handle_time(r[:time]), Subtitle.handle_content(r[:content])].flatten
end

#handle_subtitle_template(path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/guard_helpers/srt_helper.rb', line 48

def handle_subtitle_template(path)
  basename = File.basename(path).gsub(%r{\.\w+$}, "")
  sub = Sub.create name: basename
  ls = File.readlines(path).delete_if do |e|
    e.blank?
  end

  ls.each_with_index do |l, i|
    start_time, end_time, content = handle_line(l)
    sub.lns.create(no: i, start_time: start_time, end_time: end_time, content: content)
  end

  Sub.all.each do |sub|
    if sub
      @sub = sub
      filename = "lib/subtitles/#{sub.name}.srt"
      file = File.new(filename, "w+")
      subtt "srt", sub.name
    end
  end
end

#subtt(template, file) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/guard_helpers/srt_helper.rb', line 70

def subtt(template, file)
  if $lib
    template_path = File.join($lib, "templates", template) + ".erb"
    file_path = File.join($lib, "subtitles", file) + ".srt"
    ctn = RoFile.read(template_path)
    result = ERB.new(ctn).result(binding)
    RoFile.write(file_path, result)
    format_subtitle_file(file_path)
  end
end