Class: VTTFile

Inherits:
Object
  • Object
show all
Defined in:
lib/vtt2ass/vtt_file.rb

Overview

This class defines a VTT subtile file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, width, height) ⇒ VTTFile

Creates a new VTTFile instance and assigns the default values of instance variables.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vtt2ass/vtt_file.rb', line 12

def initialize(file_path, width, height)
  @title = File.basename(file_path).gsub('.vtt', '')
  @lines = []
  separator = determine_line_ending(file_path) ? "\n\n" : "\r\n\r\n"
  count = 0
  style_count = 1
  File.foreach(file_path, separator) do |paragraph|
    paragraph = paragraph.rstrip.gsub(/[\r\n]/, "\n")
    unless paragraph.eql? ''
      vtt_line = VTTLine.new(paragraph, width, height)
      if vtt_line.style.eql?('Main') &&
         !vtt_line.params.to_s.empty? &&
         (!vtt_line.params.to_s.eql?('align:middle') &&
         !vtt_line.params.to_s.eql?('align:center'))
        vtt_line.style = "Style#{style_count}"
        style_count += 1
      end
      @lines.push(vtt_line)
      count += 1
    end
  end
  @lines.shift
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



8
9
10
# File 'lib/vtt2ass/vtt_file.rb', line 8

def lines
  @lines
end

Instance Method Details

#determine_line_ending(file_path) ⇒ Object

This method determines the line ending character to use as a separator.



38
39
40
41
42
# File 'lib/vtt2ass/vtt_file.rb', line 38

def determine_line_ending(file_path)
  File.open(file_path, 'r') do |file|
    return file.readline[/\r?\n$/] == "\n"
  end
end

#to_sObject

This method concatenates the object data in the right order for a string output.



55
56
57
# File 'lib/vtt2ass/vtt_file.rb', line 55

def to_s
  "WEBVTT\n\n\n#{@lines}"
end

#write_to_file(file_path) ⇒ Object

This method writes the content of the VTTFile object into a file path that is provided.



46
47
48
49
50
51
# File 'lib/vtt2ass/vtt_file.rb', line 46

def write_to_file(file_path)
  File.open(file_path, 'w') do |line|
    line.print "\ufeff"
    line.puts to_s
  end
end