subshift 0.0.1.pre → 0.0.2
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/README.md +69 -27
- data/bin/subshift +1 -1
- data/lib/extensions/file.rb +27 -0
- data/lib/extensions/module.rb +15 -0
- data/lib/subshift.rb +8 -5
- data/lib/subshift/runner.rb +17 -5
- data/lib/subshift/time.rb +39 -0
- data/lib/subshift/version.rb +1 -1
- data/test/test_file.rb +2 -2
- data/test/test_runner.rb +36 -4
- data/test/test_time.rb +18 -0
- metadata +9 -10
- data/lib/patches/encoding.rb +0 -1
- data/lib/patches/file.rb +0 -20
- data/lib/patches/module.rb +0 -9
- data/lib/patches/string.rb +0 -16
- data/test/test_string.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12a63eff2c966223e00b6dad281bcf19c969a9ff
|
4
|
+
data.tar.gz: 66ed320752ad51fa811664fdfd6dcbda7c3b2093
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 536e92b7bf5beebfb618caafdd65a067bf78b0be5585f5cd24aaf8c7268e5cce96e1abde150cf954159bb3794d9c17385db76500aa5f35c7ece0cab556d9f1dc
|
7
|
+
data.tar.gz: 47c8dba8776adf289d120125123f319569088003b3357ddbc43abd80cd1b5c1a3496fef73ffa5d679663f3742d714eeaf821c7c4f7484ba1667bffb79b542c25
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ Here are some extracts from it:
|
|
37
37
|
|
38
38
|
require 'subshift'
|
39
39
|
|
40
|
-
Subshift::Runner.run
|
40
|
+
Subshift::Runner.run(ARGV)
|
41
41
|
```
|
42
42
|
|
43
43
|
```ruby
|
@@ -45,30 +45,65 @@ module Subshift
|
|
45
45
|
class Runner
|
46
46
|
# ...
|
47
47
|
|
48
|
-
def run
|
48
|
+
def run
|
49
49
|
File.copylines(source, destination) do |line|
|
50
|
-
|
50
|
+
shift_times(line) if timeline?(line)
|
51
51
|
end
|
52
52
|
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def shift_times(line)
|
57
|
+
line.gsub(Time::FORMAT) do |time|
|
58
|
+
Time.parse(time) + delay
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def timeline?(line)
|
63
|
+
/-->/ === line
|
64
|
+
end
|
53
65
|
end
|
54
66
|
end
|
55
67
|
```
|
56
68
|
|
57
69
|
```ruby
|
58
|
-
require '
|
70
|
+
require 'singleton'
|
71
|
+
|
72
|
+
module Subshift
|
73
|
+
class Time
|
74
|
+
include Singleton
|
75
|
+
|
76
|
+
FORMAT = /\d{2,}:\d{2}:\d{2},\d{3}/
|
77
|
+
|
78
|
+
attr_accessor :total_ms
|
79
|
+
|
80
|
+
def self.parse(string)
|
81
|
+
h, m, s, ms = string.split(/:|,/).map(&:to_i)
|
59
82
|
|
60
|
-
|
61
|
-
|
83
|
+
instance.total_ms = \
|
84
|
+
h * 60 * 60 * 1000 +
|
85
|
+
m * 60 * 1000 +
|
86
|
+
s * 1000 +
|
87
|
+
ms
|
62
88
|
|
63
|
-
|
64
|
-
gsub(TIME) do |time|
|
65
|
-
new_time = Time.parse(time) + delay
|
66
|
-
new_time.strftime '%H:%M:%S,%3N'
|
89
|
+
instance
|
67
90
|
end
|
68
|
-
end
|
69
91
|
|
70
|
-
|
71
|
-
|
92
|
+
def +(seconds)
|
93
|
+
tap do |t|
|
94
|
+
t.total_ms += seconds * 1000
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def to_s
|
101
|
+
h, ms = total_ms.divmod(60 * 60 * 1000)
|
102
|
+
m, ms = ms.divmod(60 * 1000)
|
103
|
+
s, ms = ms.divmod(1000)
|
104
|
+
|
105
|
+
format '%02d:%02d:%02d,%03d', h, m, s, ms
|
106
|
+
end
|
72
107
|
end
|
73
108
|
end
|
74
109
|
```
|
@@ -76,24 +111,31 @@ end
|
|
76
111
|
```ruby
|
77
112
|
require 'tempfile'
|
78
113
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
114
|
+
module Extensions
|
115
|
+
module File
|
116
|
+
def copylines(src, dst)
|
117
|
+
tempfile = Tempfile.new(dst)
|
118
|
+
|
119
|
+
begin
|
120
|
+
open(src).each_line do |line|
|
121
|
+
if block_given?
|
122
|
+
new_line = yield(line)
|
123
|
+
line = new_line unless new_line.nil?
|
124
|
+
end
|
125
|
+
|
126
|
+
tempfile.write line
|
127
|
+
end
|
128
|
+
|
129
|
+
FileUtils.chmod(stat(src).mode, tempfile.path)
|
130
|
+
FileUtils.move(tempfile.path, dst)
|
131
|
+
ensure
|
132
|
+
tempfile.close!
|
88
133
|
end
|
89
|
-
|
90
|
-
FileUtils.chmod(stat(src).mode, tempfile.path)
|
91
|
-
FileUtils.move(tempfile.path, dst)
|
92
|
-
ensure
|
93
|
-
tempfile.close!
|
94
134
|
end
|
95
135
|
end
|
96
136
|
end
|
137
|
+
|
138
|
+
File.singleton_class.prepend Extensions::File
|
97
139
|
```
|
98
140
|
|
99
141
|
## Contributing
|
data/bin/subshift
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module Extensions
|
4
|
+
module File
|
5
|
+
def copylines(src, dst)
|
6
|
+
tempfile = Tempfile.new(dst)
|
7
|
+
|
8
|
+
begin
|
9
|
+
open(src).each_line do |line|
|
10
|
+
if block_given?
|
11
|
+
new_line = yield(line)
|
12
|
+
line = new_line unless new_line.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
tempfile.write line
|
16
|
+
end
|
17
|
+
|
18
|
+
FileUtils.chmod(stat(src).mode, tempfile.path)
|
19
|
+
FileUtils.move(tempfile.path, dst)
|
20
|
+
ensure
|
21
|
+
tempfile.close!
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
File.singleton_class.prepend Extensions::File
|
data/lib/subshift.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require 'patches/file'
|
4
|
-
require 'patches/string'
|
1
|
+
require 'extensions/module'
|
2
|
+
require 'extensions/file'
|
5
3
|
|
6
|
-
require 'subshift/version'
|
7
4
|
require 'subshift/runner'
|
5
|
+
require 'subshift/time'
|
6
|
+
require 'subshift/version'
|
8
7
|
require 'subshift/options'
|
8
|
+
|
9
|
+
module Subshift
|
10
|
+
Encoding.default_external = 'ISO-8859-1'
|
11
|
+
end
|
data/lib/subshift/runner.rb
CHANGED
@@ -4,18 +4,30 @@ module Subshift
|
|
4
4
|
|
5
5
|
delegate :source, :destination, :delay, to: :options
|
6
6
|
|
7
|
-
def self.run
|
8
|
-
new(argv).run
|
7
|
+
def self.run(argv)
|
8
|
+
new(argv).run
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(argv)
|
12
|
-
@options =
|
12
|
+
@options = Options.new(argv)
|
13
13
|
end
|
14
14
|
|
15
|
-
def run
|
15
|
+
def run
|
16
16
|
File.copylines(source, destination) do |line|
|
17
|
-
|
17
|
+
shift_times(line) if timeline?(line)
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def shift_times(line)
|
24
|
+
line.gsub(Time::FORMAT) do |time|
|
25
|
+
Time.parse(time) + delay
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def timeline?(line)
|
30
|
+
/-->/ === line
|
31
|
+
end
|
20
32
|
end
|
21
33
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Subshift
|
4
|
+
class Time
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
FORMAT = /\d{2,}:\d{2}:\d{2},\d{3}/
|
8
|
+
|
9
|
+
attr_accessor :total_ms
|
10
|
+
|
11
|
+
def self.parse(string)
|
12
|
+
h, m, s, ms = string.split(/:|,/).map(&:to_i)
|
13
|
+
|
14
|
+
instance.total_ms = \
|
15
|
+
h * 60 * 60 * 1000 +
|
16
|
+
m * 60 * 1000 +
|
17
|
+
s * 1000 +
|
18
|
+
ms
|
19
|
+
|
20
|
+
instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def +(seconds)
|
24
|
+
tap do |t|
|
25
|
+
t.total_ms += seconds * 1000
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
h, ms = total_ms.divmod(60 * 60 * 1000)
|
33
|
+
m, ms = ms.divmod(60 * 1000)
|
34
|
+
s, ms = ms.divmod(1000)
|
35
|
+
|
36
|
+
format '%02d:%02d:%02d,%03d', h, m, s, ms
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/subshift/version.rb
CHANGED
data/test/test_file.rb
CHANGED
@@ -19,7 +19,7 @@ class TestFile < Minitest::Test
|
|
19
19
|
|
20
20
|
def test_copylines
|
21
21
|
File.copylines 'file', 'copy' do |line|
|
22
|
-
|
22
|
+
"First line\n" if /1/ === line
|
23
23
|
end
|
24
24
|
|
25
25
|
assert_equal "First line\n2\n3\n", File.read('copy')
|
@@ -27,7 +27,7 @@ class TestFile < Minitest::Test
|
|
27
27
|
|
28
28
|
def test_copylines_on_the_same_file
|
29
29
|
File.copylines 'file', 'file' do |line|
|
30
|
-
|
30
|
+
"First line\n" if /1/ === line
|
31
31
|
end
|
32
32
|
|
33
33
|
assert_equal "First line\n2\n3\n", File.read('file')
|
data/test/test_runner.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class RunnerTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@runner = Subshift::Runner.new(['sample.srt', '0'])
|
6
|
+
end
|
7
|
+
|
4
8
|
def teardown
|
5
9
|
if File.exist?('sample_delayed.srt')
|
6
10
|
FileUtils.rm('sample_delayed.srt')
|
@@ -17,15 +21,43 @@ class RunnerTest < Minitest::Test
|
|
17
21
|
end
|
18
22
|
|
19
23
|
def test_run
|
20
|
-
regexp =
|
24
|
+
regexp = Subshift::Time::FORMAT
|
25
|
+
|
21
26
|
assert_equal '00:00:42,407', regexp.match(File.read('sample.srt'))[0]
|
22
|
-
Subshift::Runner.run
|
27
|
+
Subshift::Runner.run(['sample.srt', '3'])
|
23
28
|
assert_equal '00:00:45,407', regexp.match(File.read('sample.srt'))[0]
|
24
29
|
|
25
30
|
# Reset
|
26
|
-
|
31
|
+
Subshift::Runner.run(['sample.srt', '-3'])
|
27
32
|
|
28
|
-
|
33
|
+
assert_equal '00:00:42,407', regexp.match(File.read('sample.srt'))[0]
|
34
|
+
Subshift::Runner.run(['sample.srt', '3', '-d', 'sample_delayed.srt'])
|
29
35
|
assert_equal '00:00:45,407', regexp.match(File.read('sample_delayed.srt'))[0]
|
30
36
|
end
|
37
|
+
|
38
|
+
def test_timeline
|
39
|
+
refute @runner.send(:timeline?, '1')
|
40
|
+
assert @runner.send(:timeline?, '00:00:42,407 --> 00:00:44,932')
|
41
|
+
refute @runner.send(:timeline?, '<i>"Winner, winner, chicken dinner."</i>')
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_shift_times
|
45
|
+
set_delay(2)
|
46
|
+
assert_equal '00:00:44,407 --> 00:00:46,932', @runner.send(:shift_times, '00:00:42,407 --> 00:00:44,932')
|
47
|
+
|
48
|
+
set_delay(-2)
|
49
|
+
assert_equal '00:00:40,407 --> 00:00:42,932', @runner.send(:shift_times, '00:00:42,407 --> 00:00:44,932')
|
50
|
+
|
51
|
+
set_delay(2.5)
|
52
|
+
assert_equal '00:00:44,907 --> 00:00:47,432', @runner.send(:shift_times, '00:00:42,407 --> 00:00:44,932')
|
53
|
+
|
54
|
+
set_delay(-2.5)
|
55
|
+
assert_equal '00:00:39,907 --> 00:00:42,432', @runner.send(:shift_times, '00:00:42,407 --> 00:00:44,932')
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def set_delay(seconds)
|
61
|
+
@runner.options.instance_variable_set :@delay, seconds
|
62
|
+
end
|
31
63
|
end
|
data/test/test_time.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestTime < Minitest::Test
|
4
|
+
def test_parse
|
5
|
+
time = Subshift::Time.parse('00:00:00,000')
|
6
|
+
assert_equal 0, time.total_ms
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_plus
|
10
|
+
time = Subshift::Time.parse('00:00:00,000') + 1
|
11
|
+
assert_equal 1000, time.total_ms
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_to
|
15
|
+
time = Subshift::Time.parse('12:34:56,789')
|
16
|
+
assert_equal '12:34:56,789', time.send(:to_s)
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subshift
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- delba
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,13 +67,12 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- bin/subshift
|
70
|
-
- lib/
|
71
|
-
- lib/
|
72
|
-
- lib/patches/module.rb
|
73
|
-
- lib/patches/string.rb
|
70
|
+
- lib/extensions/file.rb
|
71
|
+
- lib/extensions/module.rb
|
74
72
|
- lib/subshift.rb
|
75
73
|
- lib/subshift/options.rb
|
76
74
|
- lib/subshift/runner.rb
|
75
|
+
- lib/subshift/time.rb
|
77
76
|
- lib/subshift/version.rb
|
78
77
|
- sample.srt
|
79
78
|
- subshift.gemspec
|
@@ -83,7 +82,7 @@ files:
|
|
83
82
|
- test/test_module.rb
|
84
83
|
- test/test_options.rb
|
85
84
|
- test/test_runner.rb
|
86
|
-
- test/
|
85
|
+
- test/test_time.rb
|
87
86
|
homepage: https://github.com/delba/subshift
|
88
87
|
licenses:
|
89
88
|
- MIT
|
@@ -99,9 +98,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
98
|
version: '2.1'
|
100
99
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
100
|
requirements:
|
102
|
-
- - "
|
101
|
+
- - ">="
|
103
102
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
103
|
+
version: '0'
|
105
104
|
requirements: []
|
106
105
|
rubyforge_project:
|
107
106
|
rubygems_version: 2.4.5
|
@@ -115,5 +114,5 @@ test_files:
|
|
115
114
|
- test/test_module.rb
|
116
115
|
- test/test_options.rb
|
117
116
|
- test/test_runner.rb
|
118
|
-
- test/
|
117
|
+
- test/test_time.rb
|
119
118
|
has_rdoc:
|
data/lib/patches/encoding.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Encoding.default_external = Encoding.find('ISO-8859-1')
|
data/lib/patches/file.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'tempfile'
|
2
|
-
|
3
|
-
class File
|
4
|
-
def self.copylines(src, dst)
|
5
|
-
tempfile = Tempfile.new(dst)
|
6
|
-
|
7
|
-
begin
|
8
|
-
readlines(src).each do |line|
|
9
|
-
line = yield(line) if block_given?
|
10
|
-
|
11
|
-
tempfile.write line
|
12
|
-
end
|
13
|
-
|
14
|
-
FileUtils.chmod(stat(src).mode, tempfile.path)
|
15
|
-
FileUtils.move(tempfile.path, dst)
|
16
|
-
ensure
|
17
|
-
tempfile.close!
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/lib/patches/module.rb
DELETED
data/lib/patches/string.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'time'
|
2
|
-
|
3
|
-
class String
|
4
|
-
TIME = /\d{2}:\d{2}:\d{2},\d{3}/
|
5
|
-
|
6
|
-
def shift_times(delay)
|
7
|
-
gsub(TIME) do |time|
|
8
|
-
new_time = Time.parse(time) + delay
|
9
|
-
new_time.strftime '%H:%M:%S,%3N'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def timeline?
|
14
|
-
/-->/ === self
|
15
|
-
end
|
16
|
-
end
|
data/test/test_string.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestString < Minitest::Test
|
4
|
-
def test_timeline
|
5
|
-
refute '1'.timeline?
|
6
|
-
assert '00:00:42,407 --> 00:00:44,932'.timeline?
|
7
|
-
refute '<i>"Winner, winner, chicken dinner."</i>'.timeline?
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_shift_times
|
11
|
-
assert_equal '00:00:44,407 --> 00:00:46,932', '00:00:42,407 --> 00:00:44,932'.shift_times(2)
|
12
|
-
assert_equal '00:00:40,407 --> 00:00:42,932', '00:00:42,407 --> 00:00:44,932'.shift_times(-2)
|
13
|
-
assert_equal '00:00:44,907 --> 00:00:47,432', '00:00:42,407 --> 00:00:44,932'.shift_times(2.5)
|
14
|
-
assert_equal '00:00:39,907 --> 00:00:42,432', '00:00:42,407 --> 00:00:44,932'.shift_times(-2.5)
|
15
|
-
end
|
16
|
-
end
|