sassmagic 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Readme.md CHANGED
@@ -0,0 +1,48 @@
1
+ ### sassmagic
2
+
3
+
4
+ description:Sass extention tool
5
+
6
+
7
+ ### install
8
+
9
+
10
+ sudo gem install sassmagic
11
+
12
+
13
+ ### use
14
+
15
+
16
+ same to sass
17
+
18
+
19
+ ### configuration
20
+
21
+
22
+ sassmagic.json 配置文件:
23
+
24
+
25
+
26
+
27
+ {
28
+ "isNeedPxToRem": true,
29
+ "browserDefaultFontSize": "75px",
30
+ "ignore": [
31
+ "1px",
32
+ "[data-dpr=",
33
+ "font-size"
34
+ ],
35
+ "outputExtra": [
36
+ "1x",
37
+ "2x",
38
+ "3x"
39
+ ],
40
+ "imageMaxSize": "5120",
41
+ "imageLoader": "imageuploader.js",
42
+ "imagesPath": {
43
+ },
44
+ "remote":[]
45
+ }
46
+
47
+ imageuploader.js
48
+ 图片上传
data/bin/sassmagic CHANGED
@@ -8,6 +8,7 @@ end
8
8
 
9
9
  require 'sass'
10
10
  require 'sass/exec'
11
+ require 'sassmagic/remote'
11
12
  require 'sassmagic/utils'
12
13
  require 'sassmagic/reset'
13
14
  # require 'debugger'
@@ -0,0 +1,117 @@
1
+ ##used remote-sass
2
+ require 'net/http'
3
+ require 'time'
4
+ module RemoteSass
5
+ class << self
6
+ def location
7
+ @location
8
+ end
9
+
10
+ def location= location
11
+ @location = location
12
+ Sass.load_paths << Sass::Importers::HTTP.new(location)
13
+ end
14
+ end
15
+ end
16
+ module Sass
17
+ module Importers
18
+ class HTTP < Base
19
+ def initialize root
20
+ @root = URI.parse root
21
+
22
+ unless scheme_allowed? @root
23
+ raise ArgumentError, "Absolute HTTP URIs only"
24
+ end
25
+ end
26
+
27
+ def find_relative uri, base, options
28
+ _find @root + base + uri, options
29
+ end
30
+
31
+ def find uri, options
32
+ _find @root + uri, options
33
+ end
34
+
35
+ def mtime uri, options
36
+ uri = URI.parse uri
37
+ return unless scheme_allowed? uri
38
+ Net::HTTP.start(uri.host, uri.port) do |http|
39
+ response = http.head uri.request_uri
40
+
41
+ if response.is_a?(Net::HTTPOK) && response['Last-Modified']
42
+ Time.parse response['Last-Modified']
43
+ elsif response.is_a? Net::HTTPOK
44
+ # we must assume that it just changed
45
+ Time.now
46
+ else
47
+ nil
48
+ end
49
+ end
50
+ end
51
+
52
+ def key(uri, options)
53
+ [self.class.name, uri]
54
+ end
55
+
56
+ def to_s
57
+ @root.to_s
58
+ end
59
+
60
+ protected
61
+
62
+ def extensions
63
+ {'.sass' => :sass, '.scss' => :scss}
64
+ end
65
+
66
+ private
67
+
68
+ def scheme_allowed? uri
69
+ uri.absolute? && (uri.scheme == 'http' || uri.scheme == 'https')
70
+ end
71
+
72
+ def exists? uri
73
+ Net::HTTP.start(uri.host, uri.port) do |http|
74
+ http.head(uri.request_uri).is_a? Net::HTTPOK
75
+ end
76
+ end
77
+
78
+ def get_syntax uri
79
+ # determine the syntax being used
80
+ ext = File.extname uri.path
81
+ syntax = extensions[ext]
82
+
83
+ # this must not be the full path: try another
84
+ if syntax.nil?
85
+ ext, syntax = extensions.find do |possible_ext, possible_syntax|
86
+ new_uri = uri.dup
87
+ new_uri.path += possible_ext
88
+ exists? new_uri
89
+ end
90
+ return if syntax.nil?
91
+ uri.path += ext
92
+ end
93
+ syntax
94
+ end
95
+
96
+ def _find uri, options
97
+ raise ArgumentError, "Absolute HTTP URIs only" unless scheme_allowed? uri
98
+
99
+ syntax = get_syntax uri
100
+
101
+ # fetch the content
102
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
103
+ # debugger
104
+ response = http.get uri.request_uri
105
+ response.value
106
+
107
+ options[:importer] = self
108
+ options[:filename] = uri.to_s
109
+ options[:syntax] = syntax
110
+ Sass::Engine.new response.body, options
111
+ end
112
+ # rescue
113
+ # nil
114
+ end
115
+ end
116
+ end
117
+ end
@@ -146,6 +146,11 @@ module Sass
146
146
  options = args.last.is_a?(Hash) ? args.pop : {}
