Module: Sass::Script::Functions
- Defined in:
- lib/SassyExport.rb
Overview
SassyExport : convert passed map to json and write to path/to/filename.json
Instance Method Summary collapse
- #opts(value) ⇒ Object
-
#recurs_to_a(array) ⇒ Object
recursive parse to array.
-
#recurs_to_h(hash) ⇒ Object
recursive parse to hash.
- #SassyExport(path, map, pretty, debug) ⇒ Object
-
#u(s) ⇒ Object
unquote strings.
Instance Method Details
#opts(value) ⇒ Object
26 27 28 29 |
# File 'lib/SassyExport.rb', line 26 def opts(value) value. = value end |
#recurs_to_a(array) ⇒ Object
recursive parse to array
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 |
# File 'lib/SassyExport.rb', line 37 def recurs_to_a(array) if array.is_a?(Array) array.map do | l | if l.is_a?(Sass::Script::Value::Map) # if map, recurse to hash l = recurs_to_h(l) elsif l.is_a?(Sass::Script::Value::List) # if list, recurse to array l = recurs_to_a(l) elsif l.is_a?(Sass::Script::Value::Bool) # convert to bool l = l.to_bool elsif l.is_a?(Sass::Script::Value::Number) # if it's a unitless number, convert to ruby val if l.unitless? l = l.value # else convert to string else l = u(l) end elsif l.is_a?(Sass::Script::Value::Color) # get hex/rgba value for color l = l.inspect else # convert to string l = u(l) end l end else recurs_to_a(array.to_a) end end |
#recurs_to_h(hash) ⇒ Object
recursive parse to hash
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 |
# File 'lib/SassyExport.rb', line 72 def recurs_to_h(hash) if hash.is_a?(Hash) hash.inject({}) do | h, (k, v) | if v.is_a?(Sass::Script::Value::Map) h[u(k)] = recurs_to_h(v) elsif v.is_a?(Sass::Script::Value::List) h[u(k)] = recurs_to_a(v) elsif v.is_a?(Sass::Script::Value::Bool) h[u(k)] = v.to_bool elsif v.is_a?(Sass::Script::Value::Number) if v.unitless? h[u(k)] = v.value else h[u(k)] = u(v) end elsif v.is_a?(Sass::Script::Value::Color) h[u(k)] = v.inspect else h[u(k)] = u(v) end h end else recurs_to_h(hash.to_h) end end |
#SassyExport(path, map, pretty, debug) ⇒ Object
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 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 |
# File 'lib/SassyExport.rb', line 24 def SassyExport(path, map, pretty, debug) def opts(value) value. = value end # unquote strings def u(s) unquote(s) end # recursive parse to array def recurs_to_a(array) if array.is_a?(Array) array.map do | l | if l.is_a?(Sass::Script::Value::Map) # if map, recurse to hash l = recurs_to_h(l) elsif l.is_a?(Sass::Script::Value::List) # if list, recurse to array l = recurs_to_a(l) elsif l.is_a?(Sass::Script::Value::Bool) # convert to bool l = l.to_bool elsif l.is_a?(Sass::Script::Value::Number) # if it's a unitless number, convert to ruby val if l.unitless? l = l.value # else convert to string else l = u(l) end elsif l.is_a?(Sass::Script::Value::Color) # get hex/rgba value for color l = l.inspect else # convert to string l = u(l) end l end else recurs_to_a(array.to_a) end end # recursive parse to hash def recurs_to_h(hash) if hash.is_a?(Hash) hash.inject({}) do | h, (k, v) | if v.is_a?(Sass::Script::Value::Map) h[u(k)] = recurs_to_h(v) elsif v.is_a?(Sass::Script::Value::List) h[u(k)] = recurs_to_a(v) elsif v.is_a?(Sass::Script::Value::Bool) h[u(k)] = v.to_bool elsif v.is_a?(Sass::Script::Value::Number) if v.unitless? h[u(k)] = v.value else h[u(k)] = u(v) end elsif v.is_a?(Sass::Script::Value::Color) h[u(k)] = v.inspect else h[u(k)] = u(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 # parse to bool pretty = pretty.to_bool debug = debug.to_bool # parse to string path = unquote(path).to_s # define root path up to current working directory root = 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 ext == '.js' ? json = "var " + filename + " = " + json : json = json # define flags flag = 'w' flag = 'wb' if Sass::Util.windows? && [:unix_newlines] # open 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}" # print path string if debug puts debug_msg if debug # return succcess string opts(Sass::Script::Value::String.new(debug_msg)) end |
#u(s) ⇒ Object
unquote strings
32 33 34 |
# File 'lib/SassyExport.rb', line 32 def u(s) unquote(s) end |