Class: SubviewerReader

Inherits:
Object
  • Object
show all
Includes:
Patterns
Defined in:
lib/subtitle-library/reader.rb

Constant Summary

Constants included from Patterns

Patterns::MICRO_DVD_LINE, Patterns::SUBVIEWER_LINE, Patterns::SUBVIEWER_METADATA, Patterns::SUB_RIP_LINE, Patterns::SUB_RIP_TIMING

Instance Method Summary collapse

Constructor Details

#initialize(subs_path) ⇒ SubviewerReader

Returns a new instance of SubviewerReader.



267
268
269
270
# File 'lib/subtitle-library/reader.rb', line 267

def initialize(subs_path)
  @subs_path = subs_path
  @fps = 23.976
end

Instance Method Details

#check_timing(start_time, end_time, last_end_time, error_log, check_syntax, actual_lines) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/subtitle-library/reader.rb', line 342

def check_timing(start_time, end_time, last_end_time, error_log, check_syntax, actual_lines)
  if start_time.year + start_time.month + start_time.day +
    end_time.year + end_time.month + end_time.day != 6 or
      start_time >= end_time or start_time < last_end_time
        if check_syntax
          error_log += "Invalid timing at line #{actual_lines}.\n"
        else
          puts "Invalid timing at line #{actual_lines}.\n"
        end
        return [false, error_log]
  end
  [true, error_log]
end

#parse_timing(line) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/subtitle-library/reader.rb', line 332

def parse_timing(line)
  start_end = line.split ','
  time_args = [1,1,1] + start_end[0].split(/\.|:/).collect(&:to_i)
  time_args[6] *= 1000
  start_time = Time.mktime *time_args
  time_args = [1,1,1] + start_end[1].split(/\.|:/).collect(&:to_i)
  time_args[6] *= 1000
  [start_time, Time.mktime(*time_args)]
end

#read_metadata(subs, check_syntax) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/subtitle-library/reader.rb', line 314

def (subs, check_syntax)
  actual_lines = 0
  error_log = ''
   = ''
  while line = subs.gets
    actual_lines += 1
    strip_line = line.strip
    if strip_line != ''
      if /\A\d/ =~ strip_line
        error_log += "Syntax error in metadata.\n" if check_syntax and not SUBVIEWER_METADATA =~ 
        break
      end
       += strip_line
    end
  end
  [actual_lines, error_log, line]
end

#read_subs(check_syntax) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/subtitle-library/reader.rb', line 272

def read_subs(check_syntax)
  cues = []
  error_log = ''
  last_end_time = Time.mktime 1, 1, 1
  File.open(@subs_path, 'r') do |subs|
    actual_lines, error_log, line =  subs, check_syntax 
    while line
      strip_line = line.strip
      if strip_line != ''
        if SUBVIEWER_LINE =~ strip_line
          start_time, end_time = parse_timing strip_line
          valid_timing, error_log = check_timing start_time, end_time, last_end_time, error_log, check_syntax, actual_lines
          unless valid_timing
            break unless subs.gets
            line = subs.gets
            actual_lines += 2
            next
          end
          line = subs.gets
          break unless line
          actual_lines += 1
          cues << Cue.new(start_time, end_time, line.strip.gsub('[br]', "\n")) unless check_syntax
          last_end_time = end_time
        else
          error_log += "Syntax error at line #{actual_lines}.\n" if check_syntax
          break unless subs.gets
          line = subs.gets
          actual_lines += 2
          next
        end
      end
      line = subs.gets
      actual_lines += 1
    end
  end
  if check_syntax
    error_log == '' ? 'No errors were found.' : error_log.rstrip
  else
    [cues, @fps]
  end
end