class Subber::Parser::Srt
Constants
- COUNTER_REGEX
- DELIMITER_REGEX
- SUBTITLE_REGEX
- TIMECODE_REGEX
- TIME_RANGE_REGEX
- WINDOW_LINE_BREAK_REGEX
Public Class Methods
parse(file_content)
click to toggle source
@param file_content [String] @return [Array<Subber::Subtitle>]
# File lib/subber/parser/srt.rb, line 15 def parse(file_content) file_content = remove_window_line_break(file_content) subtitle_texts = file_content.split(DELIMITER_REGEX) subtitle_texts.map do |subtitle_text| convert_text_to_subtitle(subtitle_text) end end
Private Class Methods
convert_text_to_subtitle(subtitle_text)
click to toggle source
@param subtitle_text [String] @return [Subber::Subtitle]
# File lib/subber/parser/srt.rb, line 36 def convert_text_to_subtitle(subtitle_text) matches = subtitle_text.match(SUBTITLE_REGEX).to_a raise(Subber::Errors::InvalidSrtFormat, subtitle_text) if matches.empty? _subtitle_text, counter, time_range_string, _new_line, content = matches counter = extract_counter(counter) from, to = extract_time_range(time_range_string) Subber::Subtitle.new( counter: counter, start_time: convert_time_to_ms(from), end_time: convert_time_to_ms(to), content: content ) rescue Subber::Errors::InvalidCounter raise(Subber::Errors::InvalidCounter, subtitle_text) rescue Subber::Errors::InvalidTimeRange raise(Subber::Errors::InvalidTimeRange, subtitle_text) rescue Subber::Errors::InvalidTimestamp raise(Subber::Errors::InvalidTimestamp, subtitle_text) end
convert_time_to_ms(time_string)
click to toggle source
@param time_string [String] @return [Integer] @raise [Subber::Errors::InvalidTimestamp]
# File lib/subber/parser/srt.rb, line 88 def convert_time_to_ms(time_string) matches = time_string.match(TIMECODE_REGEX).to_a raise(Subber::Errors::InvalidTimestamp, time_string) if matches.empty? int_matches = matches.map(&:to_i) _time_string, hours, minutes, seconds, milliseconds = int_matches ((hours * 60 + minutes) * 60 + seconds) * 1000 + milliseconds end
extract_counter(text)
click to toggle source
@param counter_string [String] @return [Integer] @raise [Subber::Errors::InvalidCounter]
# File lib/subber/parser/srt.rb, line 63 def extract_counter(text) counter_text = text.match(COUNTER_REGEX).to_a.last raise(Subber::Errors::InvalidCounter) if counter_text.nil? counter_text.to_i end
extract_time_range(text)
click to toggle source
@param text [String] @return [
[String] from [String] to
] @raise [Subber::Errors::InvalidCounter]
# File lib/subber/parser/srt.rb, line 76 def extract_time_range(text) matches = text.match(TIME_RANGE_REGEX).to_a raise(Subber::Errors::InvalidTimeRange) if matches.empty? _text, from, to = matches [from, to] end
remove_window_line_break(file_content)
click to toggle source
@param file_content [String] @return [String]
# File lib/subber/parser/srt.rb, line 29 def remove_window_line_break(file_content) file_content.gsub(WINDOW_LINE_BREAK_REGEX, '') end