subtitle2utf8 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7299bced9d2870fa841eece07f08e4ac23b89fee
4
+ data.tar.gz: 5f7af2b826832afc3502c84fdde7376e72ddf904
5
+ SHA512:
6
+ metadata.gz: 541bd920730e4ec4e07860f01f253bf190093840e08f68d1c691ce3af1e87c2d78e785417d1dec687858f5a202240b19e95a71319b528f3dc18642b063ff1364
7
+ data.tar.gz: eb036b48465fb37f41610e02316ca5c60515763fdb399b48be1bf6dd4059013b533219b51340db021a2941190c35b5cbd69af5076de5796efc2ac8f8a02bf0f7
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in subtitle2utf8.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rick Yu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Subtitle2utf8
2
+
3
+ This simple gem is used to convert movie subtitle's encoding to utf-8.
4
+
5
+ ## Installation
6
+
7
+ Install it yourself as:
8
+
9
+ ~~~
10
+ $ gem install subtitle2utf8
11
+ ~~~
12
+
13
+ ## Usage
14
+
15
+ Use Subtitle2utf8 is much easy. Subtitle2utf8 now has two options:
16
+
17
+ ~~~
18
+ $ cd directory
19
+ $ s2u -f file
20
+ ~~~
21
+
22
+ It converts a file in the present directory.
23
+
24
+ ~~~
25
+ $ s2u -d directory
26
+ ~~~
27
+
28
+ It converts files in this directory except for subdirectories.
29
+
30
+ ~~~
31
+ $ s2u -d -r directory
32
+ ~~~
33
+
34
+ It converts files in this directory including subdirectories.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/cosmtrek/subtitle2utf8/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/s2u ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/subtitle2utf8'
5
+
6
+ options = {}
7
+ OptionParser.new do |opts|
8
+ opts.banner = 'Usage: s2u [-fdr] file'
9
+
10
+ opts.on('-f', '--file', 'convert this file') do
11
+ options[:file] = true
12
+ end
13
+
14
+ opts.on('-d', '--directory', 'convert files in this directory except for subdirectories') do
15
+ options[:dir] = true
16
+ end
17
+
18
+ opts.on('-r', '--recursion', 'convert files in this directory including subdirectories') do
19
+ options[:recursion] = true
20
+ end
21
+ end.parse!
22
+
23
+ if ARGV.size != 1
24
+ puts "Wrong arguments, one is enough."
25
+ exit
26
+ end
27
+
28
+ if options[:file] && options[:dir]
29
+ puts "Wrong switches..."
30
+ exit
31
+ end
32
+
33
+ include Subtitle2utf8
34
+ if options[:file]
35
+ Subtitle2utf8.convert(ARGV[0], :file)
36
+ elsif options[:dir] && options[:recursion]
37
+ Subtitle2utf8.convert(ARGV[0], :dir, true)
38
+ else
39
+ Subtitle2utf8.convert(ARGV[0], :dir, false)
40
+ end
@@ -0,0 +1,3 @@
1
+ module Subtitle2utf8
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,76 @@
1
+ require 'subtitle2utf8/version'
2
+ require 'rchardet'
3
+
4
+ module Subtitle2utf8
5
+ def self.convert(val, type, recursion = false)
6
+ val = File.expand_path val
7
+
8
+ case type
9
+ when :file
10
+ if !File.exist? val
11
+ puts "#{val} does not exist!"
12
+ exit
13
+ end
14
+
15
+ convert_file val
16
+ when :dir
17
+ if !Dir.exist? val
18
+ puts "#{val} does not exist!"
19
+ exit
20
+ end
21
+
22
+ if recursion
23
+ convert_dirs val
24
+ else
25
+ convert_dir val
26
+ end
27
+ end
28
+ end
29
+
30
+ def convert_file(origin)
31
+ valid = '.srt'
32
+
33
+ if (ext = File.extname(origin)) != valid
34
+ puts "#{origin} is not valid file!"
35
+ exit
36
+ end
37
+
38
+ name = File.basename(origin, ext)
39
+ # Ignore converted file.
40
+ return if name =~ /^*.utf-8/
41
+
42
+ dir = File.dirname origin
43
+ new_file = "#{dir}/#{name}.utf-8#{ext}"
44
+ encoding = CharDet.detect(File.new(origin).read(64))['encoding']
45
+ # GB18030 is a superset of Chinese encoding GB2312.
46
+ encoding = encoding == 'GB2312' ? 'GB18030' : encoding
47
+
48
+ begin
49
+ file_content =
50
+ # For UTF-16 encodings(UTF-16LE(UNIX platform) and UTF-16BE(WIndows platform))
51
+ # the file open mode must be binary.
52
+ if encoding == 'UTF-16LE' || encoding == 'UTF-16BE'
53
+ IO.read(origin, mode: 'rb', encoding: encoding).dup.force_encoding(encoding).encode('utf-8')
54
+ else
55
+ File.read(origin, encoding: encoding).dup.force_encoding(encoding).encode('utf-8')
56
+ end
57
+ rescue Exception => e
58
+ puts "#{origin} cannot be converted to utf-8..."
59
+ return
60
+ end
61
+
62
+ File.open(new_file, 'w') {|f| f.write file_content}
63
+ end
64
+
65
+ def convert_dir(present)
66
+ Dir.chdir(present) do
67
+ Dir['*.srt'].each { |name| convert_file name }
68
+ end
69
+ end
70
+
71
+ def convert_dirs(present)
72
+ Dir.chdir(present) do
73
+ Dir['**/*.srt'].each { |name| convert_file name }
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'subtitle2utf8/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'subtitle2utf8'
8
+ spec.version = Subtitle2utf8::VERSION
9
+ spec.authors = ['Rick Yu']
10
+ spec.email = ['cosmtrek@gmail.com']
11
+ spec.summary = %q{Convert movie subtitle's encoding to utf-8.}
12
+ spec.description = %q{This simple gem is used to convert movie subtitle's encoding to utf-8.}
13
+ spec.homepage = 'https://github.com/cosmtrek/subtitle2utf8'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_dependency 'rchardet', '~> 1.5.0'
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subtitle2utf8
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rick Yu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rchardet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.5.0
55
+ description: This simple gem is used to convert movie subtitle's encoding to utf-8.
56
+ email:
57
+ - cosmtrek@gmail.com
58
+ executables:
59
+ - s2u
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/s2u
69
+ - lib/subtitle2utf8.rb
70
+ - lib/subtitle2utf8/version.rb
71
+ - subtitle2utf8.gemspec
72
+ homepage: https://github.com/cosmtrek/subtitle2utf8
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Convert movie subtitle's encoding to utf-8.
96
+ test_files: []