Sha256: f4ede728d2cae4a63dca2fabed73de25e0864a9b817c685f9768d9796d767984

Contents?: true

Size: 807 Bytes

Versions: 1

Compression:

Stored size: 807 Bytes

Contents

# CSSHTTPRequest Ruby Encoder
#
# Author: Cameron Walters <cameron@nb.io>
# License: Apache License 2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
# Copyright (c) 2008, Cameron Walters

require "erb"

class CSSHTTPRequest
  PREFIX = "data:,".freeze
  LENGTH = (2000 - PREFIX.size).freeze # Internet Explorer 2KB URI limit

  def self.encode(str)
    quoted = ERB::Util.url_encode(str)
    slice_num = 0
    last_start = 0
    chunks = (quoted.length.to_f / LENGTH).ceil
    output = ""
    chunks.times do |n|
      finish = last_start + LENGTH
      output += "#c%d{background:url(%s%s);}\n" % [slice_num, PREFIX, quoted[last_start...finish]]
      slice_num += 1
      last_start = finish
    end
    output
  end  
end

if __FILE__ == $PROGRAM_NAME
  
  puts CSSHTTPRequest.encode(STDIN.read)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nbio-csshttprequest-1.0.3 lib/csshttprequest.rb