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.



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

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")
        if not paragraph.eql? "" then
            vtt_line = VTTLine.new(paragraph, width, height)
            if vtt_line.style.eql? 'Main' and
                not vtt_line.params.to_s.empty? and
                (not vtt_line.params.to_s.eql? 'align:middle' and
                not vtt_line.params.to_s.eql? 'align:center') then
                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.



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

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.



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

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.



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

def to_s
    return "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.



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

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