-
Notifications
You must be signed in to change notification settings - Fork 4
/
sass_plugins.rb
executable file
·75 lines (65 loc) · 1.87 KB
/
sass_plugins.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env ruby
# This is an alternate entry point to the sass command line tool that allows
# us, Monetology, to inject our own functions.
require 'base64'
class Scope
@options = []
def self.add(options)
@options << options
end
def self.find(name)
# first lets check relative to all the files. this is wrong
# and i need to fix it.
@options.each { |opt|
f = File.join(File.dirname(opt[:filename]), name)
return f if File.exists?(f)
}
# next let's look in all the load_paths
@options.first[:load_paths].each { |p|
f = File.join(p.root, name)
return f if File.exists?(f)
}
#nothing, just return a path that isn't going to work
return name
end
end
class Url <Sass::Script::Literal
def initialize(url)
super(url)
end
def to_s(opts = {})
"url(\"#{@value}\")"
end
end
# monkey-patch the _to_tree method in Engine so that we can capture
# the execution context
class Sass::Engine
alias_method :__to_tree, :_to_tree
def _to_tree
Scope.add(@options)
__to_tree
end
end
module Sass::Script::Functions
def datauri(string)
assert_type string, :String
name = string.value.downcase
mime = "application/octet-stream"
mime = "image/svg+xml" if name.end_with?(".svg")
mime = "image/png" if name.end_with?(".png")
mime = "image/gif" if name.end_with?(".gif")
mime = "image/jpg" if name.end_with?(".jpg") || name.end_with?(".jpeg")
file = File.open(Scope.find(string.value), 'rb')
begin
# love that gsub at the end? ruby's Base64 adds \n's every
# 60 characters. Why? I have no ideas. RFC 2045 doesn't
# say anything about that. It's just the usual ruby community
# gift!
data = Base64.encode64(file.read).strip.gsub("\n", "")
Url.new("data:#{mime};base64,#{data}")
ensure
file.close
end
end
declare :datauri, [:string]
end