Module: Sass::Script::Functions

Includes:
UserFunctions
Defined in:
lib/sassmagic/utils.rb,
lib/sassmagic/utils.rb,
lib/sassmagic/utils.rb,
lib/sassmagic/utils.rb,
lib/sassmagic/utils.rb,
lib/sassmagic/utils.rb,
lib/sassmagic/utils.rb,
lib/sassmagic/reset.rb

Overview

url重写

Constant Summary collapse

FONT_TYPES =
{
    eot: 'embedded-opentype',
    woff: 'woff',
    ttf: 'truetype',
    svg: 'svg'
}
MIME_TYPES =
{
    png: 'image/png',
    jpg: 'image/jpeg',
    jpeg: 'image/jpeg',
    gif: 'image/gif',
    eot: 'application/vnd.ms-fontobject',
    woff: 'application/font-woff',
    ttf: 'font/truetype',
    svg: 'image/svg+xml'
}
PATH_REGEX =
/^(.*)(\.\w+)(\??[^#]*)(#?.*)$/

Instance Method Summary collapse

Instance Method Details

#app_config(name) ⇒ Object

Returns the config associated with the given name. Configs are be grouped by ‘SASS_ENV` environment.

Examples: $app-config: (

development: (
  foo: bar
),
production: (
  foo: baz
)

);

$ sass –watch -r base.sass src:dist app-config(foo) => bar

$ SASS_ENV=production sass –watch -r base.sass src:dist app-config(foo) => baz



40
41
42
43
44
45
46
47
48
49
# File 'lib/sassmagic/utils.rb', line 40

def app_config(name)
  assert_type name, :String
# debugger
  config = environment.global_env.var('app-config')
  return null unless config.is_a? Sass::Script::Value::Map

  config = map_get(config, env(identifier('sass-env')))

  map_get(config, name)
end

#call($name, $args...) ⇒ Object

Dynamically calls a function. This can call user-defined functions, built-in functions, or plain CSS functions. It will pass along all arguments, including keyword arguments, to the called function.

Examples:

call(rgb, 10, 100, 255) => #0a64ff
call(scale-color, #0a64ff, $lightness: -10%) => #0058ef

$fn: nth;
call($fn, (a b c), 2) => b

Parameters:

  • $name (String)

    The name of the function to call.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sassmagic/reset.rb', line 48

def call(name, *args)
  assert_type name, :String, :name
  kwargs = args.last.is_a?(Hash) ? args.pop : {}
  funcall = Sass::Script::Tree::Funcall.new(
      name.value,
      args.map {|a| Sass::Script::Tree::Literal.new(a)},
      Sass::Util.map_vals(kwargs) {|v| Sass::Script::Tree::Literal.new(v)},
      nil,
      nil)
  funcall.options = options
  perform(funcall)
end

#env(name) ⇒ Object

Returns the value of environment variable associated with the given name. Returns null if the named variable does not exist.

Examples: env(SASS_ENV) => development env(sass_env) => development env(sass-env) => development



17
18
19
20
# File 'lib/sassmagic/utils.rb', line 17

def env(name)
  assert_type name, :String
  ruby_to_sass(ENV[name.value.gsub('-', '_').upcase])
end

#inline_image(path, mime_type = nil) ⇒ Object



142
143
144
145
146
147
# File 'lib/sassmagic/utils.rb', line 142

def inline_image(path, mime_type = nil)
  # debugger
  path = path.value
  real_path = File.expand_path("#{File.dirname(options[:filename])}/#{path}")
  inline_image_string(data(real_path), compute_mime_type(path, mime_type))
end

#load_json(path) ⇒ Object

protected



483
484
485
486
487
488
489
# File 'lib/sassmagic/utils.rb', line 483

def load_json(path)
  if File::exists?( path )
    JSON.load(
        read_file(File.expand_path(path)).to_s.gsub(/(\\r|\\n)/, '')
    )
  end
end

#parse_json(path) ⇒ Object

Parses a local json file, returns a map, and the result will be cached. If the ‘path` is not a absolute path, relative to current process directory.

Examples: parse-json(‘~/Desktop/example.json’) parse-json(‘package.json’)



468
469
470
471
472
473
474
475
476
477
478
# File 'lib/sassmagic/utils.rb', line 468

def parse_json(path)
  assert_type path, :String
  path = File.expand_path(path.value)

  if $cached_files.key? path
    Sass.logger.debug "Reading file from cache: #{path}"
    $cached_files[path]
  else
    $cached_files[path] = ruby_to_sass(load_json(path))
  end
end

#pxtorem(string) ⇒ Object



29
30
31
32
# File 'lib/sassmagic/reset.rb', line 29

def pxtorem(string)
  assert_type string, :String
  Sass::Script::Value::String.new(string+'rem')
end

#read_file(path) ⇒ Object

Raises:

  • (Sass::SyntaxError)


491
492
493
494
495
496
# File 'lib/sassmagic/utils.rb', line 491

def read_file(path)
  raise Sass::SyntaxError, "File not found or cannot be read: #{path}" unless File.readable? path

  Sass.logger.debug "Reading file: #{path}"
  File.open(path, 'rb') { |f| f.read }
end

#reverse(string) ⇒ Object



24
25
26
27
# File 'lib/sassmagic/reset.rb', line 24

def reverse(string)
  assert_type string, :String
  Sass::Script::Value::String.new(string.value.reverse)
end

#strftime(format = nil) ⇒ Object

Formats time according to the directives in the given format string. Read more: www.ruby-doc.org/core-2.1.1/Time.html#method-i-strftime

Examples: strftime() => 1399392214 strftime(‘%FT%T%:z’) => 2014-05-07T00:03:34+08:00 strftime(‘at %I:%M%p’) => at 12:03AM



63
64
65
66
67
68
69
70
71
72
# File 'lib/sassmagic/utils.rb', line 63

def strftime(format = nil)
  time = Time.now.localtime

  if format
    assert_type format, :String
    identifier(time.strftime(format.value))
  else
    identifier(time.to_i.to_s)
  end
end

#url(*paths) ⇒ Object

Reinforce the official ‘url()` in CSS to support multi url and data url. Activates only when all paths are wrapped with quotes.

Examples: url() => url() # Did nothing url(‘’) => url(a.com/b.png?1399394203) url(‘a.png’, ‘b.png’) => url(a.png?1399394203), url(b.png?1399394203) url(‘a.eot#iefix’, ‘b.woff’) => url(a.eot?1399394203#iefix) format(‘embedded-opentype’), url(b.woff?1399394203) format(‘woff’)

url(‘a.png’, $timestamp: false) => url(a.png) url(‘a.png’, $timestamp: ‘1.0.0’) => url(a.png?1.0.0)

$app-config: (timestamp: ‘1.0.0’); url(‘a.png’) => url(a.png?1.0.0)

$app-config: (timestamp: ‘p1’); url(‘a.png’, $timestamp: ‘p0’) => url(a.png?p0)

url(‘a.png’, $base64: true) => url(data:image/png;base64,iVBORw…)

Raises:

  • (Sass::SyntaxError)


243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/sassmagic/utils.rb', line 243

def url(*paths)
  # debugger
  $configHash ||= load_json(File.expand_path("#{File.dirname(options[:filename])}/../config/sassmagic.json")) || Hash.new
  kwargs = paths.last.is_a?(Hash) ? paths.pop : {}
  raise Sass::SyntaxError, 'url() needs one path at least' if paths.empty?

  encode = kwargs['base64'] == bool(true)
  ts = timestamp(kwargs['timestamp'])

  paths = paths.map { |path| sass_to_ruby(path) }.flatten
  .map { |path| compress_img(path, encode, ts); }

  list(paths, :comma)
end