Module: Formats

Includes:
PlatformEndLine
Included in:
SubtitleIt::Subtitle
Defined in:
lib/subtitle_it/formats/ass.rb,
lib/subtitle_it/formats/mpl.rb,
lib/subtitle_it/formats/rsb.rb,
lib/subtitle_it/formats/srt.rb,
lib/subtitle_it/formats/sub.rb,
lib/subtitle_it/formats/xml.rb,
lib/subtitle_it/formats/yml.rb

Overview

SubtitleIt YML Dump

Instance Method Summary collapse

Methods included from PlatformEndLine

#endline, #platform

Instance Method Details

#endlObject



14
15
16
# File 'lib/subtitle_it/formats/srt.rb', line 14

def endl
  endline(@raw)
end

#parse_assObject



6
7
# File 'lib/subtitle_it/formats/ass.rb', line 6

def parse_ass
end

#parse_mplObject



10
11
12
13
14
15
16
17
18
# File 'lib/subtitle_it/formats/mpl.rb', line 10

def parse_mpl
  @raw.lines.inject([]) do |i, l|
    line_data = l.scan(/^\[([0-9]{1,})\]\[([0-9]{1,})\](.+)$/)
    line_data = line_data.at 0
    time_on, time_off, text = line_data
    time_on, time_off = [time_on.to_i, time_off.to_i].map { |t| t.to_i * 1000 }
    i << Subline.new(time_on, time_off, text.chomp)
  end
end

#parse_rsbObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/subtitle_it/formats/rsb.rb', line 13

def parse_rsb
  inn = @raw.lines
  @title = inn.delete_at(0).split(':')[1]
  @authors = inn.delete_at(0).split(':')[1]
  @version = inn.delete_at(0).split(':')[1]
  inn.inject([]) do |final, line|
    time_on, time_off = line.split('=>').map(&:strip)
    text = line.split('==')[1] # .strip
    final << Subline.new(time_on, time_off, text ? text.strip : nil)
  end
end

#parse_srtObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/subtitle_it/formats/srt.rb', line 18

def parse_srt
  @raw.split(endl * 2).inject([]) do |final, line|
    line = line.split(endl)
    line.delete_at(0)
    time_on, time_off = line[0].split('-->').map(&:strip)
    line.delete_at(0)
    text = line.join('|')
    final << Subline.new(time_on, time_off, text)
  end
end

#parse_subObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/subtitle_it/formats/sub.rb', line 25

def parse_sub
  @raw.lines.reduce([]) do |i, l|
    line_data = l.scan(/^\{([0-9]{1,})\}\{([0-9]{1,})\}(.+)$/)
    line_data = line_data.at 0
    time_on, time_off, text = line_data
    time_on, time_off = [time_on.to_i, time_off.to_i].map do |t|
      (t.to_i / @fps * 1000 / ratio).to_i
    end
    i << Subline.new(time_on, time_off, text ? text.chomp : nil)
  end
end

#parse_time(n) ⇒ Object



46
47
48
# File 'lib/subtitle_it/formats/sub.rb', line 46

def parse_time(n)
  n.to_i / 1000 * @fps * ratio
end

#parse_xmlObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/subtitle_it/formats/xml.rb', line 27

def parse_xml
  final = []
  doc = Nokogiri::XML.parse(@raw)
  (doc / 'tt' / 'p').each do |line|
    time_on, time_off = %w(begin dur).map { |str| line[str.to_sym] }
    text = line.inner_html
    final << Subline.new(time_on, time_off, text)
  end
  final
end

#parse_ymlObject



6
7
8
9
10
11
12
13
# File 'lib/subtitle_it/formats/yml.rb', line 6

def parse_yml
  @yaml = YAML.load(@raw)
  header = @yaml.delete('header')
  @title = header['title']
  @author = header['authors']
  @version = header['version']
  @yaml['lines'].map { |l| Subline.new(l[0], l[1], l[2]) }
end

#ratioObject

between our formats, what changes can be reduced to a value



21
22
23
# File 'lib/subtitle_it/formats/sub.rb', line 21

def ratio
  1
end

#to_assObject

not mine!



10
11
# File 'lib/subtitle_it/formats/ass.rb', line 10

def to_ass
end

#to_mplObject



20
21
22
23
24
25
26
27
28
# File 'lib/subtitle_it/formats/mpl.rb', line 20

def to_mpl
  endl = endline(@raw)
  line_ary = []
  @lines.each do |l|
    start, stop = [l.time_on, l.time_off].map { |val| val.to_i / 100 }
    line_ary << '[%d][%d]%s' % [start, stop, l.text]
  end
  line_ary.join(endl) + endl
end

#to_rsbObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/subtitle_it/formats/rsb.rb', line 25

def to_rsb
  endl = endline(@raw)
  out = ["- title: #{@title}", "- authors: #{@authors}", "- version: #{@version}"]
  @lines.each do |l|
    out << '%s => %s == %s' % [l.time_on.to_s, l.time_off.to_s, l.text]
  end
  out.join(endl) + endl
  # out = "- title: #{@title}\n- authors: #{@authors}\n- version: #{@version}\n"
  # out << @lines.inject([]) do |i,l|
  #  i << "%s => %s == %s" % [l.time_on.to_s, l.time_off.to_s, l.text]
  # end.join("\n")
end

#to_srtObject



29
30
31
32
33
34
35
36
37
# File 'lib/subtitle_it/formats/srt.rb', line 29

def to_srt
  out = []
  @lines.each_with_index do |l, i|
    out << "#{i + 1}"
    out << '%s --> %s' % [l.time_on.to_s(','), l.time_off.to_s(',')]
    out << (l.text ? l.text.gsub('|', endl) : ' ') + endl
  end
  out.join(endl)
end

#to_subObject



37
38
39
40
41
42
43
44
# File 'lib/subtitle_it/formats/sub.rb', line 37

def to_sub
  endl = endline(@raw)
  line_ary = []
  @lines.each do |l|
    line_ary << '{%d}{%d}%s' % [parse_time(l.time_on), parse_time(l.time_off), l.text]
  end
  line_ary.join(endl) + endl
end

#to_xmlObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/subtitle_it/formats/xml.rb', line 48

def to_xml
  endl = endline(@raw)
  out = <<XML
<?xml version="1.0" encoding="UTF-8"?>
<tt xml:lang="en" xmlns="http://www.w3.org/2006/10/ttaf1"  xmlns:tts="http://www.w3.org/2006/10/ttaf1#styling">
<head>
  <styling>#{@style + endl if @style}
  </styling>
</head>
<body>
  <div xml:lang="en">
#{xml_lines}
  </div>
</body>
</tt>
XML
  out.chomp
end

#to_ymlObject



15
16
17
# File 'lib/subtitle_it/formats/yml.rb', line 15

def to_yml
  YAML.dump(self)
end

#xml_linesObject



38
39
40
41
42
43
44
45
46
# File 'lib/subtitle_it/formats/xml.rb', line 38

def xml_lines
  endl = endline(@raw)
  line_ary = []
  @lines.each do |l|
    toff = l.time_off - l.time_on
    line_ary << "      <p begin=\"#{l.time_on}\" dur=\"#{toff}\">#{l.text}</p>"
  end
  line_ary.join(endl)
end