srt_parser2 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0ee6f38b54b886cea2d8c1f8f2246b88712656bac3abb2be7063a33cb882c5f
4
+ data.tar.gz: 4f7d886e24f7700afea8ccc114c5be6390393501b935807d53f12d733db8a8cd
5
+ SHA512:
6
+ metadata.gz: 60571aa10abe58d7937222a4b93d077d8d06f1987a54759be9099b9da19b4696532e5a728575b862220c89404f2ff686890f581918bde4dc366b1f7c5d10b701
7
+ data.tar.gz: 1eb068b01d00769acd7ccf2dc41a9f036942ad013999f82d9573203718e9fa89f817cabfd43425cddd5740f15d78d38f74b130edbcd3d13e20a6b9aea2e12157
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-04-23
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 jiikko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # SrtParser2
2
+
3
+ https://github.com/cpetersen/srt/issues/29
4
+ 空行を含む srt をパースするための gem です。
5
+
6
+ ```
7
+ 1
8
+ 00:00:03,500 --> 00:00:12,450
9
+
10
+ [音楽]
11
+
12
+ 2
13
+ 00:00:12,450 --> 00:00:12,460
14
+
15
+
16
+
17
+ 3
18
+ 00:00:12,460 --> 00:00:13,860
19
+
20
+
21
+ ```
22
+
23
+ ```ruby
24
+ result = SrtParser2.parse(srt_body)
25
+ expect(result.lines[0]).to have_attributes(sequence: 1, text: ['[音楽]'])
26
+ expect(result.lines[1]).to have_attributes(sequence: 2, text: [])
27
+ expect(result.lines[2]).to have_attributes(sequence: 3, text: ['ん'])
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ```ruby
33
+ result = SrtParser2.parse(srt_body)
34
+ result.lines.each do |line|
35
+ puts line.text
36
+ end
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/srt_parser2.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,43 @@
1
+ module SrtParser2
2
+ class Line
3
+ attr_accessor :sequence, :error
4
+
5
+ attr_writer :start_time, :end_time
6
+
7
+ def filled?
8
+ sequence.is_a?(Integer) && start_time.is_a?(Numeric) && end_time.is_a?(Numeric) && error.nil?
9
+ end
10
+
11
+ def start_time
12
+ to_f(@start_time)
13
+ end
14
+
15
+ def end_time
16
+ to_f(@end_time)
17
+ end
18
+
19
+ def next_sequence
20
+ raise 'invalid impliment!!!!!!!' if sequence.nil?
21
+
22
+ sequence + 1
23
+ end
24
+
25
+ def text
26
+ @text ||= []
27
+ end
28
+
29
+ private
30
+
31
+ def to_f(time_str)
32
+ return nil if time_str.nil?
33
+
34
+ hours, minutes, seconds_and_millis = time_str.split(':')
35
+ seconds, millis = seconds_and_millis.split(',')
36
+
37
+ hours.to_i * 3600 +
38
+ minutes.to_i * 60 +
39
+ seconds.to_i +
40
+ (millis.to_f / 1000)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SrtParser2
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'srt_parser2/line'
4
+ require_relative 'srt_parser2/version'
5
+
6
+ module SrtParser2
7
+ STOP_WORD = 'endfile!!!!!!!!!!!!!!!!!!!!!'
8
+ class Result
9
+ def lines
10
+ @lines ||= []
11
+ end
12
+ end
13
+
14
+ # @praam srt_body [String]
15
+ def self.parse(srt_body)
16
+ parse_string(srt_body)
17
+ end
18
+
19
+ def self.parse_string(srt_data)
20
+ result = Result.new
21
+ line = Line.new
22
+
23
+ split_srt_data(srt_data).each_with_index do |str, index|
24
+ if line.error.nil?
25
+ if line.sequence.nil?
26
+ line.sequence = str.to_i
27
+ elsif line.start_time.nil?
28
+ if (mres = str.match(/(?<start_timecode>[^[[:space:]]]+) -+> (?<end_timecode>[^[[:space:]]]+) ?/))
29
+
30
+ if (line.start_time = mres['start_timecode']).nil?
31
+ line.error = "#{index}, Invalid formatting of start timecode, [#{mres['start_timecode']}]"
32
+ warn line.error if @debug
33
+ end
34
+
35
+ if (line.end_time = mres['end_timecode']).nil?
36
+ line.error = "#{index}, Invalid formatting of end timecode, [#{mres['end_timecode']}]"
37
+ warn line.error if @debug
38
+ end
39
+ else
40
+ line.error = "#{index}, Invalid Time Line formatting, [#{str}]"
41
+ warn line.error if @debug
42
+ end
43
+ elsif line.filled? && str == line.next_sequence.to_s
44
+ result.lines << line
45
+ line = Line.new
46
+ line.sequence = str.to_i
47
+ elsif str == STOP_WORD
48
+ result.lines << line
49
+ else
50
+ line.text << str.strip if str.strip.length.positive?
51
+ end
52
+ end
53
+ rescue StandardError
54
+ line.error = "#{index}, General Error, [#{str}]"
55
+ warn line.error if @debug
56
+ end
57
+ result
58
+ end
59
+
60
+ def self.split_srt_data(srt_data)
61
+ srt_data.split(/\n/) + ["\n"] + [STOP_WORD]
62
+ end
63
+ end
@@ -0,0 +1,4 @@
1
+ module SrtParser2
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: srt_parser2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jiikko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: srt parter
14
+ email:
15
+ - n905i.1214@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/srt_parser2.rb
26
+ - lib/srt_parser2/line.rb
27
+ - lib/srt_parser2/version.rb
28
+ - sig/srt_parser2.rbs
29
+ homepage: https://github.com/jiikko/srt_parser2
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/jiikko/srt_parser2
34
+ source_code_uri: https://github.com/jiikko/srt_parser2
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.4
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: srt parter
54
+ test_files: []