sassy-export 2.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24ade834a2b95503916be110a53a1216938b4ee0
4
+ data.tar.gz: a0853c9d487d1bcad0dada0a174332497e6a04ed
5
+ SHA512:
6
+ metadata.gz: 5eacbc52086517f95edfe3250ba085e0caf00ecb281f6bc25b5190b22224009ee16283e4742d3b5f1e4f0a968f4bce278a8a6b26b08a23d5ed8d2072e745dfec
7
+ data.tar.gz: 3044db63bf3479a0e44dfc4bbb2d42a2d5b47b046790487bffaecb194351660dae3f7264b2f1cfa4de1a48d087210ed9416eae8f2e60ab346bcc769868e012a9
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Sassy Export [![Gem Version](https://badge.fury.io/rb/sassy-export.svg)](http://badge.fury.io/rb/sassy-export)
2
+
3
+ Sassy Export is a lightweight Sass extension that allows you to export an encoded Sass map into an external JSON file. Use it anyway you like.
4
+
5
+ ## Installation
6
+
7
+ 1. `gem install sassy-export`
8
+ 2. Add `require "sassy-export"` to your `config.rb`
9
+ 3. Import into your stylesheet with `@import "sassy-export";`
10
+
11
+ ## Documentation
12
+
13
+ #### Setup
14
+
15
+ Our file structure,
16
+ ```
17
+ root
18
+ ├── sass
19
+ │ ├── style.scss
20
+ ├── json
21
+ └── config.rb
22
+ ```
23
+
24
+ Sass,
25
+ ```scss
26
+ // sass/style.scss
27
+
28
+ @import "sassy-export";
29
+
30
+ $map: (
31
+ hello: world,
32
+ );
33
+
34
+ ///
35
+ /// Convert passed map to json and write to <path>/<filename>.<ext>
36
+ ///
37
+ /// @param {String} $path - Directory path and filename
38
+ /// @param {Map} $map - Map to convert to json
39
+ /// @param {Bool} $pretty - Pretty print json
40
+ /// @param {Bool} $debug - Print debug string with path
41
+ /// @param {Bool} $use_env - Use ENV["PWD"] for current directory instead of Dir.pwd
42
+ ///
43
+ /// @return {String} - Write file to path
44
+ ///
45
+ @include sassy-export("json/test.json", $map, true, true, false);
46
+ ```
47
+
48
+ #### Result
49
+
50
+ New JSON file is created at `json/hello.json` (`$path` is relative to where your `config.rb` is located). Simply calling `@include sassy-export("hello.json", $map)` will write to your working directory.
51
+ ```json
52
+ {
53
+ "hello": "world"
54
+ }
55
+ ```
56
+
57
+ #### Breakdown
58
+
59
+ The `sassy-export()` mixin takes a `\<directory\>/\<filename\>.\<ext\>` `$path` and a Sass `$map` as arguments. You can export straight to a JavaScript object by using the extension `.js` instead of `.json` for the output `$path`.
60
+
61
+ There are also optional arguments: `$pretty` which defaults to `false`, but will print pretty JSON (nicely indented) if set to `true`; and `$debug` which will print debug information if set to `true`.
62
+
63
+ It converts the `$map` into either a JSON map or Javascript object through Ruby, then creates a new directory/file (or updates an existing directory/file), and writes the contents of the JSON string to it.
64
+
65
+ Contributions are welcome. If you believe that you could improve the small amount of code here, feel free to.
66
+
67
+ Enjoy.
@@ -0,0 +1,155 @@
1
+ module SassyExport
2
+ module Exporter
3
+ BLACK = "\e[30m"
4
+ RED = "\e[31m"
5
+ GREEN = "\e[32m"
6
+ YELLOW = "\e[33m"
7
+ BLUE = "\e[34m"
8
+ MAGENTA = "\e[35m"
9
+ CYAN = "\e[36m"
10
+ WHITE = "\e[37m"
11
+
12
+ def self.declare(*args)
13
+ Sass::Script::Functions.declare *args
14
+ end
15
+
16
+ def export(path, map, pretty, debug, use_env)
17
+ assert_type path, :String, :path
18
+ assert_type map, :Map, :map
19
+ assert_type pretty, :Bool, :pretty
20
+ assert_type debug, :Bool, :debug
21
+ assert_type use_env, :Bool, :use_env
22
+
23
+ pretty = pretty.to_bool
24
+ debug = debug.to_bool
25
+ use_env = use_env.to_bool
26
+ path = unquote(path).to_s
27
+ root = use_env ? ENV["PWD"] : Dir.pwd
28
+ dir = Pathname.new(root) + path
29
+ file = File.basename dir, ".*"
30
+ ext = File.extname path
31
+ message = "#{javascript?(ext) ? "JS" : "JSON"} to #{path}"
32
+ map = Sass::Script::Value::Map.new map.value
33
+ hash = map_to_h map
34
+
35
+ output = if pretty
36
+ JSON.pretty_generate hash
37
+ else
38
+ JSON.generate hash
39
+ end
40
+
41
+ if javascript? ext
42
+ output = "var #{file} = #{output}"
43
+ end
44
+
45
+ if debug
46
+ log :debug, %Q{
47
+ file: #{dir}
48
+ map: #{map.inspect}
49
+ hash: #{hash}
50
+ output: #{output}
51
+ }, :blue
52
+ end
53
+
54
+ write_dir Sass::Util.pathname(dir)
55
+
56
+ if write_file dir, output
57
+ log :export, message
58
+ else
59
+ message = "could not export #{message}"
60
+ log :error, message, :red
61
+ end
62
+
63
+ Sass::Script::Value::String.new "exported #{message}"
64
+ end
65
+ declare :export, [:path, :map, :pretty, :debug, :use_env]
66
+
67
+ private
68
+
69
+ def log(action, message, color = :green)
70
+ puts %Q(#{self.class.const_get(color.to_s.upcase)} #{action.to_s.rjust(8)}\e[0m #{message})
71
+ end
72
+
73
+ def javascript?(ext)
74
+ ext == ".js"
75
+ end
76
+
77
+ def write_file(path, output)
78
+ flag = Sass::Util.windows? ? "wb" : "w"
79
+ begin
80
+ File.open "#{path}", flag do |file|
81
+ file.set_encoding output.encoding unless Sass::Util.ruby1_8?
82
+ file.print output
83
+ end
84
+ true
85
+ rescue
86
+ false
87
+ end
88
+ end
89
+
90
+ def write_dir(path)
91
+ dir = File.dirname path
92
+
93
+ unless File.exists?(dir)
94
+ FileUtils.mkdir_p dir
95
+ log :create, dir, :yellow
96
+ end
97
+ end
98
+
99
+ def strip_quotes(value)
100
+ if value.is_a?(String) || value.is_a?(Sass::Script::Value::String)
101
+ unquote(value)
102
+ else
103
+ value
104
+ end
105
+ end
106
+
107
+ def list_to_a(array)
108
+ if array.is_a?(Array)
109
+ array.map do |l|
110
+ case l
111
+ when Sass::Script::Value::Map
112
+ l = map_to_h(l)
113
+ when Sass::Script::Value::List
114
+ l = list_to_a(l)
115
+ when Sass::Script::Value::Bool
116
+ l = l.to_bool
117
+ when Sass::Script::Value::Number
118
+ l = l.unitless? ? l.value : strip_quotes(l)
119
+ when Sass::Script::Value::Color
120
+ l = l.inspect
121
+ else
122
+ l = strip_quotes(l)
123
+ end
124
+ l
125
+ end
126
+ else
127
+ list_to_a(array.to_a)
128
+ end
129
+ end
130
+
131
+ def map_to_h(hash)
132
+ if hash.is_a?(Hash)
133
+ hash.inject({}) do |h, (k, v)|
134
+ case v
135
+ when Sass::Script::Value::Map
136
+ h[strip_quotes(k)] = map_to_h(v)
137
+ when Sass::Script::Value::List
138
+ h[strip_quotes(k)] = list_to_a(v)
139
+ when Sass::Script::Value::Bool
140
+ h[strip_quotes(k)] = v.to_bool
141
+ when Sass::Script::Value::Number
142
+ h[strip_quotes(k)] = v.unitless? ? v.value : strip_quotes(v)
143
+ when Sass::Script::Value::Color
144
+ h[strip_quotes(k)] = v.inspect
145
+ else
146
+ h[strip_quotes(k)] = strip_quotes(v)
147
+ end
148
+ h
149
+ end
150
+ else
151
+ map_to_h(hash.to_h)
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,3 @@
1
+ module SassyExport
2
+ VERSION = "2.0.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ require "sass"
2
+ require "json"
3
+
4
+ require_relative "sassy-export/version"
5
+ require_relative "sassy-export/exporter"
6
+
7
+ base_directory = File.expand_path File.join(File.dirname(__FILE__), "..")
8
+ sassy_export_stylesheets_path = File.join base_directory, "stylesheets"
9
+
10
+ if (defined? Compass)
11
+ Compass::Frameworks.register "sassy-export",
12
+ :path => base_directory
13
+ else
14
+ ENV["SASS_PATH"] = [ENV["SASS_PATH"], sassy_export_stylesheets_path]
15
+ .compact.join File::PATH_SEPARATOR
16
+ end
17
+
18
+ module SassyExport
19
+ Sass::Script::Functions.send :include, SassyExport::Exporter
20
+ end
@@ -0,0 +1,18 @@
1
+ ///
2
+ /// Convert passed map to json and write to <path>/<filename>.<ext>
3
+ ///
4
+ /// @param {String} $path - Directory path and filename
5
+ /// @param {Map} $map - Map to convert to json
6
+ /// @param {Bool} $pretty - Pretty print json
7
+ /// @param {Bool} $debug - Print debug string with path
8
+ /// @param {Bool} $use_env - Use ENV["PWD"] for current directory instead of Dir.pwd
9
+ ///
10
+ /// @return {String} - Write file to path
11
+ ///
12
+ @mixin sassy-export($path, $map, $pretty: false, $debug: false, $use_env: false)
13
+ @at-root
14
+ @if $debug
15
+ /*! #{export($path, $map, $pretty, $debug, $use_env)} */
16
+ @else
17
+ %sassy-export
18
+ /*! #{export($path, $map, $pretty, $debug, $use_env)} */
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sassy-export
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ezekiel Gabrielse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sass
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ description: Sassy Export is a lightweight Sass extension that allows you to export
42
+ an encoded Sass map into an external JSON file.
43
+ email:
44
+ - ezekg@yahoo.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - README.md
50
+ - lib/sassy-export/exporter.rb
51
+ - lib/sassy-export/version.rb
52
+ - lib/sassy_export.rb
53
+ - stylesheets/_sassy-export.sass
54
+ homepage: https://github.com/ezekg/sassy-export/
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ - stylesheets
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.4.6
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Sassy Export allows you to export a Sass map into an external JSON file.
79
+ test_files: []