Permalink
Browse files

fix imports

  • Loading branch information...
1 parent 1a83621 commit fce73f066513843f9d47b1882edb1229957b07cc @chjj committed Nov 20, 2011
Showing with 57 additions and 43 deletions.
  1. +57 −43 lib/parser.js
View
@@ -39,7 +39,7 @@ var rule = function() {
depth++;
- while (next().type !== 'end') {
+ while (next().type !== 'end') {
switch (token.type) {
case 'rule':
token.selector = nest(selector, token.selector);
@@ -61,10 +61,10 @@ var rule = function() {
.replace(/,\s*/g, ',' + indent());
return selector
- + ' {'
- + body.join('')
- + indent()
- + '}'
+ + ' {'
+ + body.join('')
+ + indent()
+ + '}'
+ below.join('');
};
@@ -79,7 +79,7 @@ var nest = function(parent, child) {
var replace = function(val) {
return val.replace(/\$[\w-]+/g, function(name) {
if (!variables[name]) {
- throw new
+ throw new
Error('Undeclared variable: ' + name
+ '\nLine: ' + token.line);
}
@@ -94,7 +94,7 @@ var property = function() {
// clean whitespace
val = val.replace(
- /(\() +| +(\))|(, ) +/g,
+ /(\() +| +(\))|(, ) +/g,
'$1$2$3'
);
@@ -107,10 +107,10 @@ var trait = function() {
depth++;
- while (next().type !== 'end') {
+ while (next().type !== 'end') {
if (token.type !== 'property') {
throw new
- Error('Unexpected token in trait: '
+ Error('Unexpected token in trait: '
+ JSON.stringify(token));
}
body.push(indent() + property());
@@ -156,17 +156,17 @@ trait.mixin = function(token) {
, trait_ = traits[name];
if (!trait_) {
- throw new
+ throw new
Error('Undeclared mixin: ' + name
+ '\nLine: ' + token.line);
}
if (args) {
args = args
.split(/\s*,\s*(?![^(]*\))/)
- .map(function(arg) {
- return arg[0] === '('
- ? arg.slice(1, -1)
+ .map(function(arg) {
+ return arg[0] === '('
+ ? arg.slice(1, -1)
: arg;
});
return trait_.apply(null, args);
@@ -176,7 +176,7 @@ trait.mixin = function(token) {
};
var nested_at = function() {
- if (token.name === '@trait') {
+ if (token.name === '@trait') {
return trait();
}
@@ -185,26 +185,26 @@ var nested_at = function() {
depth++;
- while (next().type !== 'end') {
+ while (next().type !== 'end') {
body.push(tok());
}
depth--;
body = body.join('');
- return token_.name
- + ' '
- + token_.params
- + ' {'
- + body
- + indent()
+ return token_.name
+ + ' '
+ + token_.params
+ + ' {'
+ + body
+ + indent()
+ '}\n';
};
var at = function() {
- var name = token.name
- , val = token.params
+ var name = token.name
+ , val = token.params
, trait_;
switch (name) {
@@ -216,7 +216,7 @@ var at = function() {
trait_ = trait.mixin(token);
return replace(trait_);
case '@import':
- val = imports(val, options);
+ val = imports(val, options);
break;
}
@@ -229,13 +229,13 @@ var comment = function() {
.replace(/\s+/g, ' ')
.trim();
- return '\n'
- + indent()
- + '/**'
- + indent()
- + ' * '
- + text
- + indent()
+ return '\n'
+ + indent()
+ + '/**'
+ + indent()
+ + ' * '
+ + text
+ + indent()
+ ' */\n';
};
@@ -244,20 +244,20 @@ var tok = function() {
case 'comment':
return comment();
case 'rule':
- return indent()
+ return indent()
+ rule();
case 'property':
- return indent()
+ return indent()
+ replace(property());
case 'nested_at':
- return indent()
+ return indent()
+ nested_at();
case 'at':
- return indent()
+ return indent()
+ at();
default:
throw new
- Error('Unexpected token: '
+ Error('Unexpected token: '
+ JSON.stringify(token));
}
};
@@ -331,13 +331,13 @@ var img = {
var base64 = function(val) {
return val.replace(
- /url\(([^)]+)\)/gi,
+ /url\(([^)]+)\)/gi,
function(str, path) {
var path = path.replace(/['"]/g, '')
, ext = extname(path)
, data;
- if (~path.indexOf('//')
+ if (~path.indexOf('//')
|| !img[ext]) return str;
try {
@@ -347,10 +347,10 @@ var base64 = function(val) {
return str;
}
- return 'url("data:'
- + img[ext]
- + ';base64,'
- + data
+ return 'url("data:'
+ + img[ext]
+ + ';base64,'
+ + data
+ '")';
}
);
@@ -363,7 +363,14 @@ var base64 = function(val) {
var imports = function(path) {
if (path.indexOf('url') === 0) return path;
- var path = path.replace(/['"]/g, '')
+ var options_ = options
+ , token_ = token
+ , tokens_ = tokens
+ , variables_ = variables
+ , traits_ = traits
+ , depth_ = depth;
+
+ var path = path.replace(/['"]/g, '')
, css = fs.readFileSync(path, 'utf8');
path = join(options.dir, path);
@@ -373,6 +380,13 @@ var imports = function(path) {
dir: dirname(path)
});
+ options = options_;
+ token = token_;
+ tokens = tokens_;
+ variables = variables_;
+ traits = traits_;
+ depth = depth_;
+
return css;
};

0 comments on commit fce73f0

Please sign in to comment.