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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/subber/formatter/vtt.rb +6 -6
- data/lib/subber/parser/vtt.rb +33 -28
- data/lib/subber/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c082a74b89529051c409e05338515568c17dc26
|
4
|
+
data.tar.gz: aa6dc20d6082c75894abb08261e55baca483e708
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e83d6e85c03d1cc6f4e9fa3e980d034361495d05134f93a48836ead18b882b55e9e225800fdde5745b7f446871f6d2ef6774100a0badf5fb30d41ec0ed444f3b
|
7
|
+
data.tar.gz: 7c3d78acb225b3d14ec74c487c1a0e150a73e7f799a8cfa08c48d40770dee57142d05d8af39d936ba590c8e029d2c91118250b71dd2273661c3cc0846a70e26f
|
data/Gemfile.lock
CHANGED
data/lib/subber/formatter/vtt.rb
CHANGED
@@ -8,12 +8,12 @@ module Subber::Formatter
|
|
8
8
|
# @return [String]
|
9
9
|
#
|
10
10
|
def format(subtitles)
|
11
|
-
|
11
|
+
cues =
|
12
12
|
subtitles.map do |subtitle|
|
13
|
-
|
13
|
+
convert_subtitle_to_cue(subtitle)
|
14
14
|
end
|
15
15
|
|
16
|
-
file_content =
|
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
|
42
|
-
|
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
|
-
"#{
|
48
|
+
"#{cue}\n\n"
|
49
49
|
end
|
50
50
|
|
51
51
|
# @param subtitle [Subber::Subtitle]
|
data/lib/subber/parser/vtt.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
module Subber::Parser
|
2
2
|
class Vtt < Base
|
3
|
-
SUBTITLE_REGEX = /(
|
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 = /(
|
6
|
-
TIMECODE_REGEX = /(
|
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
|
-
|
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
|
-
|
19
|
+
cues = extract_cues(file_content)
|
20
20
|
|
21
|
-
|
22
|
-
|
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
|
33
|
-
file_content.
|
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
|
37
|
-
# @
|
38
|
+
# @param cue [String]
|
39
|
+
# @param index [Integer]
|
40
|
+
# @return [Array<Subber::Subtitle>]
|
38
41
|
#
|
39
|
-
def
|
40
|
-
|
41
|
-
|
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
|
-
|
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
|
-
|
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,
|
60
|
+
raise(Subber::Errors::InvalidCounter, cue)
|
63
61
|
rescue Subber::Errors::InvalidTimeRange
|
64
|
-
raise(Subber::Errors::InvalidTimeRange,
|
62
|
+
raise(Subber::Errors::InvalidTimeRange, cue)
|
65
63
|
rescue Subber::Errors::InvalidTimestamp
|
66
|
-
raise(Subber::Errors::InvalidTimestamp,
|
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]
|
data/lib/subber/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|