Class: ASSLine
- Inherits:
-
Object
- Object
- ASSLine
- Defined in:
- lib/vtt2ass/ass_line.rb
Overview
This class defines an ASS subtile line.
Instance Attribute Summary collapse
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#time_end ⇒ Object
readonly
Returns the value of attribute time_end.
-
#time_start ⇒ Object
readonly
Returns the value of attribute time_start.
Instance Method Summary collapse
-
#convert_time(time) ⇒ Object
This method validates the time format and sends the matching time to be converted.
-
#convert_to_ass_text(text) ⇒ Object
This method replaces characters and tags to ASS compatible characters and tags.
-
#initialize(style, time_start, time_end, text) ⇒ ASSLine
constructor
This method creates an instance of an ASSLine.
-
#pad_time_num(sep, input, pad) ⇒ Object
This method pads text so that time numbers are a fixed number of digit.
-
#to_s ⇒ Object
This method assigns the object values and outputs an ASS dialogue line.
-
#to_subs_time(str) ⇒ Object
This method converts time from VTT format to the ASS format.
Constructor Details
#initialize(style, time_start, time_end, text) ⇒ ASSLine
This method creates an instance of an ASSLine.
-
Requires a
style
name as input. -
Requires
time_start
, a VTT formatted timestamp as input. -
Requires
time_start
, a VTT formatted timestamp as input. -
Requires
text
, a VTT formatted string as input.
17 18 19 20 21 22 |
# File 'lib/vtt2ass/ass_line.rb', line 17 def initialize(style, time_start, time_end, text) @style = style @time_start = convert_time(time_start) @time_end = convert_time(time_end) @text = convert_to_ass_text(text) end |
Instance Attribute Details
#style ⇒ Object (readonly)
Returns the value of attribute style.
8 9 10 |
# File 'lib/vtt2ass/ass_line.rb', line 8 def style @style end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
8 9 10 |
# File 'lib/vtt2ass/ass_line.rb', line 8 def text @text end |
#time_end ⇒ Object (readonly)
Returns the value of attribute time_end.
8 9 10 |
# File 'lib/vtt2ass/ass_line.rb', line 8 def time_end @time_end end |
#time_start ⇒ Object (readonly)
Returns the value of attribute time_start.
8 9 10 |
# File 'lib/vtt2ass/ass_line.rb', line 8 def time_start @time_start end |
Instance Method Details
#convert_time(time) ⇒ Object
This method validates the time format and sends the matching time to be converted
-
Requires
str
, a VTT formatted time string.
58 59 60 61 |
# File 'lib/vtt2ass/ass_line.rb', line 58 def convert_time(time) matched_time = time.match(/([\d:]*)\.?(\d*)/) to_subs_time(matched_time[0]) end |
#convert_to_ass_text(text) ⇒ Object
This method replaces characters and tags to ASS compatible characters and tags.
-
Requires
text
, a string of VTT formated text as input.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/vtt2ass/ass_line.rb', line 34 def convert_to_ass_text(text) decoder = HTMLEntities.new text = text .gsub(/\r/, '') .gsub(/\n/, '\\N') .gsub(/\\n/, '\\N') .gsub(/\\N +/, '\\N') .gsub(/ +\\N/, '\\N') .gsub(/(\\N)+/, '\\N') .gsub(%r{<b[^>]*>([^<]*)</b>}) { |_s| "{\\b1}#{Regexp.last_match(1)}{\\b0}" } .gsub(%r{<i[^>]*>([^<]*)</i>}) { |_s| "{\\i1}#{Regexp.last_match(1)}{\\i0}" } .gsub(%r{<u[^>]*>([^<]*)</u>}) { |_s| "{\\u1}#{Regexp.last_match(1)}{\\u0}" } .gsub(%r{<c[^>]*>([^<]*)</c>}) { |_s| Regexp.last_match(1) } .gsub(/<[^>]>/, '') .gsub(/\\N$/, '') .gsub(/ +$/, '') decoder.decode(text) end |
#pad_time_num(sep, input, pad) ⇒ Object
This method pads text so that time numbers are a fixed number of digit.
-
Requires
sep
, a string separator. -
Requires
input
, an integer. -
Requires
pad
, an integer for the number of digits to be padded.
94 95 96 |
# File 'lib/vtt2ass/ass_line.rb', line 94 def pad_time_num(sep, input, pad) sep + input.to_s.rjust(pad, '0') end |
#to_s ⇒ Object
This method assigns the object values and outputs an ASS dialogue line.
26 27 28 |
# File 'lib/vtt2ass/ass_line.rb', line 26 def to_s "Dialogue: 0,#{@time_start},#{@time_end},#{@style},,0,0,0,,#{@text}" end |
#to_subs_time(str) ⇒ Object
This method converts time from VTT format to the ASS format.
-
Requires
str
, a VTT formatted time string.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/vtt2ass/ass_line.rb', line 67 def to_subs_time(str) n = [] x = str.split(/[:.]/).map(&:to_i) ms_len = 2 h_len = 1 x[3] = "0.#{x[3].to_s.rjust(3, '0')}" sx = (x[0] * 60 * 60) + (x[1] * 60) + x[2] + x[3].to_f sx = format('%.2f', sx).split('.') n.unshift(pad_time_num('.', sx[1], ms_len)) sx = sx[0].to_f n.unshift(pad_time_num(':', (sx % 60).to_i, 2)) n.unshift(pad_time_num(':', (sx / 60).floor % 60, 2)) n.unshift(pad_time_num('', (sx / 3600).floor % 60, h_len)) n.join end |