comicbook 0.3.0 → 0.5.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.
@@ -1,3 +1,3 @@
1
1
  class ComicBook
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
data/lib/comicbook.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require_relative 'comic_book/version'
2
2
  require_relative 'comic_book/page'
3
+ require_relative 'comic_book/page_arranger'
4
+ require_relative 'comic_book/entry'
3
5
  require_relative 'comic_book/cb'
4
6
  require_relative 'comic_book/cb7'
5
7
  require_relative 'comic_book/cba'
@@ -7,6 +9,7 @@ require_relative 'comic_book/cbr'
7
9
  require_relative 'comic_book/cbt'
8
10
  require_relative 'comic_book/cbz'
9
11
  require_relative 'comic_book/pdf'
12
+ require_relative 'comic_book/signature'
10
13
  require_relative 'comic_book/cli_helpers'
11
14
 
12
15
  class ComicBook
@@ -17,6 +20,22 @@ class ComicBook
17
20
  IMAGE_EXTENSIONS = %w[.jpg .jpeg .png .gif .bmp .webp].freeze
18
21
  IMAGE_GLOB_PATTERN = '*.{jpg,jpeg,png,gif,bmp,webp}'.freeze
19
22
 
23
+ # Metadata sidecar files (used by the :images_and_info file selection).
24
+ INFO_FILENAMES = %w[ComicInfo.xml MetronInfo.xml].freeze
25
+
26
+ # Every kind of comic: a format's file extension, or :folder for a plain folder.
27
+ TYPES = %i[cb cb7 cba cbr cbt cbz folder pdf].freeze
28
+
29
+ # The types that can be opened: their pages listed, their metadata read.
30
+ # Everything but :cba, which is recognized but not implemented.
31
+ READABLE_TYPES = %i[cb cb7 cbr cbt cbz folder pdf].freeze
32
+
33
+ # The types #archive can produce. RAR and ACE are proprietary, and PDF is only read.
34
+ WRITABLE_TYPES = %i[cb cb7 cbt cbz].freeze
35
+
36
+ # How #arrange_pages names page files unless told otherwise: page_001, page_002, ...
37
+ DEFAULT_PAGE_FILENAME_TEMPLATE = 'page_{page:3}'.freeze
38
+
20
39
  attr_reader :path, :type
21
40
 
22
41
  def initialize path
@@ -37,19 +56,6 @@ class ComicBook
37
56
  new(path).extract options
38
57
  end
39
58
 
40
- def pages
41
- case type
42
- when :folder then folder_pages
43
- else adapter.pages
44
- end
45
- end
46
-
47
- def info
48
- return CB.new(path).info if type == :folder
49
-
50
- adapter.info
51
- end
52
-
53
59
  def archive options = {}
54
60
  raise Error, 'Cannot archive a file' unless %i[folder cb].include?(type)
55
61
 
@@ -72,58 +78,112 @@ class ComicBook
72
78
  adapter.extract options
73
79
  end
74
80
 
75
- private
76
-
77
- def determine_type path
78
- if File.directory? path
79
- File.extname(path).downcase == '.cb' ? :cb : :folder
80
- elsif File.file? path
81
- extension = File.extname(path).downcase
82
-
83
- case extension
84
- when '.cbz' then :cbz
85
- when '.cb7' then :cb7
86
- when '.cbt' then :cbt
87
- when '.cbr' then :cbr
88
- when '.cba' then :cba
89
- when '.pdf' then :pdf
90
- else
91
- raise Error, "Unsupported file type: #{File.extname(path)}"
92
- end
93
- else
94
- raise Error, "Path does not exist: #{path}"
95
- end
81
+ def info = adapter.info
82
+
83
+ # Make this folder's page images exactly `sources`, in that order.
84
+ #
85
+ # Use it to reorder, delete, and add pages of an extracted comic before archiving it again:
86
+ #
87
+ # comic = ComicBook.load 'Issue1.cb'
88
+ # # Move the last of three pages to the front, drop the second, and add a new one.
89
+ # comic.arrange_pages ['page3.jpg', 'page1.jpg', '/Users/me/Desktop/extra.png']
90
+ #
91
+ # Each source is an image file: a path relative to the folder for a page already in it,
92
+ # or an absolute path for a file anywhere (it is copied in). Afterwards:
93
+ #
94
+ # - The pages are named from `template` in the given order, keeping their extensions:
95
+ # page_001, page_002, ... unless you pass another. ComicInfo::Issue#page_filename fills
96
+ # the template in, so it can use {page} and the comic's details, such as
97
+ # '{series}_{number:3}_{page:4}', and the names always sort in page order.
98
+ # - They are in the folder their pages were in before (the shared parent, if there were several).
99
+ # - Page images left out of `sources` are deleted. Files that are not images are left alone.
100
+ # - If the folder has a ComicInfo.xml, its <Page> entries and PageCount are updated to follow
101
+ # the pages (see ComicInfo::Issue#rearranging_pages).
102
+ #
103
+ # Only a folder or .cb can be arranged; extract an archive first.
104
+ # Returns the comic's pages afterwards. Raises Error, changing nothing, for a source that is
105
+ # missing or is not an image.
106
+ def arrange_pages sources, template: DEFAULT_PAGE_FILENAME_TEMPLATE
107
+ raise Error, "Cannot arrange the pages of a #{type} file; extract it first" unless %i[folder cb].include?(type)
108
+
109
+ PageArranger.new(path).arrange sources, template:
96
110
  end
