Skip to content

Instantly share code, notes, and snippets.

@icodeforlove
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save icodeforlove/0cb9f94e68fe4a64e1f4 to your computer and use it in GitHub Desktop.

Select an option

Save icodeforlove/0cb9f94e68fe4a64e1f4 to your computer and use it in GitHub Desktop.
playing around with the idea of vars and inline js
// this would add variable support and inline JS support
var RCSPreprocess = (function () {
var varRegExp = /\$([a-z0-9-_]+)/ig,
singleVarRegExp = /\$([a-z0-9-_]+)/i,
codeRegExp = /\$\{([\s\S]+)\}\$/igm,
varDefineRegExp = /\$([a-z0-9-_]+): ([^;]+);/ig,
rcsVars = {};
function process (example) {
example = example.replace(varDefineRegExp, function (string, name, value) {
var varMatch = value.match(singleVarRegExp);
if (varMatch) {
value = rcsVars[varMatch[1]];
}
rcsVars[name] = value;
return '';
});
example = example.replace(codeRegExp, function (string, code) {
try {
code = eval('(function () {' + code + '})();');
} catch (error) {
error.code = code;
throw error;
}
return code;
});
example = example.replace(varRegExp, function (string, name) {
return rcsVars[name] || '';
});
return example.trim();
}
return {
process: process
}
})();
console.log(RCSPreprocess.process('$red: #ff0000; $defultColor: $red; body {background: $defultColor;}'));
console.log(RCSPreprocess.process('body {background: $defultColor;}\n${' +
'var string = "";'+
'for (var i = 0; i<10; i++) {' +
'string+= ".box" + i + " {width: " + i + "px; color: $defultColor;}\\n"' +
'}' +
'return string;' +
'}$'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment