-
Notifications
You must be signed in to change notification settings - Fork 18
/
to_stylus.rb
276 lines (237 loc) · 7.84 KB
/
to_stylus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env ruby
# Convert SASS/SCSS to Stylus
# Initial work by Andrey Popp (https://github.com/andreypopp)
require 'sass'
require 'yaml'
class ToStylus < Sass::Tree::Visitors::Base
@@functions = YAML::load_file(File.join(__dir__ , 'functions.yml'))
def self.convert(file)
engine = Sass::Engine.for_file(file, {})
tree = engine.to_tree
visit(tree)
end
def visit(node)
if self.class.respond_to? :node_name
method = "visit_#{node.class.node_name}"
else
method = "visit_#{node_name node}"
end
return if node.is_a?(Sass::Tree::CharsetNode)
if self.respond_to?(method, true)
self.send(method, node) {visit_children(node)}
else
if self.class.respond_to? :node_name
raise "unhandled node: '#{node.class.node_name}'"
else
raise "unhandled node: '#{node_name node}'"
end
end
end
def visit_children(node)
@indent += 1
super(node)
@indent -= 1
end
def determine_support(node, is_value)
is_value ? (node_class = node.value) : (node_class = node.expr)
if node_class.is_a? Sass::Script::Tree::Funcall
@@functions['disabled_functions'].each do |func|
if !node_class.inspect.match(func.to_s).nil?
@errors.push("//#{func}: line #{node.line} in your Sass file")
emit "//Function #{func} is not supported in Stylus"
return func
end
end
return
end
end
def visit_prop(node, output="")
func_support = determine_support(node, true)
if !node.children.empty?
output << "#{node.name.join('')}-"
unless node.value.to_sass.empty?
#for nested scss with values, change the last "-" in the output to a ":" to format output correctly
if node.value.is_a? Sass::Script::Tree::Operation
func_output = "#{output}(#{node.value.to_sass})".sub(/(.*)-/, '\1: ').gsub("\#{","{")
elsif node.value.is_a?(Sass::Script::Tree::UnaryOperation) && node.value.operator.to_s == 'minus'
func_output = "#{output}".sub(/(.*)-/, '\1: ') <<"-1*#{node.value.operand.inspect}".gsub("\#{","{")
else
func_output = "#{output}#{node.value.to_sass}".sub(/(.*)-/, '\1: ').gsub("\#{","{")
end
func_support.nil? ? (emit func_output) : (emit "//" << func_output)
end
node.children.each do |child|
visit_prop(child,output)
end
else
unless node.is_a? Sass::Tree::CommentNode
"#{node.name.join("")[0]}" == "#" ?
node_name = "#{output}{#{node.name-[""]}}:".tr('[]','') : node_name = "#{output}#{node.name.join('')}:"
if node.value.is_a? Sass::Script::Tree::Operation
func_output = node_name << " (#{node.value.to_sass})".gsub("\#{", "{")
elsif node.value.is_a?(Sass::Script::Tree::UnaryOperation) && node.value.operator.to_s == 'minus'
func_output = node_name << " -1*#{node.value.operand.inspect}"
else
func_output = node_name << " #{node.value.to_sass}".gsub("\#{", "{")
end
func_support.nil? ? (emit func_output) : (emit "//" << func_output)
else
visit(node)
end
end
end
def visit_variable(node)
func_support = determine_support(node, false)
output = "$#{node.name} = #{node.expr.to_sass}"
func_support.nil? ? (emit output) : (emit "//" << output)
end
def render_arg(arg)
if arg.is_a? Array
var = arg[0]
default = arg[1]
if default
"#{arg[0].to_sass} = #{arg[1].to_sass}"
else
var.to_sass
end
else
arg.to_sass
end
end
def render_args(args)
args.map { |a| render_arg(a) }.join(', ')
end
def emit(line)
line = (' ' * @indent) + line
@lines.push line
end
def visit_if(node, isElse = false)
line = []
line.push 'else' if isElse
line.push "if #{node.expr.to_sass}" if node.expr
emit line.join(' ')
visit_children(node)
visit_if(node.else, true) if node.else
end
def visit_return(node)
func_support = determine_support(node, false)
output = node.expr.to_sass
func_support.nil? ? (emit output) : (emit "//" << output)
end
def visit_comment(node)
node.invisible? ? lines = node.to_sass.lines : lines = node.value.first.split("\n")
lines.each { |line| emit line.tr("\n", "")}
end
def visit_mixindef(node)
emit "#{node.name}(#{render_args(node.args)})"
visit_children node
end
def visit_media(node)
emit "@media #{node.query.map{|i| i.inspect}.join}".gsub! /"/, ""
visit_children node
end
def visit_content(node)
emit '{block}'
end
def visit_mixin(node)
emit "#{node.name}(#{render_args(node.args)})"
end
def visit_import(node)
emit "@import '#{node.imported_filename}'"
end
def visit_cssimport(node)
if node.to_sass.include?("http://") && !node.to_sass.include?("url")
emit "@import url(#{node.uri})"
elsif(node.to_sass.index("\"http") || node.to_sass.index("\'http"))
emit "#{node.to_sass}".chomp!
elsif(node.to_sass.index("http"))
emit "@import #{node.uri}".gsub("(", "(\"").gsub(")", "\")")
else
emit "#{node.to_sass}".chomp!
end
end
def visit_extend(node)
emit "#{node.to_sass}".gsub("%","$").chomp!
end
def visit_function(node)
if node.splat.nil?
emit "#{node.name}(#{render_args(node.args)})"
else
node.args.push([node.splat, nil])
emit "#{node.name}(#{render_args(node.args)}...)"
end
visit_children node
end
def visit_rule(node)
rule = (node.rule.length == 1 && node.rule[0].is_a?(String)) ? node.rule[0] : node.to_sass.lines[0]
emit "#{rule}".gsub("\#{", "{").gsub("%", "$").chomp
visit_children node
end
def for_helper(node, from_or_to)
@@functions['disabled_functions'].each do |func|
unless (from_or_to).inspect.match(func.to_s).nil?
@errors.push("//#{func}: line #{node.line} in your Sass file")
emit "//Function #{func} is not supported in Stylus"
end
end
end
def visit_for(node)
(node.from.is_a? Sass::Script::Tree::Funcall) ? for_helper(node, node.from) : nil
from = node.from.to_sass
(node.to.is_a? Sass::Script::Tree::Funcall) ? for_helper(node, node.to) : nil
temp_to = node.to.to_sass
(node.to.is_a? Sass::Script::Tree::Literal) ? exclusive_to = temp_to.to_i - 1 : exclusive_to = "(#{temp_to}) - 1"
node.exclusive ? to = exclusive_to : to = temp_to
emit "for $#{node.var} in (#{from})..(#{to})"
visit_children node
end
def visit_directive(node)
emit "#{node.name}"
visit_children node
end
def comment_out(node)
node.to_sass.lines.each {|l| emit "//#{l}".chomp }
end
def visit_each(node)
if node.vars.length == 1
emit "for $#{node.vars.first} in #{node.list.to_sass}".gsub(",","")
visit_children node
else
emit "//Cannot convert multi-variable each loops to Stylus"
@errors.push("// @each: line #{node.line} in your Sass file")
comment_out(node)
end
end
def visit_while(node)
emit "//Stylus does not support while loops"
@errors.push("// @while: line #{node.line} in your Sass file")
comment_out(node)
end
def visit_atroot(node)
emit "//Stylus does not support @at-root"
@errors.push("// @at-root: line #{node.line} in your Sass file")
comment_out(node)
end
def visit_debug(node)
emit "//Stylus does not support @debug"
@errors.push("// @debug: line #{node.line} in your Sass file")
comment_out(node)
end
def visit_warn(node)
emit "//Stylus does not support @warn"
@errors.push("// @warn: line #{node.line} in your Sass file")
comment_out(node)
end
def visit_root(node)
@indent = -1
@errors = []
@lines = []
visit_children(node)
unless @errors.empty?
@errors.unshift("//Below is a list of the Sass rules that could not be converted to Stylus")
@errors.push("\n")
@lines.unshift(*@errors)
end
@lines.join("\n")
end
end