147
147
  options = options.merge $configHash
148
148
  css_filename = args.shift
149
+ #是否写入loadpath
150
+ if options.has_key?"remoteStylesheet"
151
+ RemoteSass.location = options["remoteStylesheet"]
152
+ end
153
+ #
149
154
  # debugger
150
155
  #是否需要额外输出样式表
151
156
  if options.has_key?"outputExtra"
@@ -4,6 +4,7 @@
4
4
  ## Licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
5
5
 
6
6
  #env
7
+ # require 'tinypng'
7
8
  module Sass::Script::Functions
8
9
 
9
10
  # Returns the value of environment variable associated with the given name.
@@ -136,64 +137,70 @@ end
136
137
 
137
138
 
138
139
  #inline_image 取图片base64编码
139
- # module Sass::Script::Functions::InlineImage
140
- #
141
- # def inline_image(path, mime_type = nil)
142
- # # path = path.value
143
- # # real_path = File.join(Compass.configuration.images_path, path)
144
- # inline_image_string(data(path), compute_mime_type(path, mime_type))
145
- # end
146
- #
147
- # protected
148
- # def inline_image_string(data, mime_type)
149
- # data = [data].flatten.pack('m').gsub("\n","")
150
- # url = "url('data:#{mime_type};base64,#{data}')"
151
- # unquoted_string(url)
152
- # end
153
- #
154
- # private
155
- # def compute_mime_type(path, mime_type = nil)
156
- # return mime_type.value if mime_type
157
- # case path
158
- # when /\.png$/i
159
- # 'image/png'
160
- # when /\.jpe?g$/i
161
- # 'image/jpeg'
162
- # when /\.gif$/i
163
- # 'image/gif'
164
- # when /\.svg$/i
165
- # 'image/svg+xml'
166
- # when /\.otf$/i
167
- # 'font/opentype'
168
- # when /\.eot$/i
169
- # 'application/vnd.ms-fontobject'
170
- # when /\.ttf$/i
171
- # 'font/truetype'
172
- # when /\.woff$/i
173
- # 'application/font-woff'
174
- # when /\.off$/i
175
- # 'font/openfont'
176
- # when /\.([a-zA-Z]+)$/
177
- # "image/#{Regexp.last_match(1).downcase}"
178
- # else
179
- # raise Sass.logger.debug("A mime type could not be determined for #{path}, please specify one explicitly.")
180
- # end
181
- # end
182
- #
183
- # def data(real_path)
184
- # debugger
185
- # if File.readable?(real_path)
186
- # File.open(real_path, "rb") {|io| io.read}
187
- # else
188
- # raise Sass.logger.debug("File not found or cannot be read: #{real_path}")
189
- # end
190
- # end
191
- #
192
- # end
140
+ module Sass::Script::Functions
141
+
142
+ def inline_image(path, mime_type = nil)
143
+ # debugger
144
+ path = path.value
145
+ real_path = File.expand_path("#{File.dirname(options[:filename])}/#{path}")
146
+ inline_image_string(data(real_path), compute_mime_type(path, mime_type))
147
+ end
148
+
149
+ declare :inline_image, [], var_args: true, var_kwargs: true
150
+
151
+ protected
152
+ def inline_image_string(data, mime_type)
153
+ data = [data].flatten.pack('m').gsub("\n","")
154
+ url = "url(data:#{mime_type};base64,#{data})"
155
+ unquoted_string(url)
156
+ end
157
+
158
+ private
159
+ def compute_mime_type(path, mime_type = nil)
160
+ return mime_type.value if mime_type
161
+ case path
162
+ when /\.png$/i
163
+ 'image/png'
164
+ when /\.jpe?g$/i
165
+ 'image/jpeg'
166
+ when /\.gif$/i
167
+ 'image/gif'
168
+ when /\.svg$/i
169
+ 'image/svg+xml'
170
+ when /\.otf$/i
171
+ 'font/opentype'
172
+ when /\.eot$/i
173
+ 'application/vnd.ms-fontobject'
174
+ when /\.ttf$/i
175
+ 'font/truetype'
176
+ when /\.woff$/i
177
+ 'application/font-woff'
178
+ when /\.off$/i
179
+ 'font/openfont'
180
+ when /\.([a-zA-Z]+)$/
181
+ "image/#{Regexp.last_match(1).downcase}"
182
+ else
183
+ raise Sass.logger.debug("A mime type could not be determined for #{path}, please specify one explicitly.")
184
+ end
185
+ end
186
+
187
+ def data(real_path)
188
+ # debugger
189
+ if File.readable?(real_path)
190
+ File.open(real_path, "rb") {|io| io.read}
191
+ else
192
+ raise Sass.logger.debug("File not found or cannot be read: #{real_path}")
193
+ end
194
+ end
195
+
196
+ end
197
+
198
+
199
+
200
+
193
201
  #url重写
