komic-cli 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/komic-cli.gemspec +1 -0
- data/lib/komic/builder/directory.rb +29 -0
- data/lib/komic/builder/zip.rb +34 -0
- data/lib/komic/builder.rb +12 -1
- data/lib/komic/cli.rb +1 -0
- data/lib/komic/generator/content.schema.json +32 -0
- data/lib/komic/generator/generator.rb +29 -7
- data/lib/komic/version.rb +2 -2
- data/spec/builder_spec.rb +12 -2
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cd975971ff6b6728935ed9b6e5f1f3f004a692c
|
4
|
+
data.tar.gz: a85bc5db65253ff21f0b28eebe5f30810acdcc73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a290cbccea8dce6d1072238758e6baf86f6eb3947f4c0aed2d3cef51709643aa2eaab094edb683fde81423f876a905bd4a046f6dafb4bc1b45ba5de913057e3c
|
7
|
+
data.tar.gz: 147b794d19b32f8be2c0e2141ce07d84f6a791dab51ce665394f6d4ad844e905d33942736e32dbf5da405b2825f8bb370232c3db44758bbdde2f703f33a11c47
|
data/komic-cli.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |gem|
|
|
28
28
|
gem.add_runtime_dependency 'json-schema', '~> 2.5'
|
29
29
|
gem.add_runtime_dependency 'ruby-progressbar', '~> 1.7'
|
30
30
|
gem.add_runtime_dependency 'rubyzip', '~> 1.1'
|
31
|
+
gem.add_runtime_dependency 'uglifier', '~> 2.7.1'
|
31
32
|
|
32
33
|
gem.add_development_dependency 'bundler', '~> 1.0'
|
33
34
|
gem.add_development_dependency 'rake', '~> 0.8'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'komic/utils'
|
3
|
+
require 'zip'
|
4
|
+
|
5
|
+
module Komic::Builder
|
6
|
+
class Directory
|
7
|
+
attr_reader :images
|
8
|
+
|
9
|
+
def initialize(type_string, options)
|
10
|
+
@options = options
|
11
|
+
@path = File.join(Dir.pwd, type_string)
|
12
|
+
end
|
13
|
+
|
14
|
+
def images
|
15
|
+
images = []
|
16
|
+
Dir.glob(File.join(@path, FNMATCH_FOR_IMAGE)).
|
17
|
+
sort_by { |x| File.basename(x).split('.')[0].to_i }.
|
18
|
+
each_with_index do |entry_path, index|
|
19
|
+
will_be_write = Tempfile.new("#{ File.basename(entry_path) }")
|
20
|
+
image = MiniMagick::Image.open(entry_path)
|
21
|
+
image.write will_be_write.path
|
22
|
+
images.push({
|
23
|
+
width: image.width, height: image.height, src: will_be_write
|
24
|
+
})
|
25
|
+
end
|
26
|
+
return images
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'komic/utils'
|
3
|
+
require 'zip'
|
4
|
+
|
5
|
+
|
6
|
+
module Komic::Builder
|
7
|
+
class Zip
|
8
|
+
attr_reader :images
|
9
|
+
|
10
|
+
def initialize(type_string, options)
|
11
|
+
@options = options
|
12
|
+
@zip_path = File.join(Dir.pwd, type_string)
|
13
|
+
end
|
14
|
+
|
15
|
+
def images
|
16
|
+
images = []
|
17
|
+
::Zip::File.open(@zip_path) do |zip_file|
|
18
|
+
zip_file.
|
19
|
+
sort_by { |x| File.basename(x.name).split('.')[0].to_i }.
|
20
|
+
select { |x| File.fnmatch?(FNMATCH_FOR_IMAGE, x.name, File::FNM_EXTGLOB) }.
|
21
|
+
each do |entry|
|
22
|
+
p entry.name
|
23
|
+
will_be_write = Tempfile.new("#{ entry.name }")
|
24
|
+
image = MiniMagick::Image.read(entry.get_input_stream.read)
|
25
|
+
image.write will_be_write.path
|
26
|
+
images.push({
|
27
|
+
width: image.width, height: image.height, src: will_be_write
|
28
|
+
})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
return images
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/komic/builder.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require 'komic/builder/pdf'
|
2
|
+
require 'komic/builder/directory'
|
3
|
+
require 'komic/builder/zip'
|
2
4
|
require 'komic/builder/douban_album'
|
3
5
|
require 'uri'
|
4
6
|
|
5
7
|
module Komic::Builder
|
8
|
+
IMAGE_SUFFIXES = ['png', 'jpg', 'jpeg', 'webp', 'bmp', 'gif']
|
9
|
+
FNMATCH_FOR_IMAGE = "**.{#{IMAGE_SUFFIXES.join(',')}}"
|
6
10
|
class Factory
|
7
11
|
class << self
|
8
12
|
def detect_type(string)
|
@@ -14,8 +18,13 @@ module Komic::Builder
|
|
14
18
|
end
|
15
19
|
|
16
20
|
if File.exists?(path)
|
17
|
-
|
21
|
+
file_extname = File.extname(path)
|
22
|
+
if file_extname == '.pdf'
|
18
23
|
return 'pdf'
|
24
|
+
elsif file_extname == '.zip'
|
25
|
+
return 'zip'
|
26
|
+
elsif File.directory?(path)
|
27
|
+
return 'directory'
|
19
28
|
end
|
20
29
|
end
|
21
30
|
raise "Builder can't be found."
|
@@ -24,6 +33,8 @@ module Komic::Builder
|
|
24
33
|
def get_builder(type_string, options)
|
25
34
|
case detect_type(type_string)
|
26
35
|
when 'pdf' then PDF.new(type_string, options)
|
36
|
+
when 'zip' then Zip.new(type_string, options)
|
37
|
+
when 'directory' then Directory.new(type_string, options)
|
27
38
|
when 'douban_album' then DoubanAlbum.new(type_string, options)
|
28
39
|
end
|
29
40
|
end
|
data/lib/komic/cli.rb
CHANGED
@@ -32,6 +32,7 @@ module Komic
|
|
32
32
|
|
33
33
|
desc "dev", '生成 dev 的数据'
|
34
34
|
option :name, default: "dev", desc: "设定文件夹名"
|
35
|
+
option :debug, default: true, desc: "开启 debug 模式"
|
35
36
|
def dev(type_string)
|
36
37
|
generator = Komic::Generator.new
|
37
38
|
pdf_builder = Builder::Factory.get_builder(type_string, options)
|
@@ -11,6 +11,10 @@
|
|
11
11
|
"id": "komic_version",
|
12
12
|
"type": "string"
|
13
13
|
},
|
14
|
+
"content_json_uuid": {
|
15
|
+
"id": "content_json_uuid",
|
16
|
+
"type": "string"
|
17
|
+
},
|
14
18
|
"name": {
|
15
19
|
"id": "name",
|
16
20
|
"type": "string"
|
@@ -51,6 +55,34 @@
|
|
51
55
|
"id": "0",
|
52
56
|
"type": "object",
|
53
57
|
"properties": {
|
58
|
+
"web": {
|
59
|
+
"id": "web",
|
60
|
+
"type": "object",
|
61
|
+
"properties": {
|
62
|
+
"src": {
|
63
|
+
"id": "src",
|
64
|
+
"type": "object",
|
65
|
+
"properties": {
|
66
|
+
"default": {
|
67
|
+
"id": "default",
|
68
|
+
"type": "string"
|
69
|
+
},
|
70
|
+
"jpg": {
|
71
|
+
"id": "jpg",
|
72
|
+
"type": "string"
|
73
|
+
}
|
74
|
+
}
|
75
|
+
},
|
76
|
+
"width": {
|
77
|
+
"id": "width",
|
78
|
+
"type": "integer"
|
79
|
+
},
|
80
|
+
"height": {
|
81
|
+
"id": "height",
|
82
|
+
"type": "integer"
|
83
|
+
}
|
84
|
+
}
|
85
|
+
},
|
54
86
|
"src": {
|
55
87
|
"id": "src",
|
56
88
|
"type": "string"
|
@@ -7,6 +7,8 @@ require 'mini_magick'
|
|
7
7
|
require 'base64'
|
8
8
|
require 'open-uri'
|
9
9
|
require 'zip'
|
10
|
+
require 'uglifier'
|
11
|
+
require 'securerandom'
|
10
12
|
|
11
13
|
require 'komic/version'
|
12
14
|
require 'komic/utils'
|
@@ -70,16 +72,17 @@ module Komic
|
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
73
|
-
def to_build
|
75
|
+
def to_build(options)
|
74
76
|
content_builder = Jbuilder.new do |json|
|
75
77
|
json.komic_cli_version Komic::VERSION
|
76
78
|
json.content_json_version Komic::CONTENT_JSON_VERSION
|
79
|
+
json.content_json_uuid SecureRandom.uuid
|
77
80
|
json.(@meta, :name, :author, :description, :thumbnails)
|
78
81
|
json.images @images
|
79
82
|
end
|
80
83
|
data = content_builder.target!
|
81
84
|
validate_json(data)
|
82
|
-
return data
|
85
|
+
return options[:debug] ? JSON.pretty_generate(JSON.parse(data)) : data
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
@@ -127,7 +130,7 @@ module Komic
|
|
127
130
|
|
128
131
|
images = data[:images]
|
129
132
|
|
130
|
-
images.map
|
133
|
+
images.map!.with_index do |image, index|
|
131
134
|
# dirty but work
|
132
135
|
if image[:src].instance_of? Tempfile
|
133
136
|
will_be_open = image[:src].path
|
@@ -137,6 +140,7 @@ module Komic
|
|
137
140
|
|
138
141
|
manager = MiniMagick::Image.open(will_be_open)
|
139
142
|
|
143
|
+
image_type = manager.type.downcase
|
140
144
|
image_path = File.join(image_dir,
|
141
145
|
[index, manager.type.downcase].join('.'))
|
142
146
|
|
@@ -144,7 +148,19 @@ module Komic
|
|
144
148
|
manager.strip() unless manager.type.downcase == 'svg'
|
145
149
|
manager.write image_path
|
146
150
|
image[:src] = image_path
|
147
|
-
|
151
|
+
|
152
|
+
manager.format 'webp'
|
153
|
+
webp_path = File.join(image_dir,
|
154
|
+
[[index, 'webp'].join('-'), manager.type.downcase].join('.'))
|
155
|
+
manager.write webp_path
|
156
|
+
|
157
|
+
src = {}
|
158
|
+
src[:default] = image_type
|
159
|
+
src[image_type] = image_path
|
160
|
+
src[:webp] = webp_path
|
161
|
+
|
162
|
+
web = Utils.deep_merge_hashes(image, { src: src })
|
163
|
+
Utils.deep_merge_hashes(image, { web: web })
|
148
164
|
end
|
149
165
|
|
150
166
|
thumbnails_builder = ThumbnailsBuilder.new(images)
|
@@ -153,7 +169,7 @@ module Komic
|
|
153
169
|
file.write thumbnails_builder.to_build
|
154
170
|
end
|
155
171
|
|
156
|
-
images.map do |image
|
172
|
+
images.map do |image|
|
157
173
|
image[:src] = Utils.get_relative_path(image[:src], root_dir)
|
158
174
|
if options[:'remote-url']
|
159
175
|
image[:src] = "https://placeimg.com/#{image[:width]}/#{image[:height]}/any"
|
@@ -177,7 +193,7 @@ module Komic
|
|
177
193
|
|
178
194
|
content_builder = ContentBuilder.new(meta, images)
|
179
195
|
File.open(File.join(root_dir, './content.json'), 'w') do |file|
|
180
|
-
file.write content_builder.to_build
|
196
|
+
file.write content_builder.to_build(options)
|
181
197
|
end
|
182
198
|
end
|
183
199
|
|
@@ -188,12 +204,18 @@ module Komic
|
|
188
204
|
dist_branch = "master"
|
189
205
|
uri = "https://github.com/komic-awesome/#{dist_project}/archive/#{dist_branch}.zip"
|
190
206
|
source = open(uri)
|
191
|
-
Zip::File.open(source.path) do |zip_file|
|
207
|
+
::Zip::File.open(source.path) do |zip_file|
|
192
208
|
zip_file.each do |entry|
|
193
209
|
entry.extract(File.join(root_dir, File.basename(entry.name))) \
|
194
210
|
if File.fnmatch("#{dist_project}-#{dist_branch}/?*", entry.name)
|
195
211
|
end
|
196
212
|
end
|
213
|
+
Dir.glob("#{root_dir}/**/*.js") do |path|
|
214
|
+
uglified = Uglifier.compile(File.read(path))
|
215
|
+
File.open(path, 'w') do |file|
|
216
|
+
file.write uglified
|
217
|
+
end
|
218
|
+
end
|
197
219
|
end
|
198
220
|
end
|
199
221
|
end
|
data/lib/komic/version.rb
CHANGED
data/spec/builder_spec.rb
CHANGED
@@ -5,9 +5,19 @@ describe Komic::Builder do
|
|
5
5
|
subject { Komic::Builder::Factory }
|
6
6
|
context "detect file" do
|
7
7
|
before { allow(File).to receive(:exists?).and_return(true) }
|
8
|
-
|
9
|
-
|
8
|
+
|
9
|
+
context "detect type" do
|
10
|
+
before { allow(File).to receive(:directory?).and_return(true) }
|
11
|
+
it "detect type" do
|
12
|
+
expect( subject.detect_type('test.pdf') ).to be_eql('pdf')
|
13
|
+
expect( subject.detect_type('test.zip') ).to be_eql('zip')
|
14
|
+
expect( subject.detect_type('test') ).to be_eql('directory')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "detect file throw error" do
|
10
19
|
expect{ subject.detect_type('.pdf') }.to raise_error RuntimeError
|
20
|
+
expect{ subject.detect_type('.zip') }.to raise_error RuntimeError
|
11
21
|
end
|
12
22
|
end
|
13
23
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: komic-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hxgdzyuyi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '1.1'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: uglifier
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 2.7.1
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 2.7.1
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
140
|
name: bundler
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -211,8 +225,10 @@ files:
|
|
211
225
|
- komic-cli.gemspec
|
212
226
|
- lib/komic.rb
|
213
227
|
- lib/komic/builder.rb
|
228
|
+
- lib/komic/builder/directory.rb
|
214
229
|
- lib/komic/builder/douban_album.rb
|
215
230
|
- lib/komic/builder/pdf.rb
|
231
|
+
- lib/komic/builder/zip.rb
|
216
232
|
- lib/komic/cli.rb
|
217
233
|
- lib/komic/generator/content.schema.json
|
218
234
|
- lib/komic/generator/generator.rb
|