Class: Captive::SRT
- Inherits:
-
Object
show all
- Includes:
- Base
- Defined in:
- lib/captive/formats/srt.rb
Instance Attribute Summary
Attributes included from Base
#cues
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Base
#as_json, included, #initialize, #method_missing, #respond_to_missing?, #save_as
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Captive::Base
Class Method Details
66
67
68
|
# File 'lib/captive/formats/srt.rb', line 66
def self.format_time(text)
text.strip.gsub(/,/, '.')
end
|
.parse(blob:) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/captive/formats/srt.rb', line 7
def self.parse(blob:)
cue_list = []
lines = blob.split("\n")
count = 0
state = :new_cue
cue = nil
lines.each_with_index do |line, _index|
line.strip!
case state
when :new_cue
next if line.empty?
raise InvalidSubtitle, "Invalid Cue Number at line #{count}" if /^\d+$/.match(line).nil?
state = :time
when :time
raise InvalidSubtitle, "Invalid Time Format at line #{count}" unless timecode?(line)
start_time, end_time = line.split('-->').map(&:strip)
cue = Cue.new(
start_time: format_time(start_time),
end_time: format_time(end_time)
)
state = :text
when :text
if line.empty?
cue_list << cue
cue = nil
state = :new_cue
else
cue.add_text(line)
end
end
end
cue_list << cue unless cue.nil?
cue_list
end
|
.timecode?(text) ⇒ Boolean
70
71
72
|
# File 'lib/captive/formats/srt.rb', line 70
def self.timecode?(text)
!!text.match(/^\d{2,}:\d{2}:\d{2},\d{3}.*\d{2,}:\d{2}:\d{2},\d{3}$/)
end
|
Instance Method Details
#to_s ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/captive/formats/srt.rb', line 51
def to_s
string = String.new
cues.each_with_index do |cue, index|
string << (index + 1).to_s
string << "\n"
string << milliseconds_to_timecode(cue.start_time).gsub!('.', ',')
string << ' --> '
string << milliseconds_to_timecode(cue.end_time).gsub!('.', ',')
string << "\n"
string << cue.text
string << "\n\n"
end
string
end
|