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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca7e283f22402dc2a4e9ea4eb15c2ac3da7b2e13d5a320ff5858e7b986b63466
4
- data.tar.gz: 3519b1b05c1625ce9a8d29bc9bcc2fa19cb5d58cdd6af2a91f324fddab03252b
3
+ metadata.gz: ce6edcb779358d5670da7149c518ccaedd23746a87a712b743a4d013bbb57f3a
4
+ data.tar.gz: 61eeb0de33e7b8b971187e04e5176f5fee498c83589f1d3b20ce42b91da1eebd
5
5
  SHA512:
6
- metadata.gz: cf19d5137d8894d23d9307902711c9561fb8b0b644506713afdddd75b44892ff8f8548a266d247187ddaf8f59f9957c75209fece1dfaf1e24b5240eccb53a148
7
- data.tar.gz: 907361482cb74018e0d98ce29e755c930bced812f6a2754d6da8268f37c7fc23bbf0195595ae4cbf930ab7abf26b7b641bc46d80eb97346296bb016f8aacac0e
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, PNG bytes or a path) and opens it |
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 |
@@ -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
- # PNG bytes, or the path of an image file.
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 the PNG signature or NUL bytes, naming a file
88
- if image.is_a?(String) && !image.b.start_with?("\x89PNG".b) && !image.include?("\0") && File.file?(image)
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.b
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChunkyBacon
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chunky_bacon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andi Idogawa