Module: Sprockets::EncodingUtils

### Home Classes Methods

### In Files

- lib/sprockets/encoding_utils.rb

### Methods

- #base64
- #charlock_detect
- #deflate
- #detect
- #detect_css
- #detect_html
- #detect_unicode
- #detect_unicode_bom
- #gzip
- #scan_css_charset
- #unmarshaled_deflated

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::EncodingUtils

Internal: HTTP transport encoding and charset detecting related functions. Mixed into Environment.

### Constants

BOM

  Internal: Mapping unicode encodings to byte order markers.

CHARSET_DETECT

  Internal: Shorthand aliases for detecter functions.

CHARSET_SIZE
CHARSET_START

  Internal: @charset bytes

### Public Instance Methods

base64(str) click to toggle source

Public: Use base64 to encode data.

str - String data

Returns a encoded String

    # File lib/sprockets/encoding_utils.rb, line 72
    def base64(str)
      Base64.strict_encode64(str)
    end

charlock_detect(str) click to toggle source

Internal: Use Charlock Holmes to detect encoding.

To enable this code path, require 'charlock_holmes'

Returns encoded String.

    # File lib/sprockets/encoding_utils.rb, line 121
    def charlock_detect(str)
      if defined? CharlockHolmes::EncodingDetector
        if detected = CharlockHolmes::EncodingDetector.detect(str)
          str.force_encoding(detected[:encoding]) if detected[:encoding]
        end
      end

      str
    end

deflate(str) click to toggle source

Public: Use deflate to compress data.

str - String data

Returns a compressed String

    # File lib/sprockets/encoding_utils.rb, line 18
    def deflate(str)
      deflater = Zlib::Deflate.new(
        Zlib::BEST_COMPRESSION,
        -Zlib::MAX_WBITS,
        Zlib::MAX_MEM_LEVEL,
        Zlib::DEFAULT_STRATEGY
      )
      deflater << str
      deflater.finish
    end

detect(str) click to toggle source

Public: Basic string detecter.

Attempts to parse any Unicode BOM otherwise falls back to the environment's external encoding.

str - ASCII-8BIT encoded String

Returns encoded String.

    # File lib/sprockets/encoding_utils.rb, line 99
    def detect(str)
      str = detect_unicode_bom(str)

      # Attempt Charlock detection
      if str.encoding == Encoding::BINARY
        charlock_detect(str)
      end

      # Fallback to environment's external encoding
      if str.encoding == Encoding::BINARY
        str.force_encoding(Encoding.default_external)
      end

      str
    end

detect_css(str) click to toggle source

Public: Detect and strip @charset from CSS style sheet.

str - String.

Returns a encoded String.

    # File lib/sprockets/encoding_utils.rb, line 177
    def detect_css(str)
      str = detect_unicode_bom(str)

      if name = scan_css_charset(str)
        encoding = Encoding.find(name)
        str = str.dup
        str.force_encoding(encoding)
        len = "@charset \"#{name}\";".encode(encoding).size
        str.slice!(0, len)
        str
      end

      # Fallback to UTF-8
      if str.encoding == Encoding::BINARY
        str.force_encoding(Encoding::UTF_8)
      end

      str
    end

detect_html(str) click to toggle source

Public: Detect charset from HTML document.

Attempts to parse any Unicode BOM otherwise attempt Charlock detection and finally falls back to the environment's external encoding.

str - String.

Returns a encoded String.

    # File lib/sprockets/encoding_utils.rb, line 244
    def detect_html(str)
      str = detect_unicode_bom(str)

      # Attempt Charlock detection
      if str.encoding == Encoding::BINARY
        charlock_detect(str)
      end

      # Fallback to environment's external encoding
      if str.encoding == Encoding::BINARY
        str.force_encoding(Encoding.default_external)
      end

      str
    end

detect_unicode(str) click to toggle source

Public: Detect Unicode string.

Attempts to parse Unicode BOM and falls back to UTF-8.

str - ASCII-8BIT encoded String

Returns encoded String.

    # File lib/sprockets/encoding_utils.rb, line 138
    def detect_unicode(str)
      str = detect_unicode_bom(str)

      # Fallback to UTF-8
      if str.encoding == Encoding::BINARY
        str.force_encoding(Encoding::UTF_8)
      end

      str
    end

detect_unicode_bom(str) click to toggle source

Public: Detect and strip BOM from possible unicode string.

str - ASCII-8BIT encoded String

Returns UTF 8/16/32 encoded String without BOM or the original String if no BOM was present.

    # File lib/sprockets/encoding_utils.rb, line 156
    def detect_unicode_bom(str)
      bom_bytes = str.byteslice(0, 4).bytes.to_a

      BOM.each do |encoding, bytes|
        if bom_bytes[0, bytes.size] == bytes
          str = str.dup
          str.force_encoding(Encoding::BINARY)
          str.slice!(0, bytes.size)
          str.force_encoding(encoding)
          return str
        end
      end

      return str
    end

gzip(str) click to toggle source

Public: Use gzip to compress data.

str - String data

Returns a compressed String

    # File lib/sprockets/encoding_utils.rb, line 58
    def gzip(str)
      io = StringIO.new
      gz = Zlib::GzipWriter.new(io, Zlib::BEST_COMPRESSION)
      gz.mtime = 1
      gz << str
      gz.finish
      io.string
    end

scan_css_charset(str) click to toggle source

Internal: Scan binary CSS string for @charset encoding name.

str - ASCII-8BIT encoded String

Returns encoding String name or nil.

    # File lib/sprockets/encoding_utils.rb, line 207
    def scan_css_charset(str)
      buf = []
      i = 0

      str.each_byte.each do |byte|
        # Halt on line breaks
        break if byte == 0x0A || byte == 0x0D

        # Only ascii bytes
        next unless 0x0 < byte && byte <= 0xFF

        if i < CHARSET_SIZE
        elsif i == CHARSET_SIZE
          if buf == CHARSET_START
            buf = []
          else
            break
          end
        elsif byte == 0x22
          return buf.pack('C*')
        end

        buf << byte
        i += 1
      end

      nil
    end

unmarshaled_deflated(str, window_bits = -Zlib::MAX_WBITS) click to toggle source

Internal: Unmarshal optionally deflated data.

Checks leading marshal header to see if the bytes are uncompressed otherwise inflate the data an unmarshal.

str - Marshaled String window_bits - Integer deflate window size. See ZLib::Inflate.new()

Returns unmarshaled Object or raises an Exception.

    # File lib/sprockets/encoding_utils.rb, line 38
    def unmarshaled_deflated(str, window_bits = -Zlib::MAX_WBITS)
      major, minor = str[0], str[1]
      if major && major.ord == Marshal::MAJOR_VERSION &&
          minor && minor.ord <= Marshal::MINOR_VERSION
        marshaled = str
      else
        begin
          marshaled = Zlib::Inflate.new(window_bits).inflate(str)
        rescue Zlib::DataError
          marshaled = str
        end
      end
      Marshal.load(marshaled)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.