Class: Compass::Fontcustom::FontImporter

Inherits:
Sass::Importers::Base
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.content_for_font(uri, name) ⇒ String

Renders the stylesheet for font ‘name` at `uri`

Parameters:

  • uri (String)

    the uri to glob files from

  • name (String)

    the font’s name

Returns:

  • (String)


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.

Parameters:

  • uri (String)

    the uri to glob files from

Returns:

  • (Array)


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`.

Returns:

  • (String)

See Also:



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_pathString

Returns the fonts path

Returns:

  • (String)


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`.

Parameters:

  • uri (String)

    the uri to glob files from

Returns:

  • (Array)


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`

Returns:

  • (String)

See Also:



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`.

Parameters:

  • uri (String)

    an uri to match glyph image

Returns:

  • (Array)

    a two-element array containing the path and file name components



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`

Parameters:

  • uri (String)

    the uri to glob files from

  • name (String)

    the font’s name

  • importer (Sass::Importers::Base)

    the importer to use (in this case ‘self`)

  • options (Hash)

    options to merge with

Returns:

  • (Sass::Engine)


90
91
92
93
# File 'lib/compass/fontcustom/font_importer.rb', line 90

def sass_engine(uri, name, importer, options)
  content = content_for_font(uri, name)
  Sass::Engine.new(content, sass_options(uri, importer, options))
end

.sass_options(uri, importer, options) ⇒ Hash

Returns ‘Sass::Engine` options with defaults

Parameters:

  • uri (String)

    the uri to glob files from

  • importer (Sass::Importers::Base)

    the importer to use (in this case ‘self`)

  • options (Hash)

    options to merge with

Returns:

  • (Hash)


80
81
82
# File 'lib/compass/fontcustom/font_importer.rb', line 80

def sass_options(uri, importer, options)
  options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer)
end

.search_pathsObject



5
6
7
# File 'lib/compass/fontcustom/rails.rb', line 5

def self.search_paths
  Rails.application.config.assets.paths
end

.template_pathString

Returns the gems’ internal template path.

Returns:

  • (String)


113
114
115
# File 'lib/compass/fontcustom/font_importer.rb', line 113

def template_path
  File.expand_path('../templates', __FILE__)
end

Instance Method Details

#eql?(other) ⇒ Boolean

Compares this instance with another object.

Parameters:

  • other (Object)

    another object

Returns:

  • (Boolean)


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.

Parameters:

  • uri (String)

    the uri from the import directive

  • options (Hash)

    options for the returned Sass::Engine`

Returns:

  • (Sass::Engine)


132
133
134
135
136
137
# File 'lib/compass/fontcustom/font_importer.rb', line 132

def find(uri, options)
  if uri =~ GLYPH_FILE_REGEX
    return self.class.sass_engine(uri, self.class.font_name(uri), self, options)
  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, options)
  nil
end

#hashString

Returns the hash of this instance.

Returns:

  • (String)


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

Parameters:

  • uri (String)

    the uri to glob files from

  • options (Hash) (defaults to: {})

    hash of options

Returns:

  • (Array)


173
174
175
# File 'lib/compass/fontcustom/font_importer.rb', line 173

def key(uri, options={})
  [self.class.name + ":fontcustom:" + File.dirname(File.expand_path(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, options)
  self.class.files(uri).inject(Time.at(0)) do |max_time, file|
    (t = File.mtime(file)) > max_time ? t : max_time
  end
end

#to_sString

Returns the string representation of this instance.

Returns:

  • (String)


146
147
148
# File 'lib/compass/fontcustom/font_importer.rb', line 146

def to_s
  self.class.name
end