dataurl 0.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +42 -0
  3. data/bin/dataurl +39 -0
  4. data/lib/dataurl.rb +26 -0
  5. metadata +54 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3d5342e9845645526e98b529e9d3191d1e9bc513bb19f6badae43467ffdb4243
4
+ data.tar.gz: 8d89250fd1afae5db25431f78efb81f992c4b81e056fbf1039c1c5609e948998
5
+ SHA512:
6
+ metadata.gz: 3d1ba7fba0417a76e2ca1801ee656d436203376ca3db06e66355610ec6a36538c5815b8229258a3f54cf5c6bc2ea3e4e77f162ddcade4f7eb0fb97485175cd9b
7
+ data.tar.gz: 790258065ea5118186a4f56c28d0d5cdd4e900055bd345a7dad97d694e8e91ce148ffb955132f2c50a0b85c70b54eb3ed90b03ee858af0957c11cbe7db1523ad
@@ -0,0 +1,42 @@
1
+ = DataURL: Generate data URLs for use in HTML, CSS, etc.
2
+
3
+ DataURL is an RFC 2397-compliant data URL generator. It consists of both a small
4
+ library and a command-line utility.
5
+
6
+ == Command-Line Usage
7
+
8
+ $ dataurl myfile.txt
9
+ $ dataurl -m text/plain myfile
10
+ $ echo Hello | dataurl -m text/plain
11
+
12
+ == Library Usage
13
+
14
+ require 'dataurl'
15
+
16
+ # Figure out the mime type based on the file name.
17
+ mime_type = DataURL.guess_mime(filename)
18
+
19
+ # Generate the data URL.
20
+ DataURL.generate(mime_type: mime_type, content: IO.binread(filename))
21
+
22
+ == Copyright and License
23
+
24
+ Copyright (C) 2020 Megan Ruggiero
25
+
26
+ Permission is hereby granted, free of charge, to any person obtaining a copy
27
+ of this software and associated documentation files (the "Software"), to deal
28
+ in the Software without restriction, including without limitation the rights
29
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30
+ copies of the Software, and to permit persons to whom the Software is
31
+ furnished to do so, subject to the following conditions:
32
+
33
+ The above copyright notice and this permission notice shall be included in all
34
+ copies or substantial portions of the Software.
35
+
36
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42
+ SOFTWARE.
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/dataurl'
5
+ require 'optparse'
6
+
7
+ mime_type = nil
8
+ chomp = false
9
+
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: #{$PROGRAM_NAME} [FILENAME]"
12
+
13
+ opts.on('-mMIME', '--mime-type=MIME',
14
+ 'Set the MIME type (default: autodetect)') do |value|
15
+ mime_type = value
16
+ end
17
+
18
+ opts.on('-c', '--chomp',
19
+ 'Chomp the final newline (default: disabled)') do |value|
20
+ chomp = value
21
+ end
22
+ end.parse!
23
+
24
+ filename = ARGV[0]
25
+
26
+ if mime_type.nil?
27
+ begin
28
+ if filename.nil? || (mime_type = DataURL.guess_mime(filename)).nil?
29
+ abort('Failed to guess MIME type. Please specify with the -m option.')
30
+ end
31
+ rescue Errno::ENOENT
32
+ abort('/etc/mime.types does not exist. Please specify the type with -m.')
33
+ end
34
+ end
35
+
36
+ content = filename.nil? ? $stdin.read : IO.binread(filename)
37
+ content.chomp! if chomp
38
+
39
+ puts(DataURL.generate(mime_type: mime_type, content: content))
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Utilities for generating data URLs for use in HTML, CSS, etc.
4
+ module DataURL
5
+ # Generate a data URL.
6
+ # @param mime_type [String] media type of the data to encode
7
+ # @param content [String] content to encode
8
+ # @return [String] RFC 2397-compliant data URL.
9
+ def self.generate(mime_type:, content:)
10
+ "data:#{mime_type};base64,#{[content].pack('m0')}"
11
+ end
12
+
13
+ # Guess the MIME type of a file using mailcap's /etc/mime.types.
14
+ # @param filename [String] filename to check against mime.types
15
+ # @return [String] media type or nil if it could not be detected
16
+ def self.guess_mime(filename)
17
+ extension = File.extname(filename)[1..-1].downcase
18
+
19
+ File.foreach('/etc/mime.types') do |line|
20
+ match = /\A([^#\s]+)\s+([^#]+)/.match(line)
21
+ return match[1] if match && match[2].split.include?(extension)
22
+ end
23
+
24
+ nil
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dataurl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Megan Ruggiero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ DataURL is an RFC 2397-compliant data URL generator. It consists of both a
15
+ small library and a command-line utility.
16
+ email:
17
+ executables:
18
+ - dataurl
19
+ extensions: []
20
+ extra_rdoc_files:
21
+ - README.rdoc
22
+ files:
23
+ - README.rdoc
24
+ - bin/dataurl
25
+ - lib/dataurl.rb
26
+ homepage: https://github.com/decadentsoup/dataurl
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ bug_tracker_uri: https://github.com/decadentsoup/dataurl/issues
31
+ documentation_uri: https://www.rubydoc.info/gems/dataurl
32
+ homepage_uri: https://github.com/decadentsoup/dataurl
33
+ source_code_uri: https://github.com/decadentsoup/dataurl
34
+ wiki_uri: https://github.com/decadentsoup/dataurl/wiki
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.1.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Generate data URLs for use in HTML, CSS, etc.
54
+ test_files: []