sass-json-vars 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sass-json-vars.rb +1 -2
- data/lib/sass-json-vars/importer.rb +40 -27
- data/lib/sass-json-vars/monkeypatch.rb +19 -0
- data/lib/sass-json-vars/version.rb +1 -1
- data/readme.md +2 -0
- data/test/fixtures/lists/style.scss +1 -1
- data/test/fixtures/maps/style.scss +2 -2
- data/test/fixtures/sass/style.sass +4 -0
- data/test/fixtures/sass/variables.json +4 -0
- data/test/fixtures/strings/style.scss +1 -1
- data/test/sass-json-vars_test.rb +1 -26
- data/test/sass_test.rb +10 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4ce9ac9d7f27e142c4220cb100d7e2b14c6ea8a
|
4
|
+
data.tar.gz: 2dfca0a31019e3e0850c6e6907d33aef2e73febe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9bf0243a85026b1f841e25a2bcae4fd029e6989c300076e889a31a00b55be05ccab63cbcaed3f9e2b680546d98bcd036fa8f1d49f79be503bd80a4025399dd2d
|
7
|
+
data.tar.gz: 7499e303158c6f7f9497cccaf25359121a9e011f3bf26ebdb47f6090d50e341c8180bd983d2d1d53fd875553b3703b907b7bfec35cfee70213e67faa69629d7a
|
data/lib/sass-json-vars.rb
CHANGED
@@ -1,51 +1,64 @@
|
|
1
1
|
require 'sass'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
class SassJSONVars::Importer < Sass::Importers::
|
4
|
+
class SassJSONVars::Importer < Sass::Importers::Filesystem
|
5
5
|
|
6
|
-
def
|
7
|
-
|
8
|
-
self.class.sass_engine(uri, self, options)
|
9
|
-
else
|
10
|
-
nil
|
11
|
-
end
|
6
|
+
def watched_file?(uri)
|
7
|
+
filename =~ /\.json$/ && filename.start_with?(root + File::SEPARATOR)
|
12
8
|
end
|
13
9
|
|
14
|
-
def
|
15
|
-
|
10
|
+
def extensions
|
11
|
+
{'json' => :scss}
|
16
12
|
end
|
17
13
|
|
18
|
-
def
|
19
|
-
|
14
|
+
def find(name, options)
|
15
|
+
File.extname(name) == '.json' ? super(name, options) : nil
|
20
16
|
end
|
21
17
|
|
22
|
-
def
|
23
|
-
|
18
|
+
def find_relative(name, base, options)
|
19
|
+
File.extname(name) == '.json' ? super(name, base, options) : nil
|
24
20
|
end
|
25
21
|
|
26
|
-
def
|
27
|
-
|
22
|
+
def sass_engine(uri, options)
|
23
|
+
json = JSON.parse(IO.read(uri))
|
24
|
+
|
25
|
+
variables = json.map do |name, value|
|
26
|
+
output = _convert_to_sass(value);
|
27
|
+
"$#{name}: #{output}"
|
28
|
+
end
|
29
|
+
|
30
|
+
Sass::Engine.new(variables.join("\n"))
|
28
31
|
end
|
29
32
|
|
30
|
-
|
33
|
+
private
|
34
|
+
|
35
|
+
def _find(dir, name, options)
|
36
|
+
full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options))
|
37
|
+
return unless full_filename && File.readable?(full_filename)
|
38
|
+
|
39
|
+
options[:syntax] = syntax
|
40
|
+
options[:filename] = full_filename
|
41
|
+
options[:importer] = self
|
42
|
+
|
43
|
+
sass_engine(full_filename, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def _convert_to_sass(item)
|
31
47
|
if item.is_a? Array
|
32
|
-
|
48
|
+
_make_list(item)
|
33
49
|
elsif item.is_a? Hash
|
34
|
-
|
50
|
+
_make_map(item)
|
35
51
|
else
|
36
52
|
item
|
37
53
|
end
|
38
54
|
end
|
39
55
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
variables = json.map do |name, value|
|
44
|
-
output = self.convert_to_sass(value);
|
45
|
-
|
46
|
-
"$#{name}: #{output}"
|
47
|
-
end
|
56
|
+
def _make_list(item)
|
57
|
+
'(' + item.map { |i| _convert_to_sass(i) }.join(',') + ')'
|
58
|
+
end
|
48
59
|
|
49
|
-
|
60
|
+
def _make_map(item)
|
61
|
+
'(' + item.map {|key, value| key.to_s + ':' + _convert_to_sass(value) }.join(',') + ')'
|
50
62
|
end
|
63
|
+
|
51
64
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'sass-json-vars/importer'
|
2
|
+
|
3
|
+
# Taken from https://github.com/chriseppstein/sass-css-importer/blob/master/lib/sass/css_importer/monkey_patches.rb
|
4
|
+
# TODO: This feels wrong, surely there must be a better way to handle this
|
5
|
+
|
6
|
+
class Sass::Engine
|
7
|
+
alias initialize_without_json_importer initialize
|
8
|
+
|
9
|
+
def initialize(template, options={})
|
10
|
+
initialize_without_json_importer(template, options)
|
11
|
+
|
12
|
+
json_importer = self.options[:load_paths].find {|lp| lp.is_a?(SassJSONVars::Importer) }
|
13
|
+
|
14
|
+
unless json_importer
|
15
|
+
root = File.dirname(options[:filename] || ".")
|
16
|
+
self.options[:load_paths] << SassJSONVars::Importer.new(root)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/readme.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Sass JSON Vars
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/vigetlabs/sass-json-vars.png?branch=master)](https://travis-ci.org/vigetlabs/sass-json-vars) [![Gem Version](https://badge.fury.io/rb/sass-json-vars.png)](http://badge.fury.io/rb/sass-json-vars)
|
4
|
+
|
3
5
|
`@import` json data into Sass `$variables`.
|
4
6
|
|
5
7
|
### Usage
|
data/test/sass-json-vars_test.rb
CHANGED
@@ -3,32 +3,7 @@ require 'sass'
|
|
3
3
|
require 'sass-json-vars'
|
4
4
|
|
5
5
|
class SassJSONVarsTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
flattened = SassJSONVars::Importer.convert_to_sass(['#c33', '#33c'])
|
8
|
-
assert_equal '(#c33,#33c)', flattened
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_can_convert_nested_arrays_to_lists
|
12
|
-
flattened = SassJSONVars::Importer.convert_to_sass([['#c33', 'red'], '#33c'])
|
13
|
-
assert_equal '((#c33,red),#33c)', flattened
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_can_convert_hashes_to_maps
|
17
|
-
flattened = SassJSONVars::Importer.convert_to_sass({ red: '#c33' })
|
18
|
-
assert_equal '(red:#c33)', flattened
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_can_convert_nested_hashes_to_maps
|
22
|
-
flattened = SassJSONVars::Importer.convert_to_sass({ colors: { red: '#c33' } })
|
23
|
-
assert_equal '(colors:(red:#c33))', flattened
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_can_convert_complicated_objects
|
27
|
-
flattened = SassJSONVars::Importer.convert_to_sass({ colors: ['red', 'blue', 'green'] })
|
28
|
-
assert_equal '(colors:(red,blue,green))', flattened
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_strings
|
6
|
+
def test_strings
|
32
7
|
scss = Sass.compile_file("test/fixtures/strings/style.scss")
|
33
8
|
assert_equal "body {\n color: #cc3333; }\n", scss
|
34
9
|
end
|
data/test/sass_test.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sass-json-vars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Hunzaker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sass
|
@@ -34,15 +34,19 @@ files:
|
|
34
34
|
- Rakefile
|
35
35
|
- lib/sass-json-vars.rb
|
36
36
|
- lib/sass-json-vars/importer.rb
|
37
|
+
- lib/sass-json-vars/monkeypatch.rb
|
37
38
|
- lib/sass-json-vars/version.rb
|
38
39
|
- readme.md
|
39
40
|
- test/fixtures/lists/style.scss
|
40
41
|
- test/fixtures/lists/variables.json
|
41
42
|
- test/fixtures/maps/style.scss
|
42
43
|
- test/fixtures/maps/variables.json
|
44
|
+
- test/fixtures/sass/style.sass
|
45
|
+
- test/fixtures/sass/variables.json
|
43
46
|
- test/fixtures/strings/style.scss
|
44
47
|
- test/fixtures/strings/variables.json
|
45
48
|
- test/sass-json-vars_test.rb
|
49
|
+
- test/sass_test.rb
|
46
50
|
homepage: https://github.com/vigetlabs/sass-json-vars
|
47
51
|
licenses:
|
48
52
|
- MIT
|
@@ -72,6 +76,9 @@ test_files:
|
|
72
76
|
- test/fixtures/lists/variables.json
|
73
77
|
- test/fixtures/maps/style.scss
|
74
78
|
- test/fixtures/maps/variables.json
|
79
|
+
- test/fixtures/sass/style.sass
|
80
|
+
- test/fixtures/sass/variables.json
|
75
81
|
- test/fixtures/strings/style.scss
|
76
82
|
- test/fixtures/strings/variables.json
|
77
83
|
- test/sass-json-vars_test.rb
|
84
|
+
- test/sass_test.rb
|