Class: WebVTT::Cue
- Inherits:
-
Object
- Object
- WebVTT::Cue
- Defined in:
- lib/webvtt/parser.rb
Instance Attribute Summary collapse
-
#end ⇒ Object
Returns the value of attribute end.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#start ⇒ Object
Returns the value of attribute start.
-
#style ⇒ Object
Returns the value of attribute style.
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
Instance Method Summary collapse
- #end_in_sec ⇒ Object
-
#initialize(cue = nil) ⇒ Cue
constructor
A new instance of Cue.
- #length ⇒ Object
- #offset_by(offset_secs) ⇒ Object
- #parse ⇒ Object
- #start_in_sec ⇒ Object
- #to_webvtt ⇒ Object
Constructor Details
#initialize(cue = nil) ⇒ Cue
Returns a new instance of Cue.
87 88 89 90 |
# File 'lib/webvtt/parser.rb', line 87 def initialize(cue = nil) @content = cue @style = {} end |
Instance Attribute Details
#end ⇒ Object
Returns the value of attribute end.
85 86 87 |
# File 'lib/webvtt/parser.rb', line 85 def end @end end |
#identifier ⇒ Object
Returns the value of attribute identifier.
85 86 87 |
# File 'lib/webvtt/parser.rb', line 85 def identifier @identifier end |
#start ⇒ Object
Returns the value of attribute start.
85 86 87 |
# File 'lib/webvtt/parser.rb', line 85 def start @start end |
#style ⇒ Object
Returns the value of attribute style.
85 86 87 |
# File 'lib/webvtt/parser.rb', line 85 def style @style end |
#text ⇒ Object
Returns the value of attribute text.
85 86 87 |
# File 'lib/webvtt/parser.rb', line 85 def text @text end |
Class Method Details
.parse(cue) ⇒ Object
92 93 94 95 96 |
# File 'lib/webvtt/parser.rb', line 92 def self.parse(cue) cue = Cue.new(cue) cue.parse return cue end |
.timestamp_in_sec(timestamp) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/webvtt/parser.rb', line 109 def self.() mres = .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_sec ⇒ Object
121 122 123 |
# File 'lib/webvtt/parser.rb', line 121 def end_in_sec @end.to_f end |
#length ⇒ Object
125 126 127 |
# File 'lib/webvtt/parser.rb', line 125 def length @end.to_f - @start.to_f end |
#offset_by(offset_secs) ⇒ Object
129 130 131 132 |
# File 'lib/webvtt/parser.rb', line 129 def offset_by( offset_secs ) @start += offset_secs @end += offset_secs end |
#parse ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/webvtt/parser.rb', line 134 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.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 $2 @style = Hash[$3.strip.split(" ").map{|s| s.split(":").map(&:strip) }] end @text = lines[1..-1].join("\n") end |
#start_in_sec ⇒ Object
117 118 119 |
# File 'lib/webvtt/parser.rb', line 117 def start_in_sec @start.to_f end |
#to_webvtt ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/webvtt/parser.rb', line 98 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 |