Class: Juicer::ImageEmbed
- Inherits:
-
Object
- Object
- Juicer::ImageEmbed
- Includes:
- Chainable
- Defined in:
- lib/juicer/image_embed.rb
Overview
The ImageEmbed is a tool that can parse a CSS file and substitute all referenced URLs by a data uri
-
data uri (en.wikipedia.org/wiki/Data_URI_scheme)
Only local resources will be processed this way, external resources referenced by absolute urls will be left alone
Constant Summary collapse
- SIZE_LIMIT =
The maximum supported limit for modern browsers, See the Readme.rdoc for details
32768
Instance Method Summary collapse
- #embed_data_uri(path) ⇒ Object
-
#initialize(options = {}) ⇒ ImageEmbed
constructor
A new instance of ImageEmbed.
-
#save(file, output = nil) ⇒ Object
Update file.
-
#size_limit ⇒ Object
Returns the size limit.
-
#urls(file) ⇒ Object
Returns all referenced URLs in
file
.
Methods included from Chainable
included, #next_in_chain, #next_in_chain=
Constructor Details
#initialize(options = {}) ⇒ ImageEmbed
Returns a new instance of ImageEmbed.
28 29 30 31 32 33 34 35 36 |
# File 'lib/juicer/image_embed.rb', line 28 def initialize( = {}) @document_root = [:document_root] @document_root.sub!(%r{/?$}, "") if @document_root # Remove trailing slash @type = [:type] || :none @contents = nil @hosts = [:hosts] @path_resolver = Juicer::Asset::PathResolver.new(:document_root => [:document_root], :hosts => [:hosts]) end |
Instance Method Details
#embed_data_uri(path) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/juicer/image_embed.rb', line 86 def ( path ) new_path = path supported_file_matches = path.match( /(?:\.)(png|gif|jpg|jpeg)(?:\?embed=true)$/i ) filetype = supported_file_matches[1] if supported_file_matches if ( filetype ) filename = path.gsub('?embed=true','') # check if file exists, throw an error if it doesn't exist if File.exist?( filename ) # read contents of file into memory content = File.read( filename ) content_type = "image/#{filetype}" # encode the url new_path = Datafy::make_data_uri( content, content_type ) else puts "Unable to locate file #{filename} on local file system, skipping image embedding" end end return new_path end |
#save(file, output = nil) ⇒ Object
Update file. If no output
is provided, the input file is overwritten
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/juicer/image_embed.rb', line 41 def save(file, output = nil) return unless @type == :data_uri output_file = output || file @contents = File.read(file) used = [] @path_resolver = Juicer::Asset::PathResolver.new(:document_root => @document_root, :hosts => @hosts, :base => File.dirname(file)) assets = urls(file) # TODO: Remove "?embed=true" from duplicate urls duplicates = duplicate_urls(assets) if duplicates.length > 0 Juicer::LOGGER.warn("Duplicate image urls detected, these images will not be embedded: #{duplicates.collect { |v| v.gsub('?embed=true', '') }.inspect}") end assets.each do |asset| begin next if used.include?(asset) || duplicates.include?(asset.path) used << asset # make sure we do not exceed SIZE_LIMIT new_path = (asset.filename) if new_path.length < SIZE_LIMIT # replace the url in the css file with the data uri @contents.gsub!(asset.path, (asset.filename)) if asset.filename.match( /\?embed=true$/ ) else Juicer::LOGGER.warn("The final data uri for the image located at #{asset.path.gsub('?embed=true', '')} exceeds #{SIZE_LIMIT} and will not be embedded to maintain compatability.") end rescue Errno::ENOENT puts "Unable to locate file #{asset.path}, skipping image embedding" end end File.open(output || file, "w") { |f| f.puts @contents } @contents = nil end |
#size_limit ⇒ Object
Returns the size limit
24 25 26 |
# File 'lib/juicer/image_embed.rb', line 24 def size_limit SIZE_LIMIT end |
#urls(file) ⇒ Object
Returns all referenced URLs in file
.
114 115 116 117 118 119 120 |
# File 'lib/juicer/image_embed.rb', line 114 def urls(file) @contents = File.read(file) unless @contents @contents.scan(/url\([\s"']*([^\)"'\s]*)[\s"']*\)/m).collect do |match| @path_resolver.resolve(match.first) end end |