Class: ASSLine
- Inherits:
-
Object
- Object
- ASSLine
- Defined in:
- lib/vtt2ass/ASSLine.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
-
#convertTime(time) ⇒ Object
This method validates the time format and sends the matching time to be converted.
-
#convertToAssText(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.
-
#padTimeNum(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.
-
#toSubsTime(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.
15 16 17 18 19 20 |
# File 'lib/vtt2ass/ASSLine.rb', line 15 def initialize(style, time_start, time_end, text) @style = style @time_start = convertTime(time_start) @time_end = convertTime(time_end) @text = convertToAssText(text) end |
Instance Attribute Details
#style ⇒ Object (readonly)
Returns the value of attribute style.
6 7 8 |
# File 'lib/vtt2ass/ASSLine.rb', line 6 def style @style end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
6 7 8 |
# File 'lib/vtt2ass/ASSLine.rb', line 6 def text @text end |
#time_end ⇒ Object (readonly)
Returns the value of attribute time_end.
6 7 8 |
# File 'lib/vtt2ass/ASSLine.rb', line 6 def time_end @time_end end |
#time_start ⇒ Object (readonly)
Returns the value of attribute time_start.
6 7 8 |
# File 'lib/vtt2ass/ASSLine.rb', line 6 def time_start @time_start end |
Instance Method Details
#convertTime(time) ⇒ Object
This method validates the time format and sends the matching time to be converted
-
Requires
str
, a VTT formatted time string.
55 56 57 58 |
# File 'lib/vtt2ass/ASSLine.rb', line 55 def convertTime(time) mTime = time.match(/([\d:]*)\.?(\d*)/) return toSubsTime(mTime[0]) end |
#convertToAssText(text) ⇒ Object
This method replaces characters and tags to ASS compatible characters and tags.
-
Requires
text
, a string of VTT formated text as input.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vtt2ass/ASSLine.rb', line 32 def convertToAssText(text) decoder = HTMLEntities.new() text = text .gsub(/\r/, '') .gsub(/\n/, '\\N') .gsub(/\\n/, '\\N') .gsub(/\\N +/, '\\N') .gsub(/ +\\N/, '\\N') .gsub(/(\\N)+/, '\\N') .gsub(/<b[^>]*>([^<]*)<\/b>/) { |s| "{\\b1}#{$1}{\\b0}" } .gsub(/<i[^>]*>([^<]*)<\/i>/) { |s| "{\\i1}#{$1}{\\i0}" } .gsub(/<u[^>]*>([^<]*)<\/u>/) { |s| "{\\u1}#{$1}{\\u0}" } .gsub(/<c[^>]*>([^<]*)<\/c>/) { |s| $1 } .gsub(/<[^>]>/, '') .gsub(/\\N$/, '') .gsub(/ +$/, '') return decoder.decode(text) end |
#padTimeNum(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.
91 92 93 |
# File 'lib/vtt2ass/ASSLine.rb', line 91 def padTimeNum(sep, input, pad) return sep + (input.to_s).rjust(pad, '0') end |
#to_s ⇒ Object
This method assigns the object values and outputs an ASS dialogue line.
24 25 26 |
# File 'lib/vtt2ass/ASSLine.rb', line 24 def to_s return "Dialogue: 0,#{@time_start},#{@time_end},#{@style},,0,0,0,,#{@text}" end |
#toSubsTime(str) ⇒ Object
This method converts time from VTT format to the ASS format.
-
Requires
str
, a VTT formatted time string.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vtt2ass/ASSLine.rb', line 64 def toSubsTime(str) n = [] x = str.split(/[:.]/).map { |x| x.to_i } msLen = 2 hLen = 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 = ("%.2f" % sx).split('.') n.unshift(padTimeNum('.', sx[1], msLen)) sx = sx[0].to_f n.unshift(padTimeNum(':', (sx % 60).to_i, 2)) n.unshift(padTimeNum(':', (sx / 60).floor % 60, 2)) n.unshift(padTimeNum('', (sx / 3600).floor % 60, hLen)) return n.join('') end |