Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add support for omitting styling
  • Loading branch information
tj committed Jul 26, 2012
1 parent 4efd7df commit da42eb8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Expand Up @@ -82,6 +82,11 @@ output:
instead of styling "labels.visits", "labels.uptime" separately,
you may use `{label labels.visits}` and `{label labels.uptime}`.

## Omitting styling

To output a plain string but use the same format, simply omit the css
string from the `.compile(fmt, style)` call.

## License

(The MIT License)
Expand Down
5 changes: 5 additions & 0 deletions examples/pet.js
Expand Up @@ -21,4 +21,9 @@ var fn = css.compile(' {name} is a {species}, he is {age} years old', style);
console.log();
console.log(fn(tobi));
console.log(fn(loki));

var fn = css.compile(' {name} is a {species}, he is {age} years old');
console.log(fn(tobi));
console.log(fn(loki));

console.log();
18 changes: 15 additions & 3 deletions index.js
Expand Up @@ -71,15 +71,27 @@ exports.compile = function(str, style){
// parse css
if (style) style = css.parse(style);

// parse format
str = 'return "' + str.replace(/\{([^}]+)\}/g, function(_, name){
// styled
function styled(_, name){
// support {classname prop}
name = name.split(' ');
var classname = name.length > 1 ? name.shift() : name[0];
name = name[0];
var seq = sequence(classname, style);
return '\\033[' + seq + 'm" + obj.' + name + ' + "\\033[0m';
}) + '"';
}

// plain
function plain(_, name){
// support {classname prop}
name = name.split(' ');
var classname = name.length > 1 ? name.shift() : name[0];
name = name[0];
return '" + obj.' + name + ' + "';
}

// parse format
str = 'return "' + str.replace(/\{([^}]+)\}/g, style ? styled : plain) + '"';

return new Function('obj', str);
};
Expand Down

0 comments on commit da42eb8

Please sign in to comment.