class Subjuster::Parser

Constants

REGEXP

Attributes

inputs[R]

Parser

Subjuster::Parser parses the File you provide via UserInput object

Example:

  inputs = Subjuster::UserInput.new(source: 'somefilename')
  Subjuster::Parser.new(inputs: inputs).parse

  # [{:id=>"1", 
  #  :start_time=>"00:00:57,918", 
  #  :end_time=>"00:01:02,514", 
  #  :dialog=>"\"In order to affect a timely halt\n" + "to deteriorating conditions\n"},..]

Public Class Methods

new(inputs:) click to toggle source
# File lib/subjuster/parser.rb, line 19
def initialize(inputs:)
  @inputs = inputs
end

Public Instance Methods

parse() click to toggle source
# File lib/subjuster/parser.rb, line 23
def parse
  inputs.validate! unless $test 
  _parse
end

Private Instance Methods

_parse() click to toggle source
# File lib/subjuster/parser.rb, line 41
def _parse
  items = []
  file_content_array = File.read(inputs.source_filepath).split("\n")
  count = file_content_array.count
  index = 0

  while index < count do
    line = file_content_array[index]

    if line =~ REGEXP
      splitted_line = file_content_array[index+1].split(' --> ')
      
      dialog, index = find_dialog_from(list: file_content_array, index: index + 2)
      
      items << { id: line, start_time: splitted_line.first, end_time: splitted_line.last, dialog: dialog }
    else
      index += 1
    end
  end

  items
end
find_dialog_from(list:, index:) click to toggle source

This will find next `dialog number` embedded line and return lines joined upto that line, along with the index of line of the `dialog num`

# File lib/subjuster/parser.rb, line 31
def find_dialog_from(list:, index:)
  buffer = []
  count = list.count
   while !(list[index]  =~ REGEXP) && index < count do
     buffer << list[index]
     index += 1
   end
   [buffer.join("\n"), index]
end