├── .gitignore ├── lib ├── embed-assets-rails │ ├── version.rb │ ├── railtie.rb │ └── processor.rb └── embed-assets-rails.rb ├── test ├── fixtures │ ├── public │ │ └── images │ │ │ ├── test.gif │ │ │ └── embed │ │ │ └── testembed.gif │ ├── test.css │ └── test_embedded.css ├── test_helper.rb └── unit │ └── embed_assets_rails_test.rb ├── Rakefile ├── Gemfile ├── README.md ├── LICENCE └── embed-assets-rails.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /lib/embed-assets-rails/version.rb: -------------------------------------------------------------------------------- 1 | module Saulabs 2 | module EmbedAssets 3 | VERSION = "0.9.3" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/fixtures/public/images/test.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saulabs/embed-assets-rails/HEAD/test/fixtures/public/images/test.gif -------------------------------------------------------------------------------- /test/fixtures/public/images/embed/testembed.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saulabs/embed-assets-rails/HEAD/test/fixtures/public/images/embed/testembed.gif -------------------------------------------------------------------------------- /test/fixtures/test.css: -------------------------------------------------------------------------------- 1 | div { 2 | background: url(/images/embed/testembed.gif); 3 | } 4 | 5 | div { 6 | background: url(/images/test.jpg); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /lib/embed-assets-rails.rb: -------------------------------------------------------------------------------- 1 | 2 | module Saulabs 3 | module EmbedAssets 4 | end 5 | end 6 | 7 | require 'embed-assets-rails/processor' 8 | require 'embed-assets-rails/railtie' 9 | require 'embed-assets-rails/version' 10 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | 4 | require 'rake/testtask' 5 | 6 | Rake::TestTask.new(:test) do |t| 7 | t.libs << 'lib' 8 | t.libs << 'test' 9 | t.pattern = 'test/**/*_test.rb' 10 | t.verbose = false 11 | end 12 | 13 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in sass-rails.gemspec 4 | gemspec 5 | 6 | gem "rails", :git => "git://github.com/rails/rails.git" 7 | gem "sprockets", :git => "git://github.com/sstephenson/sprockets.git" 8 | 9 | # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+) 10 | # gem 'ruby-debug' 11 | gem 'ruby-debug19' 12 | 13 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | 3 | require 'test/unit' 4 | require 'pathname' 5 | require 'uri' 6 | require 'base64' 7 | 8 | FIXTURE_PATH = "#{File.dirname(__FILE__)}/fixtures" 9 | 10 | module Saulabs 11 | module EmbedAssets 12 | end 13 | end 14 | 15 | class Rails 16 | def self.public_path 17 | "#{FIXTURE_PATH}/public" 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/embed-assets-rails/railtie.rb: -------------------------------------------------------------------------------- 1 | module Saulabs::EmbedAssets 2 | class Railtie < ::Rails::Railtie 3 | 4 | initializer "saulabs.sprockets.embed_assets", :after => 'sprockets.environment' do |app| 5 | app.config.assets.embed_assets = false if app.config.assets.embed_assets.nil? 6 | app.assets.register_bundle_processor 'text/css', Saulabs::EmbedAssets::Processor 7 | end 8 | 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /test/unit/embed_assets_rails_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | require 'embed-assets-rails/processor' 3 | 4 | module Saulabs::EmbedAssets 5 | class Processor 6 | def embed_assets_enabled? 7 | true 8 | end 9 | end 10 | end 11 | 12 | class EmbedAssetsProcessorTest < Test::Unit::TestCase 13 | 14 | include Saulabs::EmbedAssets 15 | 16 | def test_classes_are_loaded 17 | assert_kind_of Module, Saulabs::EmbedAssets 18 | assert_kind_of Class, Saulabs::EmbedAssets::Processor 19 | end 20 | 21 | def test_embedding_assets 22 | expected_css = File.open("#{FIXTURE_PATH}/test_embedded.css", 'r') {|f| f.read } 23 | result_css = Processor.new("#{FIXTURE_PATH}/test.css").render 24 | 25 | assert_equal expected_css, result_css 26 | end 27 | 28 | end 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Embedding Assets 2 | 3 | This gem provides integration of embedding images and fonts into CSS files within the Rails asset pipeline. 4 | More about embedding assets and how it works, see the [jammit documentation][1]. This gem actually reuses code from that project. 5 | 6 | [1]: http://documentcloud.github.com/jammit/#embedding 7 | 8 | ## Installing 9 | 10 | Add the following to your Gemfile: 11 | 12 | gem 'embed-assets-rails' 13 | 14 | ## Configuration 15 | 16 | To enable embedding the assets set `config.assets.embed_assets` to `true`. 17 | 18 | ### Example 19 | 20 | MyProject::Application.configure do 21 | config.assets.embed_assets = true 22 | end 23 | 24 | ## Notes 25 | 26 | * Rails 3.1 and up only 27 | * Only supports images and font in public/ directory (yet) 28 | 29 | ## Thanks 30 | 31 | Thanks to documentcloud for creating [jammit][2]. 32 | 33 | [2]: http://documentcloud.github.com/jammit/ 34 | 35 | ## Running Tests 36 | 37 | $ bundle install 38 | $ bundle exec rake test 39 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 Jeremy Ashkenas, DocumentCloud 2 | Copyright (c) 2011 Dieter Komendera, abloom OG 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /embed-assets-rails.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "embed-assets-rails/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "embed-assets-rails" 7 | s.version = Saulabs::EmbedAssets::VERSION 8 | s.platform = Gem::Platform::RUBY 9 | s.authors = ["kommen"] 10 | s.email = ["dieter@komendera.com"] 11 | s.homepage = "http://www.saulabs.net/" 12 | s.summary = %q{Embed images and fonts in CSS files with the Rails asset pipeline } 13 | s.description = %q{This gem provides integration of embedding images and fonts as base64 into CSS files within the Rails asset pipeline. 14 | More about embedding assets and how it works, see the [jammit documentation][1]. This gem actually reuses code from that project.} 15 | 16 | s.rubyforge_project = "embed-assets-rails" 17 | 18 | s.add_runtime_dependency 'railties', '~> 3.1.0.rc4' 19 | s.add_runtime_dependency 'actionpack', '~> 3.1.0.rc4' 20 | 21 | s.files = `git ls-files`.split("\n") 22 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 23 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 24 | s.require_paths = ["lib"] 25 | end 26 | -------------------------------------------------------------------------------- /lib/embed-assets-rails/processor.rb: -------------------------------------------------------------------------------- 1 | require 'tilt' 2 | 3 | module Saulabs::EmbedAssets 4 | 5 | class Processor < Tilt::Template 6 | def prepare 7 | end 8 | 9 | def evaluate(context, locals, &block) 10 | @asset_contents = {} 11 | embed_assets_enabled? ? with_data_uris(data) : data 12 | end 13 | 14 | def embed_assets_enabled? 15 | Rails.application.config.assets.embed_assets 16 | end 17 | 18 | protected 19 | 20 | # Mapping from extension to mime-type of all embeddable assets. 21 | EMBED_MIME_TYPES = { 22 | '.png' => 'image/png', 23 | '.jpg' => 'image/jpeg', 24 | '.jpeg' => 'image/jpeg', 25 | '.gif' => 'image/gif', 26 | '.tif' => 'image/tiff', 27 | '.tiff' => 'image/tiff', 28 | '.ttf' => 'font/truetype', 29 | '.otf' => 'font/opentype', 30 | '.woff' => 'font/woff' 31 | } 32 | 33 | # Font extensions for which we allow embedding: 34 | EMBED_EXTS = EMBED_MIME_TYPES.keys 35 | EMBED_FONTS = ['.ttf', '.otf', '.woff'] 36 | 37 | # (32k - padding) maximum length for data-uri assets (an IE8 limitation). 38 | MAX_IMAGE_SIZE = 32700 39 | 40 | # CSS asset-embedding regexes for URL rewriting. 41 | EMBED_DETECTOR = /url\(['"]?([^\s)]+\.[a-z]+)(\?\d+)?['"]?\)/ 42 | EMBEDDABLE = /[\A\/]embed\// 43 | 44 | def with_data_uris(css) 45 | css.gsub(EMBED_DETECTOR) do |url| 46 | asset_path = Pathname.new($1) 47 | public_path = absolute_path(asset_path) 48 | if URI.parse($1).absolute? || !embeddable?(public_path) 49 | "url(#{$1})" 50 | else 51 | "url(\"data:#{mime_type($1)};charset=utf-8;base64,#{encoded_contents(public_path.to_s)}\")" 52 | end 53 | end 54 | end 55 | 56 | # An asset is valid for embedding if it exists, is less than 32K, and is 57 | # stored somewhere inside of a folder named "embed". IE does not support 58 | # Data-URIs larger than 32K, and you probably shouldn't be embedding assets 59 | # that large in any case. Because we need to check the base64 length here, 60 | # save it so that we don't have to compute it again later. 61 | def embeddable?(asset_path) 62 | font = EMBED_FONTS.include?(asset_path.extname) 63 | return false unless asset_path.to_s.match(EMBEDDABLE) && asset_path.exist? 64 | return false unless EMBED_EXTS.include?(asset_path.extname) 65 | return false unless font || encoded_contents(asset_path).length < MAX_IMAGE_SIZE 66 | return true 67 | end 68 | 69 | # Return the Base64-encoded contents of an asset on a single line. 70 | def encoded_contents(asset_path) 71 | return @asset_contents[asset_path] if @asset_contents[asset_path] 72 | data = read_binary_file(asset_path) 73 | @asset_contents[asset_path] = Base64.encode64(data).gsub(/\n/, '') 74 | end 75 | 76 | def absolute_path(asset_pathname) 77 | Pathname.new(File.join(Rails.public_path, asset_pathname)) 78 | end 79 | 80 | # Grab the mime-type of an asset, by filename. 81 | def mime_type(asset_path) 82 | EMBED_MIME_TYPES[File.extname(asset_path)] 83 | end 84 | 85 | # `File.read`, but in "binary" mode. 86 | def read_binary_file(path) 87 | File.open(path, 'rb') {|f| f.read } 88 | end 89 | 90 | end 91 | end 92 | 93 | -------------------------------------------------------------------------------- /test/fixtures/test_embedded.css: -------------------------------------------------------------------------------- 1 | div { 2 | background: url("data:image/gif;charset=utf-8;base64,R0lGODlhlAA1APf/ANLe7jMaBs3a7JSt1aS53K7B1TIYBKvA3tbj8XyOrdLg8DQcB7bJ3kQqEezx+cbY7MHL1bG5wb/T6crZ6tnl8lE6HzoiDOrv+U42HMrW6sTW69rl8rTBzt/p9DYeCcLQ59Df783d7rPJ4cXS6DgeCdzm86m7zUAlDpKUjZqx18LW67zN5tHg70QsFF1FKNrm8sHU6kovFuDo9DogCjwlDrXG4uDq9cDQ6DggC8/e76e1xc7c7LrQ5z8lDTQaBsbT6EIrE7rO5s7d7lhAJEM5NjAWA0IoELvQ6Obt+OXt9uTr9zk1Nj4nENfj8kw1G9Th8FY/I0YvFkkuFYKWucHV6kAoEVI7ICkSALC8yBoLANHd7cjZ7MXX62hRMr/O5J202FQ8IUoyGUEnD0kxGLfE0ujv+CEOACsuNj8oEVtDJkkyGTskDU83HV9IKmBJK5+02FU+I11mdy4VAS4WAi8XAzAYBDEZBLbN5i0UAS8WAi4VAltOQDUdCDEYBDcfCj0jDDMaBUApEQsFADwiC8vb7T4mD0szGjcdCJ+320UuFllCJrfO5srb7bHG3LjO57nP58zc7eL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1RgFEcthJIJ/g99yCMFSE96CvzhjwE4/elO7wc9SouRJIp1SKVIxw9GwPWuhyMcHwi72D9wDnSU4g/qNIAX8ToH5ACYCS0IFhuyvvWuc/3rYxd72UvBBmm1oGJK3k6oymGGLAji8IhPvOIPnwUzHPHaPprTIBawboXgYQHIAxSr+9tu+1igEL+atRiuEZFriAHY1CqEmpQaw72U4wq0MIPsZ0/72ssedo+/ptVLZTtUqUoKMUiG8IefjBioS1a08hnzPDLToTEGP6GIAqPAULA2uMMa3+iC9rX/DWu4ow0QAwP/pqIQCgI1ZgHZCt1lSGWqlOOprH6Lf1mNMCu010p5uBqlBnt1yijMugKaMARp4AKX0wYGCH4ukAZDoAkVAGxRsEvWUmiG9iYn8yCAsAATIiQp1wN2cgJ8woFodySHACga8jOVt1vtRjRU0n9qwCVWMCxQAAXrMATrEIPQcilOoAYPyARpcl3Gpn64kRnkgR6GQSFBMgNIeCRGeCGC4x6fcYLs1m5ysUGIkjgpsiUYwAZauIV9JyYvwjmccl3ZkT6i8g+ZMR504CPnkR4RsgBuuABseB5NyCRQuFuFkhxUiB+BEAotcAlRkCIqogYu8iJRcAktEApaYx3YMRmgIlll+GiGPMIbaegbBlCJlegb7eEedBgRT/I8hYEaVJIf0bGHQFCKpYiIAoIm1rEmjGgbj0gQf+EZnDGLtNgZw6EvECEaLscr90EiVVIIwBiM1FEgijEbvtR6QPiKB/EXIyEfg1Ea9ZEY3pAaq7Ea1VGNqeENsiEZL8QdWKGMl+GMUEIfpjEXiOEYiwEZxvhCtZEgGgOOaiEfbkEa0Ggab0gXb8iNtFEVjegm8IgbbfEVUxEXBLmP/IgW7/iPa9EWTjEa9EgVEHmQR6EgyaiQC8mQRkEUGnkWV6EXFWmR4dgUM2EUHTkTOQGS4KgSKqmSKNmSKGgZLhmTOhEQADs="); 3 | } 4 | 5 | div { 6 | background: url(/images/test.jpg); 7 | } 8 | 9 | --------------------------------------------------------------------------------