Class: WebVTT::Cue
- Inherits:
-
Object
- Object
- WebVTT::Cue
- Defined in:
- lib/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) ⇒ Cue
constructor
A new instance of Cue.
- #length ⇒ Object
- #parse ⇒ Object
- #start_in_sec ⇒ Object
- #to_webvtt ⇒ Object
Constructor Details
#initialize(cue) ⇒ Cue
Returns a new instance of Cue.
88 89 90 91 |
# File 'lib/parser.rb', line 88 def initialize(cue) @content = cue parse end |
Instance Attribute Details
#end ⇒ Object
Returns the value of attribute end.
86 87 88 |
# File 'lib/parser.rb', line 86 def end @end end |
#identifier ⇒ Object
Returns the value of attribute identifier.
86 87 88 |
# File 'lib/parser.rb', line 86 def identifier @identifier end |
#start ⇒ Object
Returns the value of attribute start.
86 87 88 |
# File 'lib/parser.rb', line 86 def start @start end |
#style ⇒ Object
Returns the value of attribute style.
86 87 88 |
# File 'lib/parser.rb', line 86 def style @style end |
#text ⇒ Object
Returns the value of attribute text.
86 87 88 |
# File 'lib/parser.rb', line 86 def text @text end |
Class Method Details
.timestamp_in_sec(timestamp) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/parser.rb', line 104 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
116 117 118 |
# File 'lib/parser.rb', line 116 def end_in_sec Cue.(@end) end |
#length ⇒ Object
120 121 122 |
# File 'lib/parser.rb', line 120 def length end_in_sec - start_in_sec end |
#parse ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/parser.rb', line 124 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_sec ⇒ Object
112 113 114 |
# File 'lib/parser.rb', line 112 def start_in_sec Cue.(@start) end |
#to_webvtt ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/parser.rb', line 93 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 |