Class: Compass::Fontcustom::FontImporter
- Inherits:
-
Sass::Importers::Base
- Object
- Sass::Importers::Base
- Compass::Fontcustom::FontImporter
- Defined in:
- lib/compass/fontcustom/font_importer.rb,
lib/compass/fontcustom/rails.rb
Overview
The Sass Importer responsible to find svg and eps files and to start the Fontcustom font generator.
Constant Summary collapse
- GLYPH_FILE_REGEX =
Regexp matching uri’s of svg and eps files. Matching group ‘$1` contains the path, `$3` the file name.
%r{((.+/)?([^\*.]+))/(.+?)\.(svg|eps)}
Class Method Summary collapse
-
.content_for_font(uri, name) ⇒ String
Renders the stylesheet for font ‘name` at `uri`.
-
.files(uri) ⇒ Array
Returns an array of font files.
-
.font_name(uri) ⇒ String
Returns only the file name component of ‘uri`.
-
.fonts_path ⇒ String
Returns the fonts path.
-
.glyph_names(uri) ⇒ Array
Returns all glyph names inside the folder at ‘uri`.
-
.path(uri) ⇒ String
Returns only the path component of ‘uri`.
-
.path_and_name(uri) ⇒ Array
Returns an array with two elements.
-
.sass_engine(uri, name, importer, options) ⇒ Sass::Engine
Returns a ‘Sass::Engine`.
-
.sass_options(uri, importer, options) ⇒ Hash
Returns ‘Sass::Engine` options with defaults.
- .search_paths ⇒ Object
-
.template_path ⇒ String
Returns the gems’ internal template path.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
Compares this instance with another object.
-
#find(uri, options) ⇒ Sass::Engine
Resolves incoming uri from an ‘@import “…”` directive.
-
#find_relative(uri, base, options) ⇒ Object
Unused.
-
#hash ⇒ String
Returns the hash of this instance.
-
#key(uri, options = {}) ⇒ Array
This instance’s Compass cache key.
- #mtime(uri, options) ⇒ Object
-
#to_s ⇒ String
Returns the string representation of this instance.
Class Method Details
.content_for_font(uri, name) ⇒ String
Renders the stylesheet for font ‘name` at `uri`
99 100 101 102 103 |
# File 'lib/compass/fontcustom/font_importer.rb', line 99 def content_for_font(uri, name) erb = File.read File.join(template_path, 'stylesheet.scss.erb') binder = TemplateData.new(uri: uri, name: name, path: fonts_path, glyph_names: glyph_names(uri)) ERB.new(erb).result(binder.expose_binding) end |
.files(uri) ⇒ Array
Returns an array of font files.
120 121 122 123 124 |
# File 'lib/compass/fontcustom/font_importer.rb', line 120 def files(uri) [].tap do |files| search_paths.each { |p| files.concat Dir[File.join p, uri] } end end |
.font_name(uri) ⇒ String
Returns only the file name component of ‘uri`.
44 45 46 47 |
# File 'lib/compass/fontcustom/font_importer.rb', line 44 def font_name(uri) _, name = path_and_name(uri) name end |
.fonts_path ⇒ String
Returns the fonts path
107 108 109 |
# File 'lib/compass/fontcustom/font_importer.rb', line 107 def fonts_path Compass.configuration.fonts_dir.to_s end |
.glyph_names(uri) ⇒ Array
Returns all glyph names inside the folder at ‘uri`.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/compass/fontcustom/font_importer.rb', line 64 def glyph_names(uri) files = [] search_paths.each { |p| files.concat Dir[File.join(p, uri)] } if files.empty? raise Compass::SpriteException, %Q{No glyph images were found matching "#{uri}" in the images path. Your current images path is: #{folder}} end files.map { |f| File.basename(f)[0..-5] } end |
.path(uri) ⇒ String
Returns only the path component of ‘uri`
52 53 54 55 |
# File 'lib/compass/fontcustom/font_importer.rb', line 52 def path(uri) path, _ = path_and_name(uri) path end |
.path_and_name(uri) ⇒ Array
Returns an array with two elements. First the path, second the file name of the ‘uri`.
33 34 35 36 37 38 39 |
# File 'lib/compass/fontcustom/font_importer.rb', line 33 def path_and_name(uri) if uri =~ GLYPH_FILE_REGEX [$1, $3] else raise Compass::Error, "Invalid glyph image uri" end end |
.sass_engine(uri, name, importer, options) ⇒ Sass::Engine
Returns a ‘Sass::Engine`
90 91 92 93 |
# File 'lib/compass/fontcustom/font_importer.rb', line 90 def sass_engine(uri, name, importer, ) content = content_for_font(uri, name) Sass::Engine.new(content, (uri, importer, )) end |
.sass_options(uri, importer, options) ⇒ Hash
Returns ‘Sass::Engine` options with defaults
80 81 82 |
# File 'lib/compass/fontcustom/font_importer.rb', line 80 def (uri, importer, ) .merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer) end |
.search_paths ⇒ Object
5 6 7 |
# File 'lib/compass/fontcustom/rails.rb', line 5 def self.search_paths Rails.application.config.assets.paths end |
.template_path ⇒ String
Returns the gems’ internal template path.
113 114 115 |
# File 'lib/compass/fontcustom/font_importer.rb', line 113 def template_path File.('../templates', __FILE__) end |
Instance Method Details
#eql?(other) ⇒ Boolean
Compares this instance with another object.
159 160 161 |
# File 'lib/compass/fontcustom/font_importer.rb', line 159 def eql?(other) other.class == self.class end |
#find(uri, options) ⇒ Sass::Engine
Resolves incoming uri from an ‘@import “…”` directive.
132 133 134 135 136 137 |
# File 'lib/compass/fontcustom/font_importer.rb', line 132 def find(uri, ) if uri =~ GLYPH_FILE_REGEX return self.class.sass_engine(uri, self.class.font_name(uri), self, ) end nil end |
#find_relative(uri, base, options) ⇒ Object
Unused. Just returns nil.
140 141 142 |
# File 'lib/compass/fontcustom/font_importer.rb', line 140 def find_relative(uri, base, ) nil end |
#hash ⇒ String
Returns the hash of this instance.
152 153 154 |
# File 'lib/compass/fontcustom/font_importer.rb', line 152 def hash self.class.name.hash end |
#key(uri, options = {}) ⇒ Array
This instance’s Compass cache key
173 174 175 |
# File 'lib/compass/fontcustom/font_importer.rb', line 173 def key(uri, ={}) [self.class.name + ":fontcustom:" + File.dirname(File.(uri)), File.basename(uri)] end |
#mtime(uri, options) ⇒ Object
163 164 165 166 167 |
# File 'lib/compass/fontcustom/font_importer.rb', line 163 def mtime(uri, ) self.class.files(uri).inject(Time.at(0)) do |max_time, file| (t = File.mtime(file)) > max_time ? t : max_time end end |
#to_s ⇒ String
Returns the string representation of this instance.
146 147 148 |
# File 'lib/compass/fontcustom/font_importer.rb', line 146 def to_s self.class.name end |