Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

79 lines (68 sloc) 2.648 kB
# requires
coffee = require 'coffee-script'
fs = require 'fs'
_ = require 'underscore'
util = require 'util'
vm = require 'vm'
# public functions
exports.compile = (fileName, newFileName, callback = (err, fileName, newFileName) ->) ->
if typeof fileName is 'object'
# assume compile object (cssObject, mixins)
return compileObject fileName, newFileName
if typeof newFileName is 'function'
callback = newFileName
newFileName = undefined
if newFileName is undefined
newFileName = fileName.replace /\.css\.coffee$/i, '.css'
fs.readFile fileName, (err, data) ->
if err
callback err, fileName, newFileName
else
try
js = coffee.compile(data.toString().trim())
sandbox =
exports:
css: {}
mixins: getDefaultMixins()
require: require
vm.runInNewContext js, sandbox
css = compileObject sandbox.exports.css, sandbox.exports.mixins
fs.writeFile newFileName, css, (err) ->
callback err, fileName, newFileName
catch err
callback err, fileName, newFileName
exports.getDefaultMixins = getDefaultMixins
# privates
compileObject = (cssObject, mixins) ->
mixins or= getDefaultMixins()
outputList = []
compileCssList = (cssObject) ->
for selector, declarations of cssObject
declarationText = []
nestedSelectors = {}
isNested = false
iterateDeclarations = (d) ->
for key, value of d
if typeof value is 'object'
nestedSelectors["#{selector} #{key}"] = value
isNested = true
else
if mixins[key]
iterateDeclarations mixins[key](value)
else
cssProperty = key.replace /[A-Z]/g, (s) -> '-' + s.toLowerCase()
declarationText.push " #{cssProperty}: #{value};"
iterateDeclarations declarations
outputList.push "#{selector} {\n#{declarationText.join('\n')}\n}"
if isNested
compileCssList nestedSelectors
compileCssList cssObject
outputList.join '\n'
getDefaultMixins = ->
boxShadowAll: (value) ->
WebkitBoxShadow: value
MozBoxShadow: value
boxShadow: value
opacityAll: (value) ->
filter: 'alpha(opacity=' + (value * 100) + ')'
opacity: value
Jump to Line
Something went wrong with that request. Please try again.