194
202
  module Sass::Script::Functions
195
- # include Sass::Script::Functions::InlineImage
196
- # declare :inline_image, [], var_args: true, var_kwargs: true
203
+
197
204
  FONT_TYPES = {
198
205
  eot: 'embedded-opentype',
199
206
  woff: 'woff',
@@ -235,6 +242,7 @@ module Sass::Script::Functions
235
242
  # url('a.png', $base64: true) => url(data:image/png;base64,iVBORw...)
236
243
  def url(*paths)
237
244
  # debugger
245
+ $configHash ||= load_json(File.expand_path("#{File.dirname(options[:filename])}/sassmagic.json")) || Hash.new
238
246
  kwargs = paths.last.is_a?(Hash) ? paths.pop : {}
239
247
  raise Sass::SyntaxError, 'url() needs one path at least' if paths.empty?
240
248
 
@@ -242,7 +250,7 @@ module Sass::Script::Functions
242
250
  ts = timestamp(kwargs['timestamp'])
243
251
 
244
252
  paths = paths.map { |path| sass_to_ruby(path) }.flatten
245
- .map { |path| to_url(path, encode, ts) }
253
+ .map { |path| compress_img(path);to_url(path, encode, ts) }
246
254
 
247
255
  list(paths, :comma)
248
256
  end
@@ -250,7 +258,54 @@ module Sass::Script::Functions
250
258
 
251
259
 
252
260
  private
261
+ def compress_img(path)
262
+ #图片压缩
263
+ # debugger
264
+ # 未设置tinypngKye或者url图片,则不优化
265
+ if !$configHash.has_key?('tinypngKye') || path =~ /^(http:|https:)\/\//
266
+ return
267
+ end
268
+ output = path.gsub(/\.(png|jpg)$/,'_tinypng.\1')
269
+ if File.exist?(output)
270
+ path.gsub!(/\.(png|jpg)$/,'_tinypng.\1')
271
+ return
272
+ end
273
+
274
+ require "net/https"
275
+ require "uri"
276
+
277
+ key = $configHash['tinypngKye'] || ''
278
+ input = path
279
+ # real_path = File.expand_path("#{File.dirname(path)}/#{path}")
280
+ # output = "tiny-output.png"
253
281
 
282
+ uri = URI.parse("https://api.tinypng.com/shrink")
283
+
284
+ http = Net::HTTP.new(uri.host, uri.port)
285
+ http.use_ssl = true
286
+
287
+ # Uncomment below if you have trouble validating our SSL certificate.
288
+ # Download cacert.pem from: http://curl.haxx.se/ca/cacert.pem
289
+ # http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
290
+
291
+ request = Net::HTTP::Post.new(uri.request_uri)
292
+ request.basic_auth("api", key)
293
+
294
+ response = http.request(request, File.binread(input))
295
+ # debugger
296
+ if response.code == "201"
297
+ # Compression was successful, retrieve output from Location header.
298
+ # debugger
299
+ output = path
300
+ path.gsub!(/\.(png|jpg)$/,'_tinypng.\1')
301
+ # output['.png'] = '_tinypng.png'
302
+ # output['.jpg'] = '_tinypng.jpg'
303
+ File.binwrite(output, http.get(response["location"]).body)
304
+ else
305
+ # Something went wrong! You can parse the JSON body for details.
306
+ puts "Compression failed"
307
+ end
308
+ end
254
309
  def timestamp(ts)
255
310
  # no kwargs
256
311
  if ts.nil?
@@ -277,7 +332,6 @@ module Sass::Script::Functions
277
332
  def to_url(path, encode, ts)
278
333
  output = "url(#{path})"
279
334
  # debugger
280
- $configHash = load_json(File.expand_path("#{File.dirname(options[:filename])}/sassmagic.json")) || Hash.new
281
335
  if path.is_a?(String) && path =~ PATH_REGEX
282
336
 
283
337
  path, ext, query, anchor = $1 + $2, $2[1..-1].downcase.to_sym, $3, $4
@@ -285,6 +339,8 @@ module Sass::Script::Functions
285
339
  if MIME_TYPES.key? ext
286
340
  # 网络地址
287
341
  if path =~ /^(http:|https:)\/\//
342
+ # path = path.replace(/(http:\/\/)|(http:\/\/)/,'//')
343
+ path['http://'] = '//'
288
344
  output = output_path(path, ext, query, anchor, ts)
289
345
  else
290
346
  if $configHash["imageMaxSize"]
@@ -342,6 +398,7 @@ module Sass::Script::Functions
342
398
  if $configHash["imagesPath"].has_key?(File.expand_path("#{File.dirname(options[:filename])}/#{path}"))
343
399
  return $configHash["imagesPath"][File.expand_path("#{File.dirname(options[:filename])}/#{path}")]
344
400
  end
401
+
345
402
  # 调用上传任务
346
403
  # debugger
347
404
  nodetask = $configHash["imageLoader"] || false
data/lib/sassmagic.rb CHANGED
@@ -12,9 +12,12 @@ ENV['SASS_ENV'] ||= 'development'
12
12
  # end
13
13
 
14
14
  $:.unshift"#{File.dirname(__FILE__)}"
15
- # require 'debugger'
15
+ require 'debugger'
16
16
  # debugger
17
17
  # require 'sass'
18
18
  require 'sassmagic/utils'
19
+ require 'sassmagic/remote'
19
20
  require 'sassmagic/reset'
20
21
 
22
+
23
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sassmagic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-04 00:00:00.000000000 Z
12
+ date: 2015-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sass
@@ -41,6 +41,7 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
43
  - bin/sassmagic
44
+ - lib/sassmagic/remote.rb
44
45
  - lib/sassmagic/reset.rb
45
46
  - lib/sassmagic/utils.rb
46
47
  - lib/sassmagic.rb