Module: Sass::Script::Functions
- Defined in:
- lib/SassyExport.rb
Instance Method Summary collapse
- #opts(value) ⇒ Object
-
#recurs_to_a(array) ⇒ Array
Recursive parse to array.
-
#recurs_to_h(hash) ⇒ Hash
Recursive parse to hash.
-
#SassyExport(path, map, pretty, debug, use_env) ⇒ String
Convert passed map to json and write to <path>/<filename>.<ext>.
-
#strip_quotes(value) ⇒ Object
Unquote strings.
Instance Method Details
#opts(value) ⇒ Object
32 33 34 35 |
# File 'lib/SassyExport.rb', line 32 def opts(value) value. = value end |
#recurs_to_a(array) ⇒ Array
Recursive parse to array
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/SassyExport.rb', line 60 def recurs_to_a(array) if array.is_a?(Array) array.map do |l| case l when Sass::Script::Value::Map # If map, recurse to hash l = recurs_to_h(l) when Sass::Script::Value::List # If list, recurse to array l = recurs_to_a(l) when Sass::Script::Value::Bool # Convert to bool l = l.to_bool when Sass::Script::Value::Number # If it's a unitless number, convert to ruby val, else convert to string l = l.unitless? ? l.value : strip_quotes(l) when Sass::Script::Value::Color # Get hex/rgba value for color l = l.inspect else # Convert to string l = strip_quotes(l) end l end else recurs_to_a(array.to_a) end end |
#recurs_to_h(hash) ⇒ Hash
Recursive parse to hash
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/SassyExport.rb', line 99 def recurs_to_h(hash) if hash.is_a?(Hash) hash.inject({}) do |h, (k, v)| case v when Sass::Script::Value::Map # If map, recurse to hash h[strip_quotes(k)] = recurs_to_h(v) when Sass::Script::Value::List # If list, recurse to array h[strip_quotes(k)] = recurs_to_a(v) when Sass::Script::Value::Bool # Convert to bool h[strip_quotes(k)] = v.to_bool when Sass::Script::Value::Number # If it's a unitless number, convert to ruby val, else convert to string h[strip_quotes(k)] = v.unitless? ? v.value : strip_quotes(v) when Sass::Script::Value::Color # Get hex/rgba value for color h[strip_quotes(k)] = v.inspect else # Convert to string h[strip_quotes(k)] = strip_quotes(v) end h end else recurs_to_h(hash.to_h) end end |
#SassyExport(path, map, pretty, debug, use_env) ⇒ String
Convert passed map to json and write to <path>/<filename>.<ext>
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/SassyExport.rb', line 30 def SassyExport(path, map, pretty, debug, use_env) def opts(value) value. = value end # # Unquote strings # # @param {*} val # # @return Unquoted string, if String was passed. Else if will # just return the passed value. # def strip_quotes(value) if value.is_a?(String) || value.is_a?(Sass::Script::Value::String) unquote(value) else value end end # # Recursive parse to array # # @param {Array} array - Array containing SassScript values # # @return {Array} - Array containing parsed SassScript->Ruby values # def recurs_to_a(array) if array.is_a?(Array) array.map do |l| case l when Sass::Script::Value::Map # If map, recurse to hash l = recurs_to_h(l) when Sass::Script::Value::List # If list, recurse to array l = recurs_to_a(l) when Sass::Script::Value::Bool # Convert to bool l = l.to_bool when Sass::Script::Value::Number # If it's a unitless number, convert to ruby val, else convert to string l = l.unitless? ? l.value : strip_quotes(l) when Sass::Script::Value::Color # Get hex/rgba value for color l = l.inspect else # Convert to string l = strip_quotes(l) end l end else recurs_to_a(array.to_a) end end # # Recursive parse to hash # # @param {Hash} hash - Hash containing SassScript values # # @return {Hash} - Hash containing parsed SassScript->Ruby values # def recurs_to_h(hash) if hash.is_a?(Hash) hash.inject({}) do |h, (k, v)| case v when Sass::Script::Value::Map # If map, recurse to hash h[strip_quotes(k)] = recurs_to_h(v) when Sass::Script::Value::List # If list, recurse to array h[strip_quotes(k)] = recurs_to_a(v) when Sass::Script::Value::Bool # Convert to bool h[strip_quotes(k)] = v.to_bool when Sass::Script::Value::Number # If it's a unitless number, convert to ruby val, else convert to string h[strip_quotes(k)] = v.unitless? ? v.value : strip_quotes(v) when Sass::Script::Value::Color # Get hex/rgba value for color h[strip_quotes(k)] = v.inspect else # Convert to string h[strip_quotes(k)] = strip_quotes(v) end h end else recurs_to_h(hash.to_h) end end # Assert types assert_type path, :String, :path assert_type map, :Map, :map assert_type pretty, :Bool, :pretty assert_type debug, :Bool, :debug assert_type use_env, :Bool, :use_env # Parse to bool pretty = pretty.to_bool debug = debug.to_bool use_env = use_env.to_bool # Parse to string path = unquote(path).to_s # Define root path up to current working directory root = use_env ? ENV["PWD"] : Dir.pwd # Define dir path dir_path = root dir_path += path # Get filename filename = File.basename(dir_path, ".*") # Get extension ext = File.extname(path) # Normalize windows path dir_path = Sass::Util.pathname(dir_path) # Check if directory exists, if not make directory dir = File.dirname(dir_path) unless File.exists?(dir) FileUtils.mkdir_p(dir) puts "Directory was not found. Created new directory: #{dir}" end # Get map values map = opts(Sass::Script::Value::Map.new(map.value)) # Recursive convert map to hash hash = recurs_to_h(map) # Convert hash to pretty json if pretty pretty ? json = JSON.pretty_generate(hash) : json = JSON.generate(hash) # If we're turning it straight to js put a variable name in front json = "var " + filename + " = " + json if ext == '.js' # Define flags flag = "w" flag = "wb" if Sass::Util.windows? && [:unix_newlines] # Ppen file (create new file if file does not exist), write string to root/path/to/filename.json File.open("#{dir_path}", flag) do |file| file.set_encoding(json.encoding) unless Sass::Util.ruby1_8? file.print(json) end # Define message # debug_msg = "#{ext == '.json' ? 'JSON' : 'JavaScript'} was successfully exported to #{dir_path}" debug_msg = "\e[0;32m export\e[0m #{ext == '.json' ? 'JSON' : 'JavaScript'} to #{dir_path}" # Print path string if debug puts debug_msg if debug # Return succcess string opts(Sass::Script::Value::String.new(debug_msg)) end |
#strip_quotes(value) ⇒ Object
Unquote strings
45 46 47 48 49 50 51 |
# File 'lib/SassyExport.rb', line 45 def strip_quotes(value) if value.is_a?(String) || value.is_a?(Sass::Script::Value::String) unquote(value) else value end end |