Class: WebVTT::Cue

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cue = nil) ⇒ Cue

Returns a new instance of Cue.



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

def initialize(cue = nil)
  @content = cue
  @style = {}
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



107
108
109
# File 'lib/webvtt/parser.rb', line 107

def end
  @end
end

#identifierObject

Returns the value of attribute identifier.



107
108
109
# File 'lib/webvtt/parser.rb', line 107

def identifier
  @identifier
end

#plain_textObject

Returns the value of attribute plain_text.



107
108
109
# File 'lib/webvtt/parser.rb', line 107

def plain_text
  @plain_text
end

#startObject

Returns the value of attribute start.



107
108
109
# File 'lib/webvtt/parser.rb', line 107

def start
  @start
end

#styleObject

Returns the value of attribute style.



107
108
109
# File 'lib/webvtt/parser.rb', line 107

def style
  @style
end

#textObject

Returns the value of attribute text.



107
108
109
# File 'lib/webvtt/parser.rb', line 107

def text
  @text
end

Class Method Details

.parse(cue) ⇒ Object



114
115
116
117
118
# File 'lib/webvtt/parser.rb', line 114

def self.parse(cue)
  cue = Cue.new(cue)
  cue.parse
  return cue
end

.timestamp_in_sec(timestamp) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/webvtt/parser.rb', line 131

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



143
144
145
# File 'lib/webvtt/parser.rb', line 143

def end_in_sec
  @end.to_f
end

#lengthObject



147
148
149
# File 'lib/webvtt/parser.rb', line 147

def length
  @end.to_f - @start.to_f
end

#offset_by(offset_secs) ⇒ Object



151
152
153
154
# File 'lib/webvtt/parser.rb', line 151

def offset_by( offset_secs )
  @start += offset_secs
  @end   += offset_secs
end

#parseObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/webvtt/parser.rb', line 156

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

  # it's a note or style section, ignore
  return if lines[0] =~ /NOTE|STYLE/

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

  if lines.empty?
    return
  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 = Timestamp.new $1
    @end = Timestamp.new $3
    @style = Hash[$5.strip.split(" ").map{|s| s.split(":").map(&:strip) }]
  else
    raise WebVTT::MalformedFile
  end


  @text = lines[1..-1].join("\n")
  @plain_text = @text.gsub(/<.+?>/, '').strip # remove style tags from text
end

#start_in_secObject



139
140
141
# File 'lib/webvtt/parser.rb', line 139

def start_in_sec
  @start.to_f
end

#to_webvttObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/webvtt/parser.rb', line 120

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