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
- #endl ⇒ Object
- #parse_ass ⇒ Object
- #parse_mpl ⇒ Object
- #parse_rsb ⇒ Object
- #parse_srt ⇒ Object
- #parse_sub ⇒ Object
- #parse_time(n) ⇒ Object
- #parse_xml ⇒ Object
- #parse_yml ⇒ Object
-
#ratio ⇒ Object
between our formats, what changes can be reduced to a value.
-
#to_ass ⇒ Object
not mine!.
- #to_mpl ⇒ Object
- #to_rsb ⇒ Object
- #to_srt ⇒ Object
- #to_sub ⇒ Object
- #to_xml ⇒ Object
- #to_yml ⇒ Object
- #xml_lines ⇒ Object
Methods included from PlatformEndLine
Instance Method Details
#endl ⇒ Object
14 15 16 |
# File 'lib/subtitle_it/formats/srt.rb', line 14 def endl endline(@raw) end |
#parse_ass ⇒ Object
6 7 |
# File 'lib/subtitle_it/formats/ass.rb', line 6 def parse_ass end |
#parse_mpl ⇒ Object
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_rsb ⇒ Object
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_srt ⇒ Object
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_sub ⇒ Object
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_xml ⇒ Object
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_yml ⇒ Object
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 |
#ratio ⇒ Object
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_ass ⇒ Object
not mine!
10 11 |
# File 'lib/subtitle_it/formats/ass.rb', line 10 def to_ass end |
#to_mpl ⇒ Object
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_rsb ⇒ Object
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_srt ⇒ Object
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_sub ⇒ Object
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_xml ⇒ Object
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_yml ⇒ Object
15 16 17 |
# File 'lib/subtitle_it/formats/yml.rb', line 15 def to_yml YAML.dump(self) end |
#xml_lines ⇒ Object
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 |