chunky_bacon 0.1.2 → 0.1.3
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/CHANGELOG.md +6 -0
- data/README.md +1 -1
- data/lib/chunky_bacon/helpers.rb +19 -6
- data/lib/chunky_bacon/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ce6edcb779358d5670da7149c518ccaedd23746a87a712b743a4d013bbb57f3a
|
|
4
|
+
data.tar.gz: 61eeb0de33e7b8b971187e04e5176f5fee498c83589f1d3b20ce42b91da1eebd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e0e229104f116c447bde017803a62e27f20bb6b439715eb130e5401e83cdf8db92f994902aa0bb5dbc618f8f930cd356242dbc518f3804f97d5c454e8443f9d2
|
|
7
|
+
data.tar.gz: 95e9ad045d5fbcd2318294847e2ee9201ce006ce27dca1be32d2c512aa442e2da1defc56f5d3825130dfc9da5e2493e9a9a7c11acdd3ea5fda8e6bff2ae2519b
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.3
|
|
4
|
+
|
|
5
|
+
- `show_image` takes JPEGs, as the course's lesson 22 (pure_jpeg) makes
|
|
6
|
+
them: what `PureJPEG.encode` returns (anything with `to_bytes`), and the
|
|
7
|
+
bytes of a JPEG, GIF or WebP, saved with their own extension.
|
|
8
|
+
|
|
3
9
|
## 0.1.2
|
|
4
10
|
|
|
5
11
|
- The homepage is the course itself, https://chunkybacon.idogawa.com (also
|
data/README.md
CHANGED
|
@@ -33,7 +33,7 @@ never mix with your program's output.
|
|
|
33
33
|
| Helper | On your computer |
|
|
34
34
|
|---|---|
|
|
35
35
|
| `install_gem "name"` | installs the gem unless it is there, and activates it |
|
|
36
|
-
| `show_image image` | saves `chunky-image-N.png` (ChunkyPNG image,
|
|
36
|
+
| `show_image image` | saves `chunky-image-N.png` (or `.jpg`, `.gif`, `.webp`: a ChunkyPNG image, a PureJPEG encoder, the bytes or a path) and opens it |
|
|
37
37
|
| `show_pdf pdf` | saves `chunky-document-N.pdf` (Prawn/HexaPDF document, bytes or a path) and opens it |
|
|
38
38
|
| `download_file data, "name"` | saves the data as `name` in the program's folder |
|
|
39
39
|
| `show_browser App, "/path"` | starts the Rack app (Sinatra, Roda, ...) on 127.0.0.1 and opens it; after the program's last line the server runs on until Ctrl+C |
|
data/lib/chunky_bacon/helpers.rb
CHANGED
|
@@ -34,6 +34,16 @@ module ChunkyBacon
|
|
|
34
34
|
path
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
+
IMAGE_SIGNATURES = {
|
|
38
|
+
"\x89PNG".b => "png", "\xFF\xD8\xFF".b => "jpg", "GIF8".b => "gif", "RIFF".b => "webp"
|
|
39
|
+
}.freeze
|
|
40
|
+
|
|
41
|
+
# "png", "jpg", "gif" or "webp" for a picture's bytes, nil for anything else
|
|
42
|
+
def image_type(bytes)
|
|
43
|
+
bytes = bytes.b
|
|
44
|
+
IMAGE_SIGNATURES.find { |magic, _type| bytes.start_with?(magic) }&.last
|
|
45
|
+
end
|
|
46
|
+
|
|
37
47
|
def relative(path)
|
|
38
48
|
path = File.expand_path(path)
|
|
39
49
|
base = File.expand_path(output_dir) + File::SEPARATOR
|
|
@@ -82,10 +92,11 @@ module ChunkyBacon
|
|
|
82
92
|
end
|
|
83
93
|
|
|
84
94
|
# A picture: a ChunkyPNG image (anything with to_blob or to_data_url),
|
|
85
|
-
#
|
|
95
|
+
# what PureJPEG.encode returns (anything with to_bytes), the bytes of a
|
|
96
|
+
# PNG, JPEG, GIF or WebP, or the path of an image file.
|
|
86
97
|
def show_image(image)
|
|
87
|
-
# a path: text without
|
|
88
|
-
if image.is_a?(String) && !
|
|
98
|
+
# a path: text without a picture's signature or NUL bytes, naming a file
|
|
99
|
+
if image.is_a?(String) && !ChunkyBacon.image_type(image) && !image.include?("\0") && File.file?(image)
|
|
89
100
|
ChunkyBacon::Opener.open(File.expand_path(image))
|
|
90
101
|
return nil
|
|
91
102
|
end
|
|
@@ -93,10 +104,12 @@ module ChunkyBacon
|
|
|
93
104
|
image.to_blob
|
|
94
105
|
elsif image.respond_to?(:to_data_url)
|
|
95
106
|
image.to_data_url.split(",", 2).last.unpack1("m0")
|
|
107
|
+
elsif image.respond_to?(:to_bytes)
|
|
108
|
+
image.to_bytes
|
|
96
109
|
else
|
|
97
|
-
image.to_s
|
|
98
|
-
end
|
|
99
|
-
path = ChunkyBacon.save(ChunkyBacon.next_file("chunky-image", "png"), bytes)
|
|
110
|
+
image.to_s
|
|
111
|
+
end.b
|
|
112
|
+
path = ChunkyBacon.save(ChunkyBacon.next_file("chunky-image", ChunkyBacon.image_type(bytes) || "png"), bytes)
|
|
100
113
|
ChunkyBacon::Opener.open(path)
|
|
101
114
|
nil
|
|
102
115
|
end
|
data/lib/chunky_bacon/version.rb
CHANGED