comicbook 0.4.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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/AGENT.md +8 -7
- data/CHANGELOG.md +23 -0
- data/README.md +44 -6
- data/SYSTEM.md +133 -108
- data/lib/comic_book/cb.rb +6 -1
- data/lib/comic_book/cb7/extractor.rb +1 -0
- data/lib/comic_book/cb7.rb +1 -1
- data/lib/comic_book/cbr/extractor.rb +12 -0
- data/lib/comic_book/cbr.rb +1 -1
- data/lib/comic_book/cbt/extractor.rb +1 -0
- data/lib/comic_book/cbt.rb +1 -1
- data/lib/comic_book/cbz/extractor.rb +1 -0
- data/lib/comic_book/cbz.rb +1 -1
- data/lib/comic_book/entry.rb +9 -0
- data/lib/comic_book/page_arranger.rb +95 -0
- data/lib/comic_book/signature.rb +60 -0
- data/lib/comic_book/version.rb +1 -1
- data/lib/comicbook.rb +62 -12
- metadata +7 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 031eb52d98db1f37e3b6591962bdf59b1eeded272880d535c54498a43aaccac5
|
|
4
|
+
data.tar.gz: e28d1becaa131307edefc55f9c08809fed7e2eea1db6caa985f728230564fc1e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ef8be7943dae6f665343d7e9ec890fbecf062e549a091fc4c77ed0959380a6a59eb6896fe08b18e2380cc2d49c282654dae4a3f089032554617ee820305eba86
|
|
7
|
+
data.tar.gz: 299be6b9328f8729140a1509716c63addbdcd36ee47cb01535c05aeaf61e2bc0a55e8f3e2aa3b9ddd13e6d6073a6b33bf9963a04b04faf210b0cb0e66dcac2f1
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
4.0.
|
|
1
|
+
4.0.9
|
data/AGENT.md
CHANGED
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
|
|
13
13
|
### Adapter Pattern
|
|
14
14
|
```
|
|
15
|
-
Format (e.g.,
|
|
16
|
-
├──
|
|
17
|
-
├── archiver.rb - creates archives
|
|
18
|
-
└── extractor.rb - extracts archives
|
|
15
|
+
Format (e.g., CBZ)
|
|
16
|
+
├── cbz.rb - inherits from Adapter
|
|
17
|
+
├── cbz/archiver.rb - creates archives (not for extract-only formats: CBR, PDF)
|
|
18
|
+
└── cbz/extractor.rb - extracts archives
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
### TmpDir Testing
|
|
@@ -88,8 +88,9 @@ end
|
|
|
88
88
|
|
|
89
89
|
## Vendored Tools
|
|
90
90
|
|
|
91
|
-
-
|
|
92
|
-
-
|
|
91
|
+
- `lsar` / `unar` (for CBR) live in `lib/comic_book/vendor/<platform>/` for macos and windows
|
|
92
|
+
- On linux they are not vendored: `ComicBook::CLIHelpers` expects them installed on the system
|
|
93
|
+
- Run them through `ComicBook::CLIHelpers`, which detects the platform
|
|
93
94
|
- Handle command errors gracefully
|
|
94
95
|
- Parse output appropriately
|
|
95
96
|
|
|
@@ -105,6 +106,6 @@ end
|
|
|
105
106
|
|
|
106
107
|
- All tests pass
|
|
107
108
|
- RuboCop clean
|
|
108
|
-
- Same patterns as CB7/CBT/CBZ
|
|
109
|
+
- Same patterns as CB7/CBR/CBT/CBZ
|
|
109
110
|
- No repo pollution
|
|
110
111
|
- Documentation current
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.5.0] - 2026-11-19
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `ComicBook#arrange_pages(sources, template:)`: make a folder's page images exactly the given files, in order. Reorders, deletes, and adds pages of an extracted comic in one step: pages stay in the subfolder they were in, files that are not images are left alone, and a `ComicInfo.xml` in the folder has its `<Page>` entries and `PageCount` updated to match. Pages are named `page_001…` unless a `template:` such as `{series}_{number:3}_{page:4}` is given, which the comicinfo gem's `Issue#page_filename` fills in from the folder's `ComicInfo.xml`. `ComicBook::PageArranger` does the work
|
|
8
|
+
- Format support is public: `ComicBook::TYPES`, `ComicBook::READABLE_TYPES` (everything but `:cba`) and `ComicBook::WRITABLE_TYPES` (`:cb`, `:cb7`, `:cbt`, `:cbz`)
|
|
9
|
+
- `ComicBook::DEFAULT_PAGE_FILENAME_TEMPLATE` (`page_{page:3}`)
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- Mislabelled archives now open. A comic's type is told from the bytes the file starts with, so a RAR named `.cbz` (common in the wild) loads with `type == :cbr` and is read as one. ZIP, RAR, 7-Zip, TAR, PDF and ACE are recognized, by the new `ComicBook::Signature`. The extension still decides which files are comics at all (a `.zip` is refused as before) and is used when the contents are not recognized
|
|
14
|
+
- macOS metadata is no longer part of a comic. Archives made on a Mac can carry AppleDouble members (`._page1.jpg`, written by `tar` beside a file with extended attributes) or a `__MACOSX` folder (where Finder's Compress puts the same data). They were listed as files, counted as pages because their names end in an image extension, and extracted. All formats now leave them out of `files`, `pages` and `extract`, and a folder's `__MACOSX` is left out when it is archived. `ComicBook::Entry#macos_metadata?` is the check
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Requires comicinfo 2.1 or later (was 2.0)
|
|
19
|
+
- CI runs on `ubuntu-24.04` instead of `ubuntu-latest`: on Ubuntu 26.04 the `seven-zip` gem crashed Ruby when reading an empty 7z
|
|
20
|
+
- CI retries a stalled package download instead of hanging, and installs from Ubuntu's main archive: installing `unar` and `libvips-dev` was stalling on the runner's regional mirror
|
|
21
|
+
- Bump Ruby version from 4.0.5 to 4.0.9 (`.ruby-version`, `required_ruby_version`, CI matrix)
|
|
22
|
+
- Brought `README.md`, `SYSTEM.md`, `AGENT.md` and `TODO.md` up to date: CBR, PDF, `.cb` folders, the CLI, `files`, and `contents:` are documented as they work today; finished roadmap items are removed
|
|
23
|
+
|
|
1
24
|
## [0.4.0] - 2026-06-18
|
|
2
25
|
|
|
3
26
|
### Added
|
data/README.md
CHANGED
|
@@ -5,7 +5,8 @@ A Ruby library and CLI tool for managing comic books archives.
|
|
|
5
5
|
- `extract`: to open a `.cb*` file
|
|
6
6
|
- `archive`: to create a `.cb*` file
|
|
7
7
|
- `info`: to read `ComicInfo.xml` metadata from a `.cb*` file
|
|
8
|
-
- `pages`: to list page image files in a `.cb*` file
|
|
8
|
+
- `pages`: to list page image files in a `.cb*` file (Ruby API only)
|
|
9
|
+
- `arrange_pages`: to reorder, delete, and add pages of an extracted comic (Ruby API only)
|
|
9
10
|
|
|
10
11
|
---
|
|
11
12
|
|
|
@@ -16,12 +17,21 @@ Support for `archive` and `extract` :
|
|
|
16
17
|
- `CBT`: [Tar](<https://en.wikipedia.org/wiki/Tar_(computing)>)
|
|
17
18
|
- `CBZ`: [Zip](<https://en.wikipedia.org/wiki/ZIP_(file_format)>) **default**
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
Support for only `extract`:
|
|
20
21
|
|
|
21
22
|
- `CBR`: [RAR](https://en.wikipedia.org/wiki/WinRAR) is proprietary without an open source implementation license.
|
|
23
|
+
Uses the `lsar` / `unar` tools: bundled for macOS and Windows, on Linux install the `unar` package.
|
|
22
24
|
Extracting support because a large number of comic books are archived in .cbr/.rar format.
|
|
23
25
|
No support for creating `.cbr` files will ever be added unless `RAR` is open source (or reverse engineered).
|
|
24
26
|
- `PDF`: Convert a PDF to a `.cb*` archive using [libvips](https://www.libvips.org) . Requires `libvips` installed on the system.
|
|
27
|
+
|
|
28
|
+
A comic's type comes from what the file contains, not its name, so a mislabelled archive still opens: a RAR named `.cbz` is read as a CBR.
|
|
29
|
+
The name still decides which files are comics at all (a `.zip` is refused).
|
|
30
|
+
|
|
31
|
+
Files that macOS adds to archives it makes (`._page1.jpg`, a `__MACOSX` folder) are never listed, counted as pages, or extracted.
|
|
32
|
+
|
|
33
|
+
Not supported yet:
|
|
34
|
+
|
|
25
35
|
- `CBA`: [ACE](https://en.wikipedia.org/wiki/WinAce) is both proprietary and very old/outdated/unsupported.
|
|
26
36
|
ACE extracting support is planned, to support historical posterity and completeness.
|
|
27
37
|
**NEEDED: actual `.cba` files** to test against (with and without `ComicInfo.xml`).
|
|
@@ -57,11 +67,14 @@ comicbook extract path/to/archive.cbz --images-only
|
|
|
57
67
|
# Extract PDF at custom DPI (default: 300)
|
|
58
68
|
comicbook extract path/to/comic.pdf --dpi 600
|
|
59
69
|
|
|
70
|
+
# Delete the source after extracting (or archiving)
|
|
71
|
+
comicbook extract path/to/archive.cbz --delete-original
|
|
72
|
+
|
|
60
73
|
# Create a comic book archive from a folder
|
|
61
74
|
comicbook archive path/to/folder
|
|
62
75
|
|
|
63
|
-
# Create archive at a specific destination
|
|
64
|
-
comicbook archive path/to/folder --to path/to/output.
|
|
76
|
+
# Create archive at a specific destination (the extension picks the format: .cbz, .cb7, .cbt, .cb)
|
|
77
|
+
comicbook archive path/to/folder --to path/to/output.cb7
|
|
65
78
|
|
|
66
79
|
# Show ComicInfo.xml metadata
|
|
67
80
|
comicbook info path/to/archive.cbz
|
|
@@ -70,6 +83,7 @@ comicbook info path/to/archive.cbz
|
|
|
70
83
|
comicbook info path/to/archive.cbz --format json
|
|
71
84
|
comicbook info path/to/archive.cbz --format yaml
|
|
72
85
|
comicbook info path/to/archive.cbz --format terse
|
|
86
|
+
comicbook info path/to/archive.cbz --format verbose # default
|
|
73
87
|
|
|
74
88
|
# Show only specific fields
|
|
75
89
|
comicbook info path/to/archive.cbz --only title,writer,publisher
|
|
@@ -77,8 +91,9 @@ comicbook info path/to/archive.cbz --only title,writer,publisher
|
|
|
77
91
|
# Show all fields except specific ones
|
|
78
92
|
comicbook info path/to/archive.cbz --except summary,review
|
|
79
93
|
|
|
80
|
-
# Show help
|
|
94
|
+
# Show help or version
|
|
81
95
|
comicbook --help
|
|
96
|
+
comicbook --version
|
|
82
97
|
```
|
|
83
98
|
|
|
84
99
|
### Ruby API
|
|
@@ -96,13 +111,36 @@ ComicBook.extract 'path/to/archive.cbz', images_only: true
|
|
|
96
111
|
# Create a comic book archive from a folder (creates .cbz by default)
|
|
97
112
|
ComicBook.new('path/to/folder').archive
|
|
98
113
|
|
|
99
|
-
# Create archive at a specific destination
|
|
114
|
+
# Create archive at a specific destination (the extension picks the format)
|
|
100
115
|
ComicBook.new('path/to/folder').archive to: 'path/to/output.cbz'
|
|
101
116
|
|
|
117
|
+
# Choose what goes into the archive: :all (default), :images, or :images_and_info
|
|
118
|
+
ComicBook.new('path/to/folder').archive contents: :images_and_info
|
|
119
|
+
|
|
102
120
|
# Get pages from an archive
|
|
103
121
|
comic = ComicBook.new 'path/to/archive.cbz'
|
|
104
122
|
comic.pages # => [#<ComicBook::Page>, ...]
|
|
105
123
|
|
|
124
|
+
# List files in an archive: :all (default), :images, or :images_and_info
|
|
125
|
+
comic.files # => [#<ComicBook::Entry>, ...]
|
|
126
|
+
comic.files type: :images # => [#<ComicBook::Entry>, ...]
|
|
127
|
+
|
|
128
|
+
# Reorder, delete, and add pages of an extracted comic, then archive it again.
|
|
129
|
+
# Each source is a page already in the folder (relative path) or any image file (absolute path).
|
|
130
|
+
extracted = ComicBook.new 'path/to/extracted.cb'
|
|
131
|
+
extracted.arrange_pages ['page3.jpg', 'page1.jpg', '/Users/me/Desktop/extra.png']
|
|
132
|
+
extracted.archive to: 'path/to/output.cbz'
|
|
133
|
+
|
|
134
|
+
# Pages are renumbered page_001... in the given order, page images left out are deleted,
|
|
135
|
+
# and a ComicInfo.xml in the folder has its <Page> entries and PageCount updated to match.
|
|
136
|
+
# Pass a template to name the page files another way. It can use {page} and the comic's
|
|
137
|
+
# details, and the names always sort in page order. Write it in single quotes.
|
|
138
|
+
extracted.arrange_pages sources, template: '{series}_{number:3}_{page:4}' # Saga_007_0001.jpg ...
|
|
139
|
+
|
|
140
|
+
# Which formats can be opened, and which can be written
|
|
141
|
+
ComicBook::READABLE_TYPES # => [:cb, :cb7, :cbr, :cbt, :cbz, :folder, :pdf]
|
|
142
|
+
ComicBook::WRITABLE_TYPES # => [:cb, :cb7, :cbt, :cbz]
|
|
143
|
+
|
|
106
144
|
# Read ComicInfo.xml metadata
|
|
107
145
|
comic = ComicBook.new 'path/to/archive.cbz'
|
|
108
146
|
comic.info # => #<ComicInfo::Issue> or nil
|
data/SYSTEM.md
CHANGED
|
@@ -2,36 +2,53 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
ComicBook
|
|
5
|
+
ComicBook is a Ruby library and CLI for reading, writing, and converting comic book archive files. It provides one interface over several formats: CB (folder), CBZ (ZIP), CB7 (7-Zip), CBT (TAR), CBR (RAR, extract only), and PDF (extract only). CBA (ACE) is recognised but not implemented.
|
|
6
6
|
|
|
7
7
|
## Project Structure
|
|
8
8
|
|
|
9
9
|
```
|
|
10
|
-
comicbook
|
|
10
|
+
comicbook/
|
|
11
|
+
├── exe/
|
|
12
|
+
│ └── comicbook # CLI executable
|
|
11
13
|
├── lib/
|
|
12
|
-
│ ├── comicbook.rb
|
|
14
|
+
│ ├── comicbook.rb # Main entry point and public API
|
|
13
15
|
│ └── comic_book/
|
|
14
|
-
│ ├── adapter.rb
|
|
15
|
-
│ ├──
|
|
16
|
-
│ ├──
|
|
17
|
-
│ ├──
|
|
16
|
+
│ ├── adapter.rb # Base adapter class
|
|
17
|
+
│ ├── cli.rb # CLI commands (extract, archive, info)
|
|
18
|
+
│ ├── cli_helpers.rb # Runs the lsar / unar tools
|
|
19
|
+
│ ├── entry.rb # One file inside a comic
|
|
20
|
+
│ ├── page.rb # Page representation
|
|
21
|
+
│ ├── page_arranger.rb # Reorders, deletes and adds a folder's pages
|
|
22
|
+
│ ├── signature.rb # A file's type, from the bytes it starts with
|
|
23
|
+
│ ├── version.rb # Version constant
|
|
24
|
+
│ ├── cb.rb # CB (folder) adapter
|
|
25
|
+
│ ├── cb/
|
|
26
|
+
│ │ ├── archiver.rb
|
|
27
|
+
│ │ └── extractor.rb # Always raises: a .cb folder is already extracted
|
|
18
28
|
│ ├── cb7.rb # CB7 (7-Zip) adapter
|
|
19
29
|
│ ├── cb7/
|
|
20
|
-
│ │ ├── archiver.rb
|
|
21
|
-
│ │ └── extractor.rb
|
|
30
|
+
│ │ ├── archiver.rb
|
|
31
|
+
│ │ └── extractor.rb
|
|
32
|
+
│ ├── cba.rb # CBA (ACE) stub: every operation raises
|
|
33
|
+
│ ├── cbr.rb # CBR (RAR) adapter
|
|
34
|
+
│ ├── cbr/
|
|
35
|
+
│ │ └── extractor.rb
|
|
22
36
|
│ ├── cbt.rb # CBT (TAR) adapter
|
|
23
37
|
│ ├── cbt/
|
|
24
|
-
│ │ ├── archiver.rb
|
|
25
|
-
│ │ └── extractor.rb
|
|
38
|
+
│ │ ├── archiver.rb
|
|
39
|
+
│ │ └── extractor.rb
|
|
26
40
|
│ ├── cbz.rb # CBZ (ZIP) adapter
|
|
27
41
|
│ ├── cbz/
|
|
28
|
-
│ │ ├── archiver.rb
|
|
29
|
-
│ │ └── extractor.rb
|
|
30
|
-
│
|
|
42
|
+
│ │ ├── archiver.rb
|
|
43
|
+
│ │ └── extractor.rb
|
|
44
|
+
│ ├── pdf.rb # PDF adapter
|
|
45
|
+
│ ├── pdf/
|
|
46
|
+
│ │ └── extractor.rb # Renders each PDF page to an image
|
|
47
|
+
│ └── vendor/ # lsar / unar binaries (macOS, Windows)
|
|
31
48
|
├── spec/ # RSpec test suite
|
|
32
49
|
│ ├── fixtures/ # Test fixtures for all formats
|
|
33
|
-
│ └── comic_book/ #
|
|
34
|
-
└── bin/ #
|
|
50
|
+
│ └── comic_book/ # One spec per component
|
|
51
|
+
└── bin/ # Development tools (setup, console)
|
|
35
52
|
```
|
|
36
53
|
|
|
37
54
|
## Architecture
|
|
@@ -39,32 +56,39 @@ comicbook-ruby/
|
|
|
39
56
|
### Core Design Patterns
|
|
40
57
|
|
|
41
58
|
#### 1. Adapter Pattern
|
|
42
|
-
|
|
59
|
+
Each format implements the interface defined by `ComicBook::Adapter`.
|
|
43
60
|
|
|
44
61
|
```ruby
|
|
45
62
|
class ComicBook::Adapter
|
|
46
|
-
def archive(options = {})
|
|
47
|
-
def extract(options = {})
|
|
48
|
-
def
|
|
63
|
+
def archive(options = {}) # Create archive from folder
|
|
64
|
+
def extract(options = {}) # Extract archive to folder
|
|
65
|
+
def entries # Every file in the comic, as Entry objects
|
|
66
|
+
def info # ComicInfo::Issue from ComicInfo.xml, or nil
|
|
67
|
+
def pages # Page objects (only PDF and CBA define this themselves)
|
|
68
|
+
def images # entries that are images
|
|
69
|
+
def images_and_info # entries that are images or info files
|
|
49
70
|
end
|
|
50
71
|
```
|
|
51
72
|
|
|
52
73
|
#### 2. Strategy Pattern
|
|
53
|
-
The main `ComicBook` class
|
|
74
|
+
The main `ComicBook` class picks the adapter from the detected type:
|
|
54
75
|
|
|
55
76
|
```ruby
|
|
56
77
|
def adapter
|
|
57
78
|
case type
|
|
58
79
|
when :cb7 then CB7.new path
|
|
80
|
+
when :cba then CBA.new path
|
|
81
|
+
when :cbr then CBR.new path
|
|
59
82
|
when :cbt then CBT.new path
|
|
60
83
|
when :cbz then CBZ.new path
|
|
61
|
-
|
|
84
|
+
when :pdf then PDF.new path
|
|
85
|
+
when :cb, :folder then CB.new path
|
|
62
86
|
end
|
|
63
87
|
end
|
|
64
88
|
```
|
|
65
89
|
|
|
66
|
-
#### 3.
|
|
67
|
-
Each adapter
|
|
90
|
+
#### 3. Archiver / Extractor Classes
|
|
91
|
+
Each adapter delegates to an `Archiver` and an `Extractor` class that hold the format-specific work. Formats that cannot be written (CBR, PDF) have only an `Extractor`.
|
|
68
92
|
|
|
69
93
|
### Key Components
|
|
70
94
|
|
|
@@ -72,108 +96,128 @@ Each adapter family (CB7, CBT, CBZ) follows a consistent structure with separate
|
|
|
72
96
|
- **Location**: `lib/comicbook.rb`
|
|
73
97
|
- **Purpose**: Public API entry point and file type detection
|
|
74
98
|
- **Responsibilities**:
|
|
75
|
-
-
|
|
99
|
+
- Type detection: a folder is `:folder`, or `:cb` if its name ends in `.cb`; a file must have a comic extension, and is typed by its contents (`ComicBook::Signature`), falling back to the extension
|
|
76
100
|
- Path validation
|
|
77
|
-
- Delegation to
|
|
78
|
-
-
|
|
101
|
+
- Delegation to the adapter
|
|
102
|
+
- `archive`, `extract`, `info`, `files(type:)`, `pages`, `arrange_pages(sources, template:)` (done by `ComicBook::PageArranger`)
|
|
103
|
+
- `ComicBook::Info` is an alias of `ComicInfo::Issue`
|
|
79
104
|
|
|
80
105
|
#### 2. ComicBook::Adapter (Base Class)
|
|
81
106
|
- **Location**: `lib/comic_book/adapter.rb`
|
|
82
107
|
- **Purpose**: Abstract base class for all format adapters
|
|
83
|
-
- **Pattern**: Template method defining common interface
|
|
84
|
-
- **Key Methods**: `archive`, `extract`, `pages`
|
|
85
108
|
|
|
86
109
|
#### 3. Format Adapters
|
|
87
|
-
Each format has three components:
|
|
88
110
|
|
|
89
|
-
**Main Adapter** (`cb7.rb`, `cbt.rb`, `cbz.rb`)
|
|
111
|
+
**Main Adapter** (`cb.rb`, `cb7.rb`, `cbr.rb`, `cbt.rb`, `cbz.rb`, `pdf.rb`)
|
|
90
112
|
- Inherits from `ComicBook::Adapter`
|
|
91
113
|
- Delegates to Archiver/Extractor classes
|
|
92
|
-
- Implements `
|
|
114
|
+
- Implements `entries` and `info`
|
|
93
115
|
|
|
94
116
|
**Archiver Classes** (`*/archiver.rb`)
|
|
95
|
-
-
|
|
96
|
-
-
|
|
97
|
-
- Manages file filtering (images only)
|
|
98
|
-
- Supports options like custom extensions and delete_original
|
|
117
|
+
- Create an archive from a source folder
|
|
118
|
+
- Choose which files go in with the `contents` option
|
|
99
119
|
|
|
100
120
|
**Extractor Classes** (`*/extractor.rb`)
|
|
101
|
-
-
|
|
102
|
-
-
|
|
103
|
-
- Filters extracted files (images only)
|
|
104
|
-
- Supports custom destinations and extensions
|
|
121
|
+
- Extract an archive to a folder
|
|
122
|
+
- Extract every file unless `images_only` is set
|
|
105
123
|
|
|
106
|
-
#### 4. ComicBook::
|
|
124
|
+
#### 4. ComicBook::Entry
|
|
125
|
+
- **Location**: `lib/comic_book/entry.rb`
|
|
126
|
+
- **Purpose**: One file inside a comic
|
|
127
|
+
- **Attributes**: `path` (relative path in a folder, or entry name in an archive), `name` (basename)
|
|
128
|
+
- **Predicates**: `image?`, `info?` (`ComicInfo.xml` or `MetronInfo.xml`), `macos_metadata?` (a `._` file or anything in `__MACOSX`; never listed or extracted)
|
|
129
|
+
|
|
130
|
+
#### 5. ComicBook::Page
|
|
107
131
|
- **Location**: `lib/comic_book/page.rb`
|
|
108
|
-
- **Purpose**:
|
|
109
|
-
- **Attributes**: `path
|
|
132
|
+
- **Purpose**: One page image of a comic
|
|
133
|
+
- **Attributes**: `path`, `name`
|
|
134
|
+
|
|
135
|
+
#### 6. ComicBook::CLI
|
|
136
|
+
- **Location**: `lib/comic_book/cli.rb`, run by `exe/comicbook`
|
|
137
|
+
- **Commands**: `extract`, `archive`, `info`
|
|
110
138
|
|
|
111
139
|
### File Type Support
|
|
112
140
|
|
|
113
|
-
| Format | Extension |
|
|
114
|
-
|
|
115
|
-
|
|
|
116
|
-
|
|
|
117
|
-
|
|
|
118
|
-
|
|
|
119
|
-
|
|
|
141
|
+
| Format | Extension | Archive | Extract | Info | Dependencies |
|
|
142
|
+
|--------|--------------|-----------------------|-----------------------------|-----------------|---------------------------|
|
|
143
|
+
| CB | `.cb` folder | ✅ | n/a (already a folder) | ✅ | Ruby stdlib |
|
|
144
|
+
| CBZ | `.cbz` | ✅ | ✅ | ✅ | `rubyzip` gem |
|
|
145
|
+
| CB7 | `.cb7` | ✅ | ✅ | ✅ | `seven-zip` gem |
|
|
146
|
+
| CBT | `.cbt` | ✅ | ✅ | ✅ | Ruby stdlib |
|
|
147
|
+
| CBR | `.cbr` | ❌ RAR is proprietary | ✅ | ✅ | `lsar` / `unar` |
|
|
148
|
+
| PDF | `.pdf` | ❌ | ✅ pages rendered to images | ❌ always `nil` | `ruby-vips` gem + libvips |
|
|
149
|
+
| CBA | `.cba` | ❌ ACE is proprietary | 🚧 Planned | 🚧 Planned | TBD |
|
|
150
|
+
|
|
151
|
+
`lsar` and `unar` are vendored for macOS and Windows in `lib/comic_book/vendor/`. On Linux they must be installed on the system (`unar` package).
|
|
120
152
|
|
|
121
153
|
### Data Flow
|
|
122
154
|
|
|
123
155
|
#### Archive Creation Flow
|
|
124
|
-
1. User calls `ComicBook.new(folder_path).archive
|
|
125
|
-
2. Main class
|
|
126
|
-
3.
|
|
127
|
-
4.
|
|
128
|
-
5.
|
|
129
|
-
6.
|
|
130
|
-
7. Returns path to created archive
|
|
156
|
+
1. User calls `ComicBook.new(folder_path).archive`
|
|
157
|
+
2. Main class checks the path is a folder (or `.cb` folder)
|
|
158
|
+
3. The extension of the `to:` option picks the adapter (`.cbz` by default)
|
|
159
|
+
4. The adapter's Archiver lists the folder's files, filtered by `contents`
|
|
160
|
+
5. Creates the archive
|
|
161
|
+
6. Returns the path of the created archive
|
|
131
162
|
|
|
132
163
|
#### Archive Extraction Flow
|
|
133
|
-
1. User calls `ComicBook.new(archive_path).extract
|
|
134
|
-
2. Main class detects file type
|
|
135
|
-
3. Delegates to
|
|
136
|
-
4.
|
|
137
|
-
5.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
8. Returns path to extracted folder
|
|
164
|
+
1. User calls `ComicBook.new(archive_path).extract`
|
|
165
|
+
2. Main class detects file type from the file's contents, falling back to its extension
|
|
166
|
+
3. Delegates to the adapter's Extractor
|
|
167
|
+
4. Extracts every file (only images if `images_only` is set) to the destination folder
|
|
168
|
+
5. Returns the path of the extracted folder
|
|
169
|
+
|
|
170
|
+
For a PDF, step 4 renders each page to an image at the `dpi` option's resolution.
|
|
141
171
|
|
|
142
172
|
#### Page Reading Flow
|
|
143
173
|
1. User calls `ComicBook.new(archive_path).pages`
|
|
144
|
-
2. Main class
|
|
145
|
-
3.
|
|
146
|
-
4.
|
|
147
|
-
5. Creates Page objects with path and name
|
|
148
|
-
6. Returns sorted array of Page objects
|
|
174
|
+
2. Main class asks the adapter for its entries
|
|
175
|
+
3. Keeps the image files, sorted by path
|
|
176
|
+
4. Returns an array of Page objects
|
|
149
177
|
|
|
150
178
|
## Configuration & Constants
|
|
151
179
|
|
|
152
|
-
### Image File Support
|
|
153
180
|
```ruby
|
|
154
181
|
IMAGE_EXTENSIONS = %w[.jpg .jpeg .png .gif .bmp .webp].freeze
|
|
155
182
|
IMAGE_GLOB_PATTERN = '*.{jpg,jpeg,png,gif,bmp,webp}'.freeze
|
|
156
|
-
|
|
183
|
+
INFO_FILENAMES = %w[ComicInfo.xml MetronInfo.xml].freeze
|
|
184
|
+
|
|
185
|
+
TYPES = %i[cb cb7 cba cbr cbt cbz folder pdf].freeze
|
|
186
|
+
READABLE_TYPES = %i[cb cb7 cbr cbt cbz folder pdf].freeze
|
|
187
|
+
WRITABLE_TYPES = %i[cb cb7 cbt cbz].freeze
|
|
157
188
|
|
|
158
|
-
|
|
189
|
+
DEFAULT_PAGE_FILENAME_TEMPLATE = 'page_{page:3}'.freeze
|
|
190
|
+
```
|
|
159
191
|
|
|
160
192
|
### Options Support
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
- `
|
|
164
|
-
- `
|
|
193
|
+
|
|
194
|
+
Archive options:
|
|
195
|
+
- `to`: Output path; its extension picks the format (default `.cbz` next to the source folder)
|
|
196
|
+
- `contents`: `:all` (default), `:images`, or `:images_and_info`
|
|
197
|
+
- `extension`: File extension for the default output path
|
|
198
|
+
- `delete_original`: Remove the source folder afterwards
|
|
199
|
+
|
|
200
|
+
Extract options:
|
|
201
|
+
- `to`: Destination folder (default: a `.cb` folder next to the archive)
|
|
202
|
+
- `images_only`: Extract only image files
|
|
203
|
+
- `extension`: Folder extension for the default destination
|
|
204
|
+
- `delete_original`: Remove the source archive afterwards
|
|
205
|
+
- `dpi`: PDF render resolution (default 300)
|
|
165
206
|
|
|
166
207
|
## Dependencies
|
|
167
208
|
|
|
168
209
|
### Runtime Dependencies
|
|
169
|
-
- **
|
|
210
|
+
- **comicinfo** (>= 2.1): Reads and updates `ComicInfo.xml`, and fills in page file name templates
|
|
211
|
+
- **rubyzip** (>= 3.4.0): ZIP file handling for CBZ format
|
|
170
212
|
- **seven-zip** (~> 1.4): 7-Zip handling for CB7 format
|
|
213
|
+
- **ruby-vips** (>= 2.3): PDF rendering; loaded only when a PDF is used, and needs libvips on the system
|
|
171
214
|
- Ruby stdlib: TAR handling for CBT format
|
|
215
|
+
- `lsar` / `unar`: RAR handling for CBR format (vendored for macOS and Windows)
|
|
172
216
|
|
|
173
217
|
### Development Dependencies
|
|
174
218
|
- **rspec**: Testing framework
|
|
175
219
|
- **rspec-file_fixtures**: Fixture management for tests
|
|
176
|
-
- **rubocop
|
|
220
|
+
- **rubocop** (with `rubocop-performance`, `rubocop-rake`, `rubocop-rspec`): Code linting and style enforcement
|
|
177
221
|
|
|
178
222
|
## Testing Strategy
|
|
179
223
|
|
|
@@ -188,48 +232,29 @@ All tests use `Dir.mktmpdir` for file I/O operations to ensure:
|
|
|
188
232
|
- Proper cleanup after each test
|
|
189
233
|
- Isolation between test runs
|
|
190
234
|
|
|
191
|
-
### Test Coverage
|
|
192
|
-
- 173 examples across all components
|
|
193
|
-
- Full coverage of archive/extract/pages operations
|
|
194
|
-
- Edge cases: empty archives, non-image files, nested folders
|
|
195
|
-
|
|
196
|
-
## Performance Considerations
|
|
197
|
-
|
|
198
|
-
### Memory Usage
|
|
199
|
-
- Streaming operations where possible
|
|
200
|
-
- No full file loading into memory for large archives
|
|
201
|
-
- Lazy page enumeration
|
|
202
|
-
|
|
203
|
-
### File I/O
|
|
204
|
-
- Temporary directory usage prevents repository pollution
|
|
205
|
-
- Atomic operations with proper cleanup
|
|
206
|
-
- Path normalization for cross-platform compatibility
|
|
207
|
-
|
|
208
235
|
## Error Handling
|
|
209
236
|
|
|
210
|
-
### Exception Hierarchy
|
|
211
237
|
```ruby
|
|
212
238
|
ComicBook::Error < StandardError
|
|
213
239
|
```
|
|
214
240
|
|
|
215
241
|
### Common Error Cases
|
|
216
242
|
- Unsupported file types
|
|
217
|
-
-
|
|
218
|
-
-
|
|
219
|
-
-
|
|
243
|
+
- Path does not exist
|
|
244
|
+
- Archiving a file, or extracting a folder
|
|
245
|
+
- Archiving to CBR, CBA, or PDF
|
|
246
|
+
- Any CBA operation (not yet implemented)
|
|
247
|
+
- `lsar` / `unar` failing or missing
|
|
248
|
+
- libvips missing when a PDF is used
|
|
220
249
|
|
|
221
|
-
##
|
|
250
|
+
## Adding New Formats
|
|
222
251
|
|
|
223
|
-
### Adding New Formats
|
|
224
252
|
1. Create new adapter class inheriting from `ComicBook::Adapter`
|
|
225
253
|
2. Implement archiver and extractor classes
|
|
226
254
|
3. Add format detection to main `ComicBook` class
|
|
227
|
-
4. Add
|
|
228
|
-
|
|
229
|
-
### Plugin Architecture
|
|
230
|
-
The adapter pattern makes it easy to add new formats or extend existing ones without modifying core functionality.
|
|
255
|
+
4. Add specs following existing patterns
|
|
231
256
|
|
|
232
257
|
## Version Information
|
|
233
|
-
- **
|
|
234
|
-
- **Ruby Requirement**: >= 4.0.
|
|
258
|
+
- **Version**: see `lib/comic_book/version.rb`
|
|
259
|
+
- **Ruby Requirement**: >= 4.0.9
|
|
235
260
|
- **License**: MIT
|
data/lib/comic_book/cb.rb
CHANGED
|
@@ -14,11 +14,16 @@ class ComicBook
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
# Every file in the folder, as Entries with folder-relative paths
|
|
17
|
+
# (hidden files and macOS metadata are left out)
|
|
17
18
|
def entries
|
|
18
|
-
Dir.glob(File.join(path, '**', '*')).reject { File.directory? it }
|
|
19
|
+
files = Dir.glob(File.join(path, '**', '*')).reject { File.directory? it }
|
|
20
|
+
|
|
21
|
+
entries = files.map do |file|
|
|
19
22
|
relative = Pathname.new(file).relative_path_from(Pathname.new(path)).to_s
|
|
20
23
|
ComicBook::Entry.new relative
|
|
21
24
|
end
|
|
25
|
+
|
|
26
|
+
entries.reject(&:macos_metadata?)
|
|
22
27
|
end
|
|
23
28
|
|
|
24
29
|
def info
|
|
@@ -63,6 +63,7 @@ class ComicBook
|
|
|
63
63
|
def extract_files destination, options, seven_zip_reader
|
|
64
64
|
seven_zip_reader.entries.each do |entry|
|
|
65
65
|
next unless entry.file?
|
|
66
|
+
next if ComicBook::Entry.new(entry.path).macos_metadata?
|
|
66
67
|
next if options[:images_only] && !image_file?(entry.path)
|
|
67
68
|
|
|
68
69
|
extract_single_file entry, destination, seven_zip_reader
|
data/lib/comic_book/cb7.rb
CHANGED
|
@@ -57,9 +57,21 @@ class ComicBook
|
|
|
57
57
|
|
|
58
58
|
def extract_files destination, options
|
|
59
59
|
CLIHelpers.unar_extract archive_path, destination
|
|
60
|
+
delete_macos_metadata destination
|
|
60
61
|
delete_non_images destination if options[:images_only]
|
|
61
62
|
end
|
|
62
63
|
|
|
64
|
+
# unar extracts the whole archive, so what macOS added is removed afterwards.
|
|
65
|
+
def delete_macos_metadata destination
|
|
66
|
+
archive_entries = CLIHelpers.lsar_list archive_path
|
|
67
|
+
|
|
68
|
+
archive_entries.each do |entry|
|
|
69
|
+
next unless ComicBook::Entry.new(entry).macos_metadata?
|
|
70
|
+
|
|
71
|
+
FileUtils.rm_rf File.join(destination, entry)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
63
75
|
def delete_non_images destination
|
|
64
76
|
archive_entries = CLIHelpers.lsar_list archive_path
|
|
65
77
|
|
data/lib/comic_book/cbr.rb
CHANGED
|
@@ -61,6 +61,7 @@ class ComicBook
|
|
|
61
61
|
def extract_files destination, options, tar
|
|
62
62
|
tar.each do |entry|
|
|
63
63
|
next unless entry.file?
|
|
64
|
+
next if ComicBook::Entry.new(entry.full_name).macos_metadata?
|
|
64
65
|
next if options[:images_only] && !image_file?(entry.full_name)
|
|
65
66
|
|
|
66
67
|
extract_single_file entry, destination
|
data/lib/comic_book/cbt.rb
CHANGED
|
@@ -63,6 +63,7 @@ class ComicBook
|
|
|
63
63
|
Zip::File.open(archive_path) do |zipfile|
|
|
64
64
|
zipfile.each do |entry|
|
|
65
65
|
next if entry.directory?
|
|
66
|
+
next if ComicBook::Entry.new(entry.name).macos_metadata?
|
|
66
67
|
next if options[:images_only] && !image_file?(entry.name)
|
|
67
68
|
|
|
68
69
|
extract_single_file entry, destination_basename
|
data/lib/comic_book/cbz.rb
CHANGED
data/lib/comic_book/entry.rb
CHANGED
|
@@ -16,5 +16,14 @@ class ComicBook
|
|
|
16
16
|
|
|
17
17
|
def image? = ComicBook::IMAGE_EXTENSIONS.include?(File.extname(name).downcase)
|
|
18
18
|
def info? = ComicBook::INFO_FILENAMES.include?(name)
|
|
19
|
+
|
|
20
|
+
# True if macOS added this to an archive, so it is not part of the comic:
|
|
21
|
+
# an AppleDouble member (`._page1.jpg`, which tar writes beside a file
|
|
22
|
+
# that has extended attributes),
|
|
23
|
+
# or anything in a `__MACOSX` folder (where Finder's Compress puts the same data).
|
|
24
|
+
# Such members are never listed, counted as pages, or extracted.
|
|
25
|
+
def macos_metadata?
|
|
26
|
+
path.split('/').any? { it == '__MACOSX' || it.start_with?('._') }
|
|
27
|
+
end
|
|
19
28
|
end
|
|
20
29
|
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
require 'comicinfo'
|
|
2
|
+
require 'fileutils'
|
|
3
|
+
require 'tmpdir'
|
|
4
|
+
|
|
5
|
+
class ComicBook
|
|
6
|
+
# Makes a folder's page images exactly the given files, in the given order.
|
|
7
|
+
#
|
|
8
|
+
# See ComicBook#arrange_pages, which this does the work for.
|
|
9
|
+
class PageArranger
|
|
10
|
+
# An issue with no details, for filling in a template when the folder has no ComicInfo.xml.
|
|
11
|
+
BLANK_ISSUE_XML = '<ComicInfo/>'.freeze
|
|
12
|
+
|
|
13
|
+
def initialize folder
|
|
14
|
+
@folder = File.realpath folder
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def arrange sources, template:
|
|
18
|
+
previous_pages = comic.pages.map(&:path)
|
|
19
|
+
resolved = sources.map { resolve it, previous_pages }
|
|
20
|
+
files = resolved.map { it[:path] }
|
|
21
|
+
order = resolved.map { it[:previous_index] }
|
|
22
|
+
|
|
23
|
+
Dir.mktmpdir 'comicbook-arrange' do |staging|
|
|
24
|
+
staged = stage files, staging, template
|
|
25
|
+
replace_pages previous_pages, staged
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
update_info order
|
|
29
|
+
comic.pages
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
attr_reader :folder
|
|
35
|
+
|
|
36
|
+
# Delete the pages the folder had, and move the staged ones into where they were.
|
|
37
|
+
def replace_pages previous_pages, staged
|
|
38
|
+
previous_pages.each { FileUtils.rm File.join(folder, it) }
|
|
39
|
+
|
|
40
|
+
pages_directory = File.join folder, shared_directory(previous_pages)
|
|
41
|
+
FileUtils.mkdir_p pages_directory
|
|
42
|
+
staged.each { FileUtils.mv it, File.join(pages_directory, File.basename(it)) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def comic = ComicBook.new(folder)
|
|
46
|
+
|
|
47
|
+
def info_path = File.join(folder, 'ComicInfo.xml')
|
|
48
|
+
|
|
49
|
+
# Where a source is on disk, and which page it was before (nil for a file new to the comic).
|
|
50
|
+
def resolve source, previous_pages
|
|
51
|
+
path = source.start_with?('/') ? source : File.join(folder, source)
|
|
52
|
+
|
|
53
|
+
raise Error, "Path does not exist: #{path}" unless File.exist? path
|
|
54
|
+
raise Error, "Unsupported file type: #{File.extname(path)}" unless Entry.new(path).image?
|
|
55
|
+
|
|
56
|
+
path = File.realpath path
|
|
57
|
+
folder_prefix = "#{folder}/"
|
|
58
|
+
previous_index = previous_pages.index(path.delete_prefix(folder_prefix)) if path.start_with? folder_prefix
|
|
59
|
+
|
|
60
|
+
{ path:, previous_index: }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Copy the files into the staging folder under their new page names, in order.
|
|
64
|
+
def stage files, staging, template
|
|
65
|
+
issue = File.exist?(info_path) ? ComicInfo.load(info_path) : ComicInfo.load(BLANK_ISSUE_XML)
|
|
66
|
+
|
|
67
|
+
files.each_with_index.map do |file, index|
|
|
68
|
+
name = issue.page_filename template:, page: index + 1, of: files.length
|
|
69
|
+
destination = File.join staging, "#{name}#{File.extname(file)}"
|
|
70
|
+
|
|
71
|
+
FileUtils.cp file, destination
|
|
72
|
+
destination
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The deepest directory that contains every one of the paths, relative like them ("" for the top).
|
|
77
|
+
def shared_directory paths
|
|
78
|
+
directories = paths.map { it.split('/')[0...-1] }
|
|
79
|
+
shared = directories.first || []
|
|
80
|
+
|
|
81
|
+
directories.drop(1).each do |directory|
|
|
82
|
+
shared = shared.zip(directory).take_while { |ours, theirs| ours == theirs }.map(&:first)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
shared.join '/'
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Bring the folder's ComicInfo.xml (if any) in step with the rearranged pages.
|
|
89
|
+
def update_info order
|
|
90
|
+
return unless File.exist? info_path
|
|
91
|
+
|
|
92
|
+
ComicInfo.load(info_path).rearranging_pages(order).save info_path
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
class ComicBook
|
|
2
|
+
# Tells what kind of file a comic is from the bytes it starts with.
|
|
3
|
+
#
|
|
4
|
+
# Comic files are often mislabelled (a RAR named `.cbz`),
|
|
5
|
+
# so what the file is matters more than what it is called.
|
|
6
|
+
class Signature
|
|
7
|
+
# How many leading bytes are enough to recognize any format
|
|
8
|
+
# (TAR's marker ends at byte 262).
|
|
9
|
+
LENGTH = 262
|
|
10
|
+
|
|
11
|
+
ZIP_FILE_HEADER = "PK\x03\x04".b.freeze
|
|
12
|
+
ZIP_END_RECORD = "PK\x05\x06".b.freeze # an empty archive starts with this
|
|
13
|
+
RAR = "Rar!\x1A\x07".b.freeze # versions 4 and 5
|
|
14
|
+
SEVEN_ZIP = "7z\xBC\xAF\x27\x1C".b.freeze
|
|
15
|
+
PDF = '%PDF-'.b.freeze
|
|
16
|
+
ACE = '**ACE**'.b.freeze
|
|
17
|
+
ACE_OFFSET = 7
|
|
18
|
+
TAR = 'ustar'.b.freeze
|
|
19
|
+
TAR_OFFSET = 257
|
|
20
|
+
|
|
21
|
+
def initialize path
|
|
22
|
+
@path = path
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The type the file's contents say it is (:cbz, :cbr, :cb7, :pdf, :cba, :cbt).
|
|
26
|
+
#
|
|
27
|
+
# Nil when the file can't be read or starts like none of the formats;
|
|
28
|
+
# an old TAR with no `ustar` marker is one such file.
|
|
29
|
+
def type
|
|
30
|
+
return :cbz if zip?
|
|
31
|
+
return :cbr if starts_with? RAR
|
|
32
|
+
return :cb7 if starts_with? SEVEN_ZIP
|
|
33
|
+
return :pdf if starts_with? PDF
|
|
34
|
+
return :cba if marker? ACE, at: ACE_OFFSET
|
|
35
|
+
return :cbt if marker? TAR, at: TAR_OFFSET
|
|
36
|
+
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
attr_reader :path
|
|
43
|
+
|
|
44
|
+
def zip? = starts_with?(ZIP_FILE_HEADER) || starts_with?(ZIP_END_RECORD)
|
|
45
|
+
|
|
46
|
+
def starts_with? marker
|
|
47
|
+
marker? marker, at: 0
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def marker? marker, at:
|
|
51
|
+
bytes.byteslice(at, marker.bytesize) == marker
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def bytes
|
|
55
|
+
@bytes ||= File.binread(path, LENGTH) || ''.b
|
|
56
|
+
rescue SystemCallError
|
|
57
|
+
@bytes = ''.b
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
data/lib/comic_book/version.rb
CHANGED
data/lib/comicbook.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require_relative 'comic_book/version'
|
|
2
2
|
require_relative 'comic_book/page'
|
|
3
|
+
require_relative 'comic_book/page_arranger'
|
|
3
4
|
require_relative 'comic_book/entry'
|
|
4
5
|
require_relative 'comic_book/cb'
|
|
5
6
|
require_relative 'comic_book/cb7'
|
|
@@ -8,6 +9,7 @@ require_relative 'comic_book/cbr'
|
|
|
8
9
|
require_relative 'comic_book/cbt'
|
|
9
10
|
require_relative 'comic_book/cbz'
|
|
10
11
|
require_relative 'comic_book/pdf'
|
|
12
|
+
require_relative 'comic_book/signature'
|
|
11
13
|
require_relative 'comic_book/cli_helpers'
|
|
12
14
|
|
|
13
15
|
class ComicBook
|
|
@@ -21,6 +23,19 @@ class ComicBook
|
|
|
21
23
|
# Metadata sidecar files (used by the :images_and_info file selection).
|
|
22
24
|
INFO_FILENAMES = %w[ComicInfo.xml MetronInfo.xml].freeze
|
|
23
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
|
+
|
|
24
39
|
attr_reader :path, :type
|
|
25
40
|
|
|
26
41
|
def initialize path
|
|
@@ -65,6 +80,35 @@ class ComicBook
|
|
|
65
80
|
|
|
66
81
|
def info = adapter.info
|
|
67
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:
|
|
110
|
+
end
|
|
111
|
+
|
|
68
112
|
# The files in this comicbook, filtered by type:
|
|
69
113
|
# :all (default),
|
|
70
114
|
# :images,
|
|
@@ -101,23 +145,29 @@ class ComicBook
|
|
|
101
145
|
if File.directory? path
|
|
102
146
|
File.extname(path).downcase == '.cb' ? :cb : :folder
|
|
103
147
|
elsif File.file? path
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
when '.cbr' then :cbr
|
|
110
|
-
when '.cbt' then :cbt
|
|
111
|
-
when '.cbz' then :cbz
|
|
112
|
-
when '.pdf' then :pdf
|
|
113
|
-
else
|
|
114
|
-
raise Error, "Unsupported file type: #{File.extname(path)}"
|
|
115
|
-
end
|
|
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
|
|
116
153
|
else
|
|
117
154
|
raise Error, "Path does not exist: #{path}"
|
|
118
155
|
end
|
|
119
156
|
end
|
|
120
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
|
+
|
|
121
171
|
def filter_files entries, by:
|
|
122
172
|
case by
|
|
123
173
|
when :all then entries
|
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.
|
|
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.
|
|
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.
|
|
25
|
+
version: '2.1'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: ruby-vips
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -105,8 +105,10 @@ files:
|
|
|
105
105
|
- lib/comic_book/cli_helpers.rb
|
|
106
106
|
- lib/comic_book/entry.rb
|
|
107
107
|
- lib/comic_book/page.rb
|
|
108
|
+
- lib/comic_book/page_arranger.rb
|
|
108
109
|
- lib/comic_book/pdf.rb
|
|
109
110
|
- lib/comic_book/pdf/extractor.rb
|
|
111
|
+
- lib/comic_book/signature.rb
|
|
110
112
|
- lib/comic_book/vendor/linux/MPL-License-for-lsar-and-unar.txt
|
|
111
113
|
- lib/comic_book/vendor/macos/MPL-License-for-lsar-and-unar.txt
|
|
112
114
|
- lib/comic_book/vendor/macos/lsar
|
|
@@ -138,14 +140,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
138
140
|
requirements:
|
|
139
141
|
- - ">="
|
|
140
142
|
- !ruby/object:Gem::Version
|
|
141
|
-
version: 4.0.
|
|
143
|
+
version: 4.0.9
|
|
142
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
145
|
requirements:
|
|
144
146
|
- - ">="
|
|
145
147
|
- !ruby/object:Gem::Version
|
|
146
148
|
version: '0'
|
|
147
149
|
requirements: []
|
|
148
|
-
rubygems_version: 4.0.
|
|
150
|
+
rubygems_version: 4.0.20
|
|
149
151
|
specification_version: 4
|
|
150
152
|
summary: Ruby interface for working with comic book archive files
|
|
151
153
|
test_files: []
|