Last active
August 29, 2015 14:04
-
-
Save icodeforlove/0cb9f94e68fe4a64e1f4 to your computer and use it in GitHub Desktop.
playing around with the idea of vars and inline js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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