Sprockets::Utils

`Utils`, we didn’t know where else to put it!

Constants

UTF8_BOM_PATTERN

Define UTF-8 BOM pattern matcher. Avoid using a Regexp literal because it inheirts the files encoding and we want to avoid syntax errors in other interpreters.

UTF8_BOM_PATTERN

Define UTF-8 and UTF-16 BOM pattern matchers. Avoid using a Regexp literal to prevent syntax errors in other interpreters.

UTF16_BOM_PATTERN

Public Class Methods

normalize_extension(extension) click to toggle source

Prepends a leading “.” to an extension if its missing.

    normalize_extension("js")
    # => ".js"

    normalize_extension(".css")
    # => ".css"
    # File lib/sprockets/utils.rb, line 60
60:     def self.normalize_extension(extension)
61:       extension = extension.to_s
62:       if extension[/^\./]
63:         extension
64:       else
65:         ".#{extension}"
66:       end
67:     end
read_unicode(pathname) click to toggle source
    # File lib/sprockets/utils.rb, line 36
36:       def self.read_unicode(pathname)
37:         pathname.read.tap do |data|
38:           # If the file is UTF-8 and theres a BOM, strip it for safe concatenation.
39:           if data =~ UTF8_BOM_PATTERN
40:             data.sub!(UTF8_BOM_PATTERN, "")
41: 
42:           # If we find a UTF-16 BOM, theres nothing we can do on
43:           # 1.8. Only UTF-8 is supported.
44:           elsif data =~ UTF16_BOM_PATTERN
45:             raise EncodingError, "#{pathname} has a UTF-16 BOM. " +
46:               "Resave the file as UTF-8 or upgrade to Ruby 1.9."
47:           end
48:         end
49:       end
read_unicode(pathname, external_encoding = Encoding.default_external) click to toggle source
    # File lib/sprockets/utils.rb, line 11
11:       def self.read_unicode(pathname, external_encoding = Encoding.default_external)
12:         pathname.open("r:#{external_encoding}") do |f|
13:           f.read.tap do |data|
14:             # Eager validate the file's encoding. In most cases we
15:             # expect it to be UTF-8 unless `default_external` is set to
16:             # something else. An error is usually raised if the file is
17:             # saved as UTF-16 when we expected UTF-8.
18:             if !data.valid_encoding?
19:               raise EncodingError, "#{pathname} has a invalid " +
20:                 "#{data.encoding} byte sequence"
21: 
22:             # If the file is UTF-8 and theres a BOM, strip it for safe concatenation.
23:             elsif data.encoding.name == "UTF-8" && data =~ UTF8_BOM_PATTERN
24:               data.sub!(UTF8_BOM_PATTERN, "")
25:             end
26:           end
27:         end
28:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.