class EncoderTools::Subtitles::Parser

Public Class Methods

new(input) click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 9
def initialize(input)
  @scanner = StringScanner.new(Util::TextReader.read(input))
  @last_index = 0
end

Public Instance Methods

parse() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 14
def parse
  result = []
  result << scan_subtitle until @scanner.eos?
  return result
end

Protected Instance Methods

line() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 79
def line
  @scanner.scan_until(/\r?\n/)
end
newline() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 75
def newline
  scan /\r?\n/, 'newline'
end
scan(scannable, name=nil) click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 83
def scan(scannable, name=nil)
  if scanned = @scanner.scan(scannable)
    return scanned
  else
    raise ParseError, "expected #{name || scannable.inspect} at character #{@scanner.pos}, got #{@scanner.string[@scanner.pos, 10].inspect}"
  end
end
scan_index() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 28
def scan_index
  # 1\n
  index = string @last_index += 1
  newline
  return index
end
scan_subtitle() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 21
def scan_subtitle
  index = scan_index
  range = scan_timestamp_range
  text  = scan_text
  return Subtitle.new(range, text)
end
scan_text() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 44
def scan_text
  # No wonder you can't do it... you acquiesce\n
  # to defeat... before you even begin.\n
  # \n
  text = ''
  loop do
    l = line
    break if l.nil? || l.strip.empty?
    text << l
  end
  text.strip!

  return text
end
scan_timestamp_range() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 35
def scan_timestamp_range
  # 01:15:18,000 --> 01:15:20,300\n
  rstart = timestamp
  string ' --> '
  rend = timestamp
  newline
  return rstart..rend
end
string(str) click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 59
def string(str)
  scan /#{Regexp.escape(str.to_s)}/
end
timestamp() click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 63
def timestamp
  hours = scan /\d\d/, 'hours'
  string ':'
  minutes = scan /\d\d/, 'minutes'
  string ':'
  seconds = scan /\d\d/, 'seconds'
  string ','
  millis = scan /\d\d\d/, 'milliseconds'

  return hours.to_i * 3600 + minutes.to_i * 60 + seconds.to_i + (BigDecimal(millis) / 1000)
end
upto(scannable) click to toggle source
# File lib/encoder-tools/subtitles/parser.rb, line 91
def upto(scannable)
  @scanner.scan_until(scannable)
end