Class: WebVTT::Cue

Inherits:
Object
  • Object
show all
Defined in:
lib/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cue) ⇒ Cue

Returns a new instance of Cue.



85
86
87
88
# File 'lib/parser.rb', line 85

def initialize(cue)
  @content = cue
  parse
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



83
84
85
# File 'lib/parser.rb', line 83

def end
  @end
end

#identifierObject

Returns the value of attribute identifier.



83
84
85
# File 'lib/parser.rb', line 83

def identifier
  @identifier
end

#startObject

Returns the value of attribute start.



83
84
85
# File 'lib/parser.rb', line 83

def start
  @start
end

#styleObject

Returns the value of attribute style.



83
84
85
# File 'lib/parser.rb', line 83

def style
  @style
end

#textObject

Returns the value of attribute text.



83
84
85
# File 'lib/parser.rb', line 83

def text
  @text
end

Class Method Details

.timestamp_in_sec(timestamp) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/parser.rb', line 101

def self.timestamp_in_sec(timestamp)
  mres = timestamp.match(/([0-9]{2}):([0-9]{2}):([0-9]{2}\.[0-9]{3})/)
  sec = mres[3].to_f # seconds and subseconds
  sec += mres[2].to_f * 60 # minutes
  sec += mres[1].to_f * 60 * 60 # hours
  return sec
end

Instance Method Details

#end_in_secObject



113
114
115
# File 'lib/parser.rb', line 113

def end_in_sec
  Cue.timestamp_in_sec(@end)
end

#lengthObject



117
118
119
# File 'lib/parser.rb', line 117

def length
  end_in_sec - start_in_sec
end

#parseObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/parser.rb', line 121

def parse
  lines = @content.split("\n").map(&:strip)

  # it's a note, ignore
  return if lines[0] =~ /NOTE/

  if !lines[0].include?("-->")
    @identifier = lines[0]
    lines.shift
  end

  if lines[0].match(/([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}) -+> ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3})(.*)/)
    @start = $1
    @end = $2
    @style = Hash[$3.strip.split(" ").map{|s| s.split(":").map(&:strip) }]
  end
  @text = lines[1..-1].join("\n")
end

#start_in_secObject



109
110
111
# File 'lib/parser.rb', line 109

def start_in_sec
  Cue.timestamp_in_sec(@start)
end

#to_webvttObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/parser.rb', line 90

def to_webvtt
  res = ""
  if @identifier
    res << "#{@identifier}\n"
  end
  res << "#{@start} --> #{@end} #{@style.map{|k,v| "#{k}:#{v}"}.join(" ")}".strip + "\n"
  res << @text

  res
end