Permalink
Browse files

add support for omitting styling

  • Loading branch information...
tj committed Jul 26, 2012
1 parent 4efd7df commit da42eb8c0b56d54f33f23506ff95af655a207a75
Showing with 25 additions and 3 deletions.
  1. +5 −0 Readme.md
  2. +5 −0 examples/pet.js
  3. +15 −3 index.js
View
@@ -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)
View
@@ -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();
View
@@ -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);
};

0 comments on commit da42eb8

Please sign in to comment.