97
111
 
98
- def validate_path!
99
- return if File.exist? path
100
-
101
- raise Error, "Path does not exist: #{path}"
112
+ # The files in this comicbook, filtered by type:
113
+ # :all (default),
114
+ # :images,
115
+ # :images_and_info (images + ComicInfo.xml / MetronInfo.xml)
116
+ def files type: :all
117
+ filter_files adapter.entries, by: type
102
118
  end
103
119
 
104
- def folder_pages
105
- pattern = IMAGE_GLOB_PATTERN
106
- search_path = File.join @path, '**', pattern
107
- image_files = Dir.glob search_path, File::FNM_CASEFOLD
108
-
109
- image_files.sort.map do |file|
110
- basename = File.basename file
120
+ # Pages are the image files, in path order, wrapped as Page objects.
121
+ # PDF renders synthetic pages and CBA is a stub, so both stay custom.
122
+ def pages
123
+ return adapter.pages if %i[cba pdf].include?(type)
111
124
 
112
- Page.new file, basename
113
- end
125
+ files(type: :images).map { Page.new it.path, it.name }
114
126
  end
115
127
 
128
+ private
129
+
116
130
  def adapter
117
131
  case type
118
- when :cb then CB.new path
119
132
  when :cb7 then CB7.new path
120
133
  when :cba then CBA.new path
121
134
  when :cbr then CBR.new path
122
135
  when :cbt then CBT.new path
123
136
  when :cbz then CBZ.new path
124
137
  when :pdf then PDF.new path
138
+ when :cb, :folder then CB.new path
125
139
  else
126
140
  raise Error, "No adapter available for type: #{type}"
127
141
  end
128
142
  end
143
+
144
+ def determine_type path
145
+ if File.directory? path
146
+ File.extname(path).downcase == '.cb' ? :cb : :folder
147
+ elsif File.file? path
148
+ # The name says whether this is a comic at all.
149
+ # What kind it is comes from its contents, which are right more often than its name.
150
+ named = type_named File.extname(path)
151
+
152
+ Signature.new(path).type || named
153
+ else
154
+ raise Error, "Path does not exist: #{path}"
155
+ end
156
+ end
157
+
158
+ def type_named extension
159
+ case extension.downcase
160
+ when '.cb7' then :cb7
161
+ when '.cba' then :cba
162
+ when '.cbr' then :cbr
163
+ when '.cbt' then :cbt
164
+ when '.cbz' then :cbz
165
+ when '.pdf' then :pdf
166
+ else
167
+ raise Error, "Unsupported file type: #{extension}"
168
+ end
169
+ end
170
+
171
+ def filter_files entries, by:
172
+ case by
173
+ when :all then entries
174
+ when :images then images
175
+ when :images_and_info then images_and_info
176
+ else
177
+ raise Error, "Unknown files type: #{by.inspect} (expected :all, :images, or :images_and_info)"
178
+ end.sort_by(&:path)
179
+ end
180
+
181
+ def images = adapter.images
182
+ def images_and_info = adapter.images_and_info
183
+
184
+ def validate_path!
185
+ return if File.exist? path
186
+
187
+ raise Error, "Path does not exist: #{path}"
188
+ end
129
189
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comicbook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Becker
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '2.0'
18
+ version: '2.1'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: '2.0'
25
+ version: '2.1'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: ruby-vips
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -43,14 +43,14 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 3.2.2
46
+ version: 3.4.0
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 3.2.2
53
+ version: 3.4.0
54
54
  - !ruby/object:Gem::Dependency
55
55
  name: seven-zip
56
56
  requirement: !ruby/object:Gem::Requirement
@@ -103,9 +103,12 @@ files:
103
103
  - lib/comic_book/cbz/extractor.rb
104
104
  - lib/comic_book/cli.rb
105
105
  - lib/comic_book/cli_helpers.rb
106
+ - lib/comic_book/entry.rb
106
107
  - lib/comic_book/page.rb
108
+ - lib/comic_book/page_arranger.rb
107
109
  - lib/comic_book/pdf.rb
108
110
  - lib/comic_book/pdf/extractor.rb
111
+ - lib/comic_book/signature.rb
109
112
  - lib/comic_book/vendor/linux/MPL-License-for-lsar-and-unar.txt
110
113
  - lib/comic_book/vendor/macos/MPL-License-for-lsar-and-unar.txt
111
114
  - lib/comic_book/vendor/macos/lsar
@@ -137,14 +140,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
140
  requirements:
138
141
  - - ">="
139
142
  - !ruby/object:Gem::Version
140
- version: 4.0.0
143
+ version: 4.0.9
141
144
  required_rubygems_version: !ruby/object:Gem::Requirement
142
145
  requirements:
143
146
  - - ">="
144
147
  - !ruby/object:Gem::Version
145
148
  version: '0'
146
149
  requirements: []
147
- rubygems_version: 4.0.6
150
+ rubygems_version: 4.0.20
148
151
  specification_version: 4
149
152
  summary: Ruby interface for working with comic book archive files
150
153
  test_files: []