forked from andreypopp/less2stylus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
executable file
·378 lines (298 loc) · 9.81 KB
/
index.coffee
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env coffee
fs = require 'fs'
crypto = require 'crypto'
{Parser, tree} = require 'less'
{extend, isString} = require 'underscore'
defineVisitor = (base, props) ->
extend Object.create(base), props
genVar = ->
"var#{crypto.randomBytes(12).toString('hex')}"
renderValue = (node, options) ->
options = extend {}, options
return node if isString node
impl = Object.create(expressionVisitor)
impl.options = options
visitor = new tree.visitor impl
visitor.visit node
if impl.value then impl.value.trim() else ''
renderTree = (node, indent = '') ->
impl = Object.create(treeVisitor)
impl.indent = indent
new tree.visitor(impl).visit(node)
renderMixinParam = (node) ->
param = node.name.slice(1)
if node.value
param = "#{param}=#{renderValue(node.value)}"
param
renderMixinArg = (node) ->
param = renderValue(node.value)
if node.name
param = "#{node.name.slice(1)}:#{param}"
param
renderPrelude = ->
console.log """
lesscss-percentage(n)
(n * 100)%
""".trim()
toUnquoted = (value) ->
value
.replace(/@{/g, '"@{')
.replace(/}/g, '}"')
.split(/(@{)|}/)
.filter((v) -> v != '@{' and v != '}' and v?.length > 0)
.join(' + ')
funcMap =
'%': 's'
'percentage': 'lesscss-percentage'
mixinMap =
translate: 'mixin-translate'
scale: 'mixin-scale'
rotate: 'mixin-rotate'
skew: 'mixin-skew'
translate3d: 'mixin-translate3d'
baseVisitor =
visitAlpha: (node) ->
throw new Error('not implemented')
visitAnonymous: (node) ->
throw new Error('not implemented')
visitAssigment: (node) ->
throw new Error('not implemented')
visitAttribute: (node) ->
throw new Error('not implemented')
visitCall: (node) ->
throw new Error('not implemented')
visitColor: (node) ->
throw new Error('not implemented')
visitComment: (node) ->
throw new Error('not implemented')
visitCondition: (node) ->
throw new Error('not implemented')
visitDimension: (node) ->
throw new Error('not implemented')
visitDirective: (node) ->
throw new Error('not implemented')
visitElement: (node) ->
throw new Error('not implemented')
visitExpression: (node) ->
throw new Error('not implemented')
visitExtend: (node) ->
throw new Error('not implemented')
visitImport: (node) ->
throw new Error('not implemented')
visitJavaScript: (node) ->
throw new Error('not implemented')
visitKeyword: (node) ->
throw new Error('not implemented')
visitMedia: (node) ->
throw new Error('not implemented')
visitMixin: (node) ->
throw new Error('not implemented')
visitMixinCall: (node) ->
throw new Error('not implemented')
visitMixinDefinition: (node) ->
throw new Error('not implemented')
visitNegative: (node) ->
throw new Error('not implemented')
visitOperation: (node) ->
throw new Error('not implemented')
visitParen: (node) ->
throw new Error('not implemented')
visitQuoted: (node) ->
throw new Error('not implemented')
visitRule: (node, options) ->
throw new Error('not implemented')
visitRuleset: (node, options) ->
throw new Error('not implemented')
visitSelector: (node, options) ->
throw new Error('not implemented')
visitValue: (node) ->
throw new Error('not implemented')
visitVariable: (node) ->
throw new Error('not implemented')
visitURL: (node) ->
throw new Error('not implemented')
visitUnicodeDescriptor: (node) ->
throw new Error('not implemented')
expressionVisitor = defineVisitor baseVisitor,
acc: (v) ->
@value = '' unless @value
@value += ' ' + v
visitAnonymous: (node) ->
@acc node.value
visitDimension: (node) ->
@acc "#{node.value}#{node.unit.numerator.join('')}"
visitVariable: (node) ->
if @options.unquote
@acc "@{#{node.name.slice(1)}}"
else
@acc node.name.slice(1)
visitCall: (node, options) ->
options.visitDeeper = false
args = node.args.map((e) => renderValue(e, @options)).join(', ')
name = funcMap[node.name] or node.name
args = args.replace(/%d/g, '%s') if name == 's'
@acc "#{name}(#{args})"
visitSelector: (node, options) ->
options.visitDeeper = false
@acc node.elements
.map((e) =>
"#{e.combinator.value}#{renderValue(e, @options)}")
.join('')
.replace(/>/g, ' > ')
.replace(/\+/g, ' + ')
visitElement: (node, options) ->
options.visitDeeper = false
@acc renderValue(node.value, @options)
visitAttribute: (node, options) ->
options.visitDeeper = false
rendered = node.key
rendered += node.op + renderValue(node.value, @options) if node.op
@acc "[#{rendered}]"
visitKeyword: (node) ->
@acc node.value
visitQuoted: (node) ->
if node.escaped
value = toUnquoted(node.value)
@acc "unquote(#{node.quote}#{value}#{node.quote})"
else
@acc "#{node.quote}#{node.value}#{node.quote}"
visitParen: (node, options) ->
options.visitDeeper = false
@acc "(#{renderValue(node.value, @options)})"
visitRule: (node, options) ->
options.visitDeeper = false
@acc "#{node.name}: #{renderValue(node.value, @options)}"
visitOperation: (node, options) ->
options.visitDeeper = false
if node.operands.length != 2
throw new Error('assertion')
[left, right] = node.operands
value = "#{renderValue(left, @options)} #{node.op} #{renderValue(right, @options)}"
value = "(#{value})"
@acc value
visitValue: (node, options) ->
options.visitDeeper = false
@acc node.value.map((e) => renderValue(e, @options)).join(', ')
visitExpression: (node, options) ->
options.visitDeeper = false
@acc node.value.map((e) => renderValue(e, @options)).join(' ')
visitColor: (node) ->
if node.rgb
c = "rgb(#{node.rgb.join(', ')}"
if node.alpha
c += ", #{node.alpha}"
c += ")"
@acc c
else
throw new Error("unknow color #{node}")
visitNegative: (node, options) ->
options.visitDeeper = false
@acc "(- #{renderValue(node.value, @options)})"
treeVisitor = defineVisitor baseVisitor,
indent: ''
increaseIndent: ->
@indent + ' '
decreaseIndent: ->
@indent.slice(0, -2)
p: (m, indent) ->
indent = indent or @indent
console.log "#{indent}#{m.trim()}"
isNamespaceDefinition: (node) ->
return false unless node.type == 'Ruleset'
return false unless node.selectors.length == 1
name = renderValue node.selectors[0]
return false unless name[0] == '#'
return false unless node.rules.every (rule) ->
# TODO: variables are also allowed
rule.type == 'MixinDefinition' or rule.type == 'Comment'
return name.slice(1)
isNamespaceCall: (node) ->
visitRuleset: (node, options, directive = '') ->
unless node.root
namespace = @isNamespaceDefinition(node)
options.visitDeeper = false
if namespace
for rule in node.rules
# TODO: handle variables
if rule.type == 'MixinDefinition'
rule.name = ".#{namespace}-#{rule.name.slice(1)}"
renderTree(rule, @indent)
else
if node.rules.length > 0
@p "#{directive}#{node.selectors.map(renderValue).join(', ')}"
for rule in node.rules
renderTree(rule, @increaseIndent())
visitRulesetOut: (node) ->
unless node.root
# Generate a newline after every ruleset to make the code more readable
console.log ""
@decreaseIndent()
visitRule: (node, options) ->
options.visitDeeper = false
name = node.name
if name[0] == '@'
@p "#{name.slice(1)} = #{renderValue(node.value)}"
else
@p "#{name} #{renderValue(node.value)}#{node.important}"
visitComment: (node) ->
# Always return comments to increase readability
@p node.value
visitMedia: (node, options) ->
options.visitDeeper = false
features = renderValue(node.features, unquote: true)
if /@{/.exec features
mediaVar = genVar()
@p "#{mediaVar} = \"#{toUnquoted(features)}\""
@p "@media #{mediaVar}"
else
@p "@media #{features}"
for rule in node.ruleset.rules
renderTree(rule, @increaseIndent())
visitSelector: (node, options) ->
options.visitDeeper = false
@p node.elements.map(renderValue).join('')
visitMixinDefinition: (node, options) ->
options.visitDeeper = false
name = node.name.slice(1)
name = mixinMap[name] or name
@p "#{name}(#{node.params.map(renderMixinParam).join(', ')})"
for rule in node.rules
renderTree(rule, @increaseIndent())
console.log ""
visitMixinCall: (node, options) ->
options.visitDeeper = false
if node.selector.elements.length == 2 and node.selector.elements[0].value[0] == '#'
namespace = node.selector.elements[0].value.slice(1)
node.selector.elements[0] = node.selector.elements[1]
delete node.selector.elements[1]
node.selector.elements[0].value = "#{namespace}-#{node.selector.elements[0].value.slice(1)}"
name = renderValue(node.selector).slice(1)
name = mixinMap[name] or name
if node.arguments.length > 0
v = "#{renderValue(node.selector).slice(1)}"
v += "(#{node.arguments.map(renderMixinArg).join(', ')})"
else
v = "#{renderValue(node.selector).slice(1)}()"
@p v
@p ""
visitExtend: (node, options) ->
options.visitDeeper = false
@p "@extend #{node.selector.elements[0].value}"
visitImport: (node, options) ->
options.visitDeeper = false
@p "@import #{renderValue(node.path).replace(/\.less/, '.styl')}"
visitDirective: (node, options) ->
@visitRuleset(node.ruleset, options, node.name)
main = ->
filename = process.argv[2]
parser = new Parser(filename: filename)
str = fs.readFileSync(filename).toString()
parser.parse str, (err, node) ->
throw err if err
renderPrelude()
renderTree node
module.exports = {
main,
treeVisitor, expressionVisitor, baseVisitor,
renderValue, renderTree, renderPrelude,
}