|
1 | 1 | require 'erb'
|
2 | 2 | require 'compass'
|
3 |
| -require 'compass/sprite_importer/binding' |
4 |
| - |
5 |
| - |
6 |
| - |
| 3 | +require 'compass/sprites/importer/binding' |
7 | 4 |
|
8 | 5 | module Compass
|
9 |
| - class SpriteImporter < Sass::Importers::Base |
10 |
| - VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ |
11 |
| - SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)} |
12 |
| - |
13 |
| - TEMPLATE_FOLDER = File.join(File.expand_path('../', __FILE__), 'sprite_importer') |
14 |
| - CONTENT_TEMPLATE_FILE = File.join(TEMPLATE_FOLDER, 'content.erb') |
15 |
| - CONTENT_TEMPLATE = ERB.new(File.read(CONTENT_TEMPLATE_FILE)) |
16 |
| - |
17 |
| - |
18 |
| - |
19 |
| - # finds all sprite files |
20 |
| - def self.find_all_sprite_map_files(path) |
21 |
| - hex = "[0-9a-f]" |
22 |
| - glob = "*-s#{hex*10}{#{valid_extensions.join(",")}}" |
23 |
| - Sass::Util.glob(File.join(path, "**", glob)) |
24 |
| - end |
25 |
| - |
26 |
| - def find(uri, options) |
27 |
| - if uri =~ SPRITE_IMPORTER_REGEX |
28 |
| - return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options) |
| 6 | + module Sprites |
| 7 | + class Importer < Sass::Importers::Base |
| 8 | + VAILD_FILE_NAME = /\A#{Sass::SCSS::RX::IDENT}\Z/ |
| 9 | + SPRITE_IMPORTER_REGEX = %r{((.+/)?([^\*.]+))/(.+?)} |
| 10 | + |
| 11 | + TEMPLATE_FOLDER = File.join(File.expand_path('../', __FILE__), 'importer') |
| 12 | + CONTENT_TEMPLATE_FILE = File.join(TEMPLATE_FOLDER, 'content.erb') |
| 13 | + CONTENT_TEMPLATE = ERB.new(File.read(CONTENT_TEMPLATE_FILE)) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + # finds all sprite files |
| 18 | + def self.find_all_sprite_map_files(path) |
| 19 | + hex = "[0-9a-f]" |
| 20 | + glob = "*-s#{hex*10}{#{valid_extensions.join(",")}}" |
| 21 | + Sass::Util.glob(File.join(path, "**", glob)) |
29 | 22 | end
|
30 |
| - nil |
31 |
| - end |
32 |
| - |
33 |
| - def find_relative(uri, base, options) |
34 |
| - nil |
35 |
| - end |
36 |
| - |
37 |
| - def to_s |
38 |
| - self.class.name |
39 |
| - end |
40 |
| - |
41 |
| - def hash |
42 |
| - self.class.name.hash |
43 |
| - end |
44 |
| - |
45 |
| - def eql?(other) |
46 |
| - other.class == self.class |
47 |
| - end |
48 |
| - |
49 |
| - def mtime(uri, options) |
50 |
| - self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file| |
51 |
| - (t = File.mtime(file)) > max_time ? t : max_time |
| 23 | + |
| 24 | + def find(uri, options) |
| 25 | + if uri =~ SPRITE_IMPORTER_REGEX |
| 26 | + return self.class.sass_engine(uri, self.class.sprite_name(uri), self, options) |
| 27 | + end |
| 28 | + nil |
| 29 | + end |
| 30 | + |
| 31 | + def find_relative(uri, base, options) |
| 32 | + nil |
| 33 | + end |
| 34 | + |
| 35 | + def to_s |
| 36 | + self.class.name |
| 37 | + end |
| 38 | + |
| 39 | + def hash |
| 40 | + self.class.name.hash |
52 | 41 | end
|
53 |
| - end |
54 | 42 |
|
55 |
| - def key(uri, options={}) |
56 |
| - [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)] |
57 |
| - end |
| 43 | + def key(uri, options={}) |
| 44 | + [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)] |
| 45 | + end |
58 | 46 |
|
59 |
| - def public_url(*args) |
60 |
| - nil |
61 |
| - end |
| 47 | + def public_url(*args) |
| 48 | + nil |
| 49 | + end |
62 | 50 |
|
63 | 51 |
|
64 |
| - def self.path_and_name(uri) |
65 |
| - if uri =~ sprite_importer_regex_with_ext |
66 |
| - [$1, $3] |
67 |
| - else |
68 |
| - raise Compass::Error, "invalid sprite path" |
| 52 | + def eql?(other) |
| 53 | + other.class == self.class |
| 54 | + end |
| 55 | + |
| 56 | + def mtime(uri, options) |
| 57 | + self.class.files(uri).sort.inject(Time.at(0)) do |max_time, file| |
| 58 | + (t = File.mtime(file)) > max_time ? t : max_time |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def key(uri, options={}) |
| 63 | + [self.class.name + ":sprite:" + File.dirname(File.expand_path(uri)), File.basename(uri)] |
| 64 | + end |
| 65 | + |
| 66 | + def self.path_and_name(uri) |
| 67 | + if uri =~ sprite_importer_regex_with_ext |
| 68 | + [$1, $3] |
| 69 | + else |
| 70 | + raise Compass::Error, "invalid sprite path" |
| 71 | + end |
69 | 72 | end
|
70 |
| - end |
71 |
| - |
72 |
| - # Name of this spite |
73 |
| - def self.sprite_name(uri) |
74 |
| - _, name = path_and_name(uri) |
75 |
| - name |
76 |
| - end |
77 | 73 |
|
78 |
| - # The on-disk location of this sprite |
79 |
| - def self.path(uri) |
80 |
| - path, _ = path_and_name(uri) |
81 |
| - path |
82 |
| - end |
83 |
| - |
84 |
| - # Returns the Glob of image files for the uri |
85 |
| - def self.files(uri) |
86 |
| - Compass.configuration.sprite_load_path.compact.each do |folder| |
87 |
| - files = Sass::Util.glob(File.join(folder, uri)).sort |
88 |
| - next if files.empty? |
89 |
| - return files |
| 74 | + # Name of this spite |
| 75 | + def self.sprite_name(uri) |
| 76 | + _, name = path_and_name(uri) |
| 77 | + name |
90 | 78 | end
|
91 | 79 |
|
92 |
| - path = Compass.configuration.sprite_load_path.to_a.join(', ') |
93 |
| - raise Compass::SpriteException, %Q{No files were found in the load path matching "#{uri}". Your current load paths are: #{path}} |
94 |
| - end |
| 80 | + # The on-disk location of this sprite |
| 81 | + def self.path(uri) |
| 82 | + path, _ = path_and_name(uri) |
| 83 | + path |
| 84 | + end |
| 85 | + |
| 86 | + # Returns the Glob of image files for the uri |
| 87 | + def self.files(uri) |
| 88 | + Compass.configuration.sprite_load_path.compact.each do |folder| |
| 89 | + files = Sass::Util.glob(File.join(folder, uri)).sort |
| 90 | + next if files.empty? |
| 91 | + return files |
| 92 | + end |
| 93 | + |
| 94 | + path = Compass.configuration.sprite_load_path.to_a.join(', ') |
| 95 | + raise Compass::SpriteException, %Q{No files were found in the load path matching "#{uri}". Your current load paths are: #{path}} |
| 96 | + end |
95 | 97 |
|
96 |
| - # Returns an Array of image names without the file extension |
97 |
| - def self.sprite_names(uri) |
98 |
| - files(uri).collect do |file| |
99 |
| - File.basename(file, '.png') |
| 98 | + # Returns an Array of image names without the file extension |
| 99 | + def self.sprite_names(uri) |
| 100 | + files(uri).collect do |file| |
| 101 | + File.basename(file, '.png') |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + # Returns the sass_options for this sprite |
| 106 | + def self.sass_options(uri, importer, options) |
| 107 | + options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer) |
| 108 | + end |
| 109 | + |
| 110 | + # Returns a Sass::Engine for this sprite object |
| 111 | + def self.sass_engine(uri, name, importer, options) |
| 112 | + content = content_for_images(uri, name, options[:skip_overrides]) |
| 113 | + Sass::Engine.new(content, sass_options(uri, importer, options)) |
100 | 114 | end
|
101 |
| - end |
102 |
| - |
103 |
| - # Returns the sass_options for this sprite |
104 |
| - def self.sass_options(uri, importer, options) |
105 |
| - options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer) |
106 |
| - end |
107 |
| - |
108 |
| - # Returns a Sass::Engine for this sprite object |
109 |
| - def self.sass_engine(uri, name, importer, options) |
110 |
| - content = content_for_images(uri, name, options[:skip_overrides]) |
111 |
| - Sass::Engine.new(content, sass_options(uri, importer, options)) |
112 |
| - end |
113 | 115 |
|
114 |
| - # Generates the Sass for this sprite file |
115 |
| - def self.content_for_images(uri, name, skip_overrides = false) |
116 |
| - binder = Compass::Sprites::Binding.new(:name => name, :uri => uri, :skip_overrides => skip_overrides, :sprite_names => sprite_names(uri), :files => files(uri)) |
117 |
| - CONTENT_TEMPLATE.result(binder.get_binding) |
118 |
| - end |
| 116 | + # Generates the Sass for this sprite file |
| 117 | + def self.content_for_images(uri, name, skip_overrides = false) |
| 118 | + binder = Compass::Sprites::Binding.new(:name => name, :uri => uri, :skip_overrides => skip_overrides, :sprite_names => sprite_names(uri), :files => files(uri)) |
| 119 | + CONTENT_TEMPLATE.result(binder.get_binding) |
| 120 | + end |
119 | 121 |
|
120 |
| - private |
| 122 | + private |
121 | 123 |
|
122 |
| - def self.valid_extensions |
123 |
| - @valid_extensions ||= SassExtensions::Sprites::SpriteMap.sprite_engine_class::VALID_EXTENSIONS |
124 |
| - end |
| 124 | + def self.valid_extensions |
| 125 | + @valid_extensions ||= SassExtensions::Sprites::SpriteMap.sprite_engine_class::VALID_EXTENSIONS |
| 126 | + end |
125 | 127 |
|
126 |
| - def self.sprite_importer_regex_with_ext |
127 |
| - @importer_regex ||= %r{#{SPRITE_IMPORTER_REGEX}(#{valid_extensions.join('|')})} |
| 128 | + def self.sprite_importer_regex_with_ext |
| 129 | + @importer_regex ||= %r{#{SPRITE_IMPORTER_REGEX}(#{valid_extensions.join('|')})} |
| 130 | + end |
128 | 131 | end
|
129 | 132 | end
|
130 | 133 | end
|
|
0 commit comments