aozora_zip 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e8680a420e373ed7d627975f6e3c9b6a46156b0dd5c7f0afe19f9ff163c6330c
4
+ data.tar.gz: e51a2fdf544cb71adc485b1a2fc0d84d8b6d97e7c0dc9b0c0846650130216114
5
+ SHA512:
6
+ metadata.gz: ecd3ab36dd0151bcfbd5b969b4534e253b0769d718a398204da15f700fb34ee65a5e9779ebceae7cf910280a91dabdebe4c2287b6637556ed549a6b8a22761a0
7
+ data.tar.gz: c7e86b2331b52dc5a4e2e5ec73e12e588678e2d511b65c096f8c589932654c7218e228ee16e38b0e0ec8c0b566be8e076971a4d9ce62247dd4ae3da3e0f2d003
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - name: Set up Ruby 2.6
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+ - name: Build and test with Rake
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ bundle exec rake
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in aozora_zip.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+ gem "test-unit", "~> 3.3"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 takahashim
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.
@@ -0,0 +1,46 @@
1
+ # AozoraZip
2
+
3
+ ![](https://github.com/takahashim/aozora_zip/workflows/Ruby/badge.svg)
4
+
5
+
6
+ AozoraZip is zip/unzip tool to use archive file (*.zip) in [aozora bunko](https://www.aozora.gr.jp/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'aozora_zip'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install aozora_zip
23
+
24
+ ## Usage
25
+
26
+ ```
27
+ $ aozora_zip zip some
28
+ $ aozora_zip zip some --file other.zip
29
+ $ aozora_zip unzip some.zip
30
+ $ aozora_zip unzip some.zip --dir other
31
+ ```
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake` to run the tests.
36
+
37
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/takahashim/aozora_zip.
42
+
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
11
+
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/aozora_zip/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "aozora_zip"
5
+ spec.version = AozoraZip::VERSION
6
+ spec.authors = ["takahashim"]
7
+ spec.email = ["maki@rubycolor.org"]
8
+
9
+ spec.summary = "tool to handle zip file archived in aozorabunko"
10
+ spec.description = "tool to handle zip file archived in aozorabunko"
11
+ spec.homepage = "https://github.com/takahashim/aozora_zip"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "thor", "~> 1.0"
27
+ spec.add_dependency "rubyzip", "~> 2.0"
28
+ spec.add_development_dependency "diff-lcs", "~> 1.3"
29
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "aozora_zip"
4
+
5
+ AozoraZip::CLI.start(ARGV)
@@ -0,0 +1,8 @@
1
+ require "aozora_zip/version"
2
+ require "aozora_zip/cli"
3
+ require "aozora_zip/core"
4
+ require "aozora_zip/text"
5
+
6
+ module AozoraZip
7
+ class Error < StandardError; end
8
+ end
@@ -0,0 +1,35 @@
1
+ require 'thor'
2
+ require 'aozora_zip/core'
3
+
4
+ module AozoraZip
5
+ class CLI < Thor
6
+ class_option :verbose, :type => :boolean
7
+ class_option :force, :type => :boolean
8
+
9
+ option :file
10
+ desc "zip DIRNAME", "zip directory DIRNAME"
11
+ def zip(dirname)
12
+ filename = options[:file] || dirname+".zip"
13
+ if options[:verbose]
14
+ puts "zip directory #{dirname} into #{filename}"
15
+ end
16
+
17
+ AozoraZip::Core.zip(filename, dirname, verbose: options[:verbose], force: options[:force])
18
+ end
19
+
20
+ option :dir, aliases: :d
21
+ desc "unzip FILENAME", "unzip file FILENAME"
22
+ def unzip(filename)
23
+ dirname = options[:dir] || File.basename(filename, ".*")
24
+ if !options[:dir] && dirname == filename
25
+ raise AozoraZip::Error, "filename #{filename} should have ext, usually `.zip`"
26
+ end
27
+
28
+ if options[:verbose]
29
+ puts "unzip #{filename} to #{dirname}"
30
+ end
31
+
32
+ AozoraZip::Core.unzip(filename, dirname, verbose: options[:verbose], force: options[:force])
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,93 @@
1
+ require 'fileutils'
2
+ require 'zip'
3
+ require 'pathname'
4
+
5
+ module AozoraZip
6
+ class Core
7
+
8
+ attr_reader :text_file, :dirname
9
+
10
+ def self.zip(filename, dirname, **kargs)
11
+ self.new(dirname).zip(filename, **kargs)
12
+ end
13
+
14
+ def self.unzip(filename, dirname, **kargs)
15
+ self.new.unzip(filename, dirname, **kargs)
16
+ end
17
+
18
+ def initialize(dirname = nil)
19
+ @dirname = dirname
20
+
21
+ if @dirname
22
+ unless File.directory?(@dirname)
23
+ raise AozoraZip::Error, "No such directory '#{dirname}'"
24
+ end
25
+
26
+ @text_file = find_text_file(@dirname)
27
+ end
28
+ end
29
+
30
+ def find_text_file(dir)
31
+ path = Pathname.new(Dir.glob("#{dir}/*.txt")[0])
32
+ path.relative_path_from(dir).to_s
33
+ end
34
+
35
+ def text
36
+ AozoraZip::Text.new(text_file, self)
37
+ end
38
+
39
+ def zip(filename, force: nil, verbose: nil)
40
+ if File.exist?(filename) && !force
41
+ raise AozoraZip::Error, "target file '#{filename}' already exists"
42
+ end
43
+
44
+ Zip::File.open(filename, Zip::File::CREATE) do |f|
45
+ Dir.chdir(dirname) do
46
+ Dir.entries('.').each do |item|
47
+ next if (item == '.' || item == '..')
48
+ next if !File.file?(item)
49
+
50
+ puts "add #{item}" if verbose
51
+
52
+ fullpath = File.join(Dir.pwd, item)
53
+ filetime = File.mtime(fullpath)
54
+ entry = Zip::Entry.new(f, item, nil, nil, nil, nil, nil, nil, Zip::DOSTime.at(filetime))
55
+ f.add(entry, fullpath)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def unzip(filename, dirname, force: nil, verbose: nil)
62
+ if !File.exist?(filename)
63
+ raise AozoraZip::Error, "No such file '#{filename}'"
64
+ end
65
+
66
+ if File.exist?(dirname) && !force
67
+ raise AozoraZip::Error, "target directory '#{dirname}' already exists"
68
+ end
69
+
70
+ @dirname = dirname
71
+
72
+ Zip::File.open(filename) do |f|
73
+ f.each do |entry|
74
+ next if (entry.directory? || entry.name.end_with?('/'))
75
+ sep = if entry.name.start_with?('/')
76
+ ''
77
+ else
78
+ '/'
79
+ end
80
+ filepath = dirname + sep + entry.name
81
+
82
+ puts "extract #{filepath}" if verbose
83
+
84
+ FileUtils.mkdir_p(File.dirname(filepath))
85
+ entry.extract(filepath)
86
+ FileUtils.touch(filepath, mtime: entry.time)
87
+ end
88
+ end
89
+
90
+ @text_file = find_text_file(@dirname)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,53 @@
1
+ module AozoraZip
2
+ class Text
3
+ CREATED_DATE_PATTERN = Regexp.new("^([0-9A-Za-z]+)年([0-9A-Za-z]+)月([0-9A-Za-z]+)日作成\r\n".encode("cp932"))
4
+ FOOTER_SEPARATOR_PATTERN = Regexp.new("\r\n\r\n底本:".encode("cp932"))
5
+
6
+ def initialize(text_file, core)
7
+ @text_file = text_file
8
+ @core = core
9
+ end
10
+
11
+ def absolute_path
12
+ File.join(@core.dirname, @text_file)
13
+ end
14
+
15
+ def content
16
+ IO.binread(absolute_path).force_encoding("cp932")
17
+ end
18
+
19
+ def update(text)
20
+ IO.binwrite(absolute_path, text)
21
+ end
22
+
23
+ def footer_pos
24
+ m = FOOTER_SEPARATOR_PATTERN.match(content)
25
+ if m
26
+ m.pre_match.size + "\r\n\r\n".size
27
+ else
28
+ nil
29
+ end
30
+ end
31
+
32
+ def create_date_string(time)
33
+ "#{time.year}年#{time.month}月#{time.day}日作成"
34
+ end
35
+
36
+ def find_created_date
37
+ m = CREATED_DATE_PATTERN.match(content, footer_pos)
38
+ if m
39
+ Time.new(m[1],m[2],m[3])
40
+ end
41
+ end
42
+
43
+ def update_created_date(time)
44
+ m = CREATED_DATE_PATTERN.match(content, footer_pos)
45
+ if m
46
+ new_date = create_date_string(time).encode("cp932")
47
+ m.pre_match + new_date + "\r\n" + m.post_match
48
+ else
49
+ nil
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module AozoraZip
2
+ VERSION = "0.6.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aozora_zip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - takahashim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: diff-lcs
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ description: tool to handle zip file archived in aozorabunko
56
+ email:
57
+ - maki@rubycolor.org
58
+ executables:
59
+ - aozora_zip
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".github/workflows/ruby.yml"
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - aozora_zip.gemspec
70
+ - exe/aozora_zip
71
+ - lib/aozora_zip.rb
72
+ - lib/aozora_zip/cli.rb
73
+ - lib/aozora_zip/core.rb
74
+ - lib/aozora_zip/text.rb
75
+ - lib/aozora_zip/version.rb
76
+ homepage: https://github.com/takahashim/aozora_zip
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/takahashim/aozora_zip
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: tool to handle zip file archived in aozorabunko
100
+ test_files: []