subber 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad658615874be25c00f5f43e4a2900cf87e6d281
4
- data.tar.gz: 1da66b5b728cfa6f1e5f763cda3145e599fd5c5f
3
+ metadata.gz: 9c082a74b89529051c409e05338515568c17dc26
4
+ data.tar.gz: aa6dc20d6082c75894abb08261e55baca483e708
5
5
  SHA512:
6
- metadata.gz: 815c21c2147784312e22aeb391653e00e0de96daef0f070d9b3859f755b9673dcd9cbb8b1c037e3ced42eaa6e4e19bda6f380fc328e7f7d509d2f3d4df17ffb9
7
- data.tar.gz: 29567db3a1f1d948215976250b1f19f714f8b7d7142ef25a28dea36e6f4a04077d608b5117bb73791b9c1a59de0ab5c836aeb0b273006c2a8c0b68e07eecebee
6
+ metadata.gz: e83d6e85c03d1cc6f4e9fa3e980d034361495d05134f93a48836ead18b882b55e9e225800fdde5745b7f446871f6d2ef6774100a0badf5fb30d41ec0ed444f3b
7
+ data.tar.gz: 7c3d78acb225b3d14ec74c487c1a0e150a73e7f799a8cfa08c48d40770dee57142d05d8af39d936ba590c8e029d2c91118250b71dd2273661c3cc0846a70e26f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- subber (0.1.2)
4
+ subber (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -8,12 +8,12 @@ module Subber::Formatter
8
8
  # @return [String]
9
9
  #
10
10
  def format(subtitles)
11
- subtitle_texts =
11
+ cues =
12
12
  subtitles.map do |subtitle|
13
- convert_subtitle_to_text(subtitle)
13
+ convert_subtitle_to_cue(subtitle)
14
14
  end
15
15
 
16
- file_content = subtitle_texts.join
16
+ file_content = cues.join
17
17
  file_content = add_webvtt_header(file_content)
18
18
  file_content = add_window_line_break(file_content)
19
19
  file_content
@@ -38,14 +38,14 @@ module Subber::Formatter
38
38
  # @param subtitle [Subber::Subtitle]
39
39
  # @return [String]
40
40
  #
41
- def convert_subtitle_to_text(subtitle)
42
- subtitle_text = [
41
+ def convert_subtitle_to_cue(subtitle)
42
+ cue = [
43
43
  build_counter(subtitle),
44
44
  build_time_range(subtitle),
45
45
  build_content(subtitle),
46
46
  ].join("\n")
47
47
 
48
- "#{subtitle_text}\n\n"
48
+ "#{cue}\n\n"
49
49
  end
50
50
 
51
51
  # @param subtitle [Subber::Subtitle]
@@ -1,14 +1,14 @@
1
1
  module Subber::Parser
2
2
  class Vtt < Base
3
- SUBTITLE_REGEX = /([^\n]*)\n([^\n]*)(\n(.*))?/m
3
+ SUBTITLE_REGEX = /(\d*)\n?(^\d{0,2}:?\d{2}:\d{2}\.\d{3}\s-->\s\d{0,2}:?\d{2}:\d{2}\.\d{3}$)\n?(.*)/m
4
4
  COUNTER_REGEX = /\d+/
5
- TIME_RANGE_REGEX = /(\d{2}:\d{2}:\d{2}\.\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2}\.\d{3})/
6
- TIMECODE_REGEX = /(\d{2}):(\d{2}):(\d{2})\.(\d{3})/
5
+ TIME_RANGE_REGEX = /(^\d{0,2}:?\d{2}:\d{2}\.\d{3})\s-->\s(\d{0,2}:?\d{2}:\d{2}\.\d{3}$)/
6
+ TIMECODE_REGEX = /(^\d{0,2}):?(\d{2}):(\d{2})\.(\d{3})/
7
7
 
8
- DELIMITER_REGEX = /\n?\n\n/
8
+ CUE_DELIMITER_REGEX = /\n\n/
9
9
  WINDOW_LINE_BREAK_REGEX = /\r/
10
- WEBVTT_HEADER_REGEX = /WEBVTT\n\n/
11
10
  BYTE_ORDER_MARK_STRING = "\xEF\xBB\xBF"
11
+ INVALID_CUE_START_STRINGS = %w(WEBVTT NOTE STYLE REGION)
12
12
 
13
13
  class << self
14
14
  # @param file_content [String]
@@ -16,38 +16,36 @@ module Subber::Parser
16
16
  #
17
17
  def parse(file_content)
18
18
  file_content = remove_window_line_break(file_content)
19
- file_content = remove_webvtt_header(file_content)
19
+ cues = extract_cues(file_content)
20
20
 
21
- subtitle_texts = file_content.split(DELIMITER_REGEX)
22
- subtitle_texts.map do |subtitle_text|
23
- convert_text_to_subtitle(subtitle_text)
21
+ cues.map.with_index do |cue, index|
22
+ convert_cue_to_subtitle(cue, index)
24
23
  end
25
24
  end
26
25
 
27
26
  private
28
27
 
29
28
  # @param file_content [String]
30
- # @return [String]
29
+ # @return [Array<String>]
31
30
  #
32
- def remove_webvtt_header(file_content)
33
- file_content.sub(WEBVTT_HEADER_REGEX, '')
31
+ def extract_cues(file_content)
32
+ cues = file_content.split(CUE_DELIMITER_REGEX)
33
+ cues.reject do |cue|
34
+ cue.start_with?(*INVALID_CUE_START_STRINGS)
35
+ end
34
36
  end
35
37
 
36
- # @param file_content [String]
37
- # @return [String]
38
+ # @param cue [String]
39
+ # @param index [Integer]
40
+ # @return [Array<Subber::Subtitle>]
38
41
  #
39
- def remove_window_line_break(file_content)
40
- file_content.gsub(WINDOW_LINE_BREAK_REGEX, '')
41
- end
42
+ def convert_cue_to_subtitle(cue, index)
43
+ matches = cue.match(SUBTITLE_REGEX).to_a
44
+ raise(Subber::Errors::InvalidVttFormat, cue) if matches.empty?
42
45
 
43
- # @param subtitle_text [String]
44
- # @return [Subber::Subtitle]
45
- #
46
- def convert_text_to_subtitle(subtitle_text)
47
- matches = subtitle_text.match(SUBTITLE_REGEX).to_a
48
- raise(Subber::Errors::InvalidSrtFormat, subtitle_text) if matches.empty?
46
+ _cue, counter, time_range_string, content = matches
49
47
 
50
- _subtitle_text, counter, time_range_string, _new_line, content = matches
48
+ counter = (index + 1).to_s if counter.empty?
51
49
 
52
50
  counter = extract_counter(counter)
53
51
  from, to = extract_time_range(time_range_string)
@@ -56,14 +54,21 @@ module Subber::Parser
56
54
  counter: counter,
57
55
  start_time: convert_time_to_ms(from),
58
56
  end_time: convert_time_to_ms(to),
59
- content: content
57
+ content: content.strip
60
58
  )
61
59
  rescue Subber::Errors::InvalidCounter
62
- raise(Subber::Errors::InvalidCounter, subtitle_text)
60
+ raise(Subber::Errors::InvalidCounter, cue)
63
61
  rescue Subber::Errors::InvalidTimeRange
64
- raise(Subber::Errors::InvalidTimeRange, subtitle_text)
62
+ raise(Subber::Errors::InvalidTimeRange, cue)
65
63
  rescue Subber::Errors::InvalidTimestamp
66
- raise(Subber::Errors::InvalidTimestamp, subtitle_text)
64
+ raise(Subber::Errors::InvalidTimestamp, cue)
65
+ end
66
+
67
+ # @param file_content [String]
68
+ # @return [String]
69
+ #
70
+ def remove_window_line_break(file_content)
71
+ file_content.gsub(WINDOW_LINE_BREAK_REGEX, '')
67
72
  end
68
73
 
69
74
  # @param counter_string [String]
@@ -1,3 +1,3 @@
1
1
  module Subber
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-04 00:00:00.000000000 Z
11
+ date: 2018-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler