From c4272bb0e35e40905b0dacd26e7aa6bc8923be87 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 18 Jun 2014 03:32:46 +0300 Subject: [PATCH 1/6] Reactify. --- src/command.coffee | 4 ++-- src/compiler.coffee | 17 +++++++++++++++++ src/preprocessor.coffee | 10 ++++++---- src/scanner.coffee | 7 +++++-- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/command.coffee b/src/command.coffee index 7c44283..e9e9ed4 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -22,8 +22,8 @@ printUsage = -> process.exit 1 printVersion = -> - package = JSON.parse fs.readFileSync __dirname + "/../package.json", "utf8" - console.error "Eco version #{package.version}" + pkg = JSON.parse fs.readFileSync __dirname + "/../package.json", "utf8" + console.error "Eco version #{pkg.version}" process.exit 0 preprocessArgs = (args) -> diff --git a/src/compiler.coffee b/src/compiler.coffee index 9a5fc0b..49f7e12 100644 --- a/src/compiler.coffee +++ b/src/compiler.coffee @@ -8,6 +8,7 @@ exports.precompile = precompile = (source) -> """ function(__obj) { if (!__obj) __obj = {}; + if (window.__reactTagCount == null) window.__reactTagCount = 0; var __out = [], __capture = function(callback) { var out = __out, result; __out = []; @@ -23,6 +24,22 @@ exports.precompile = precompile = (source) -> } else { return ''; } + }, __getReactTagValue = function(tag) { + var curr = window.__reactTagCount += 1; + var sel = 'react-uniq-' + curr; + setTimeout(function() { + console.log('Append', sel, tag); + jQuery('#' + sel).replaceWith(tag); + }, 0); + return ''; + }, __reactify = function(value, isSafe) { + if (!(value instanceof rx.SrcCell)) { + return isSafe ? value : __sanitize(value); + } + return __getReactTagValue(rx.rxt.tags.span(rx.bind(function() { + var val = value.get(); + return isSafe ? rx.rxt.rawHtml(val) : __sanitize(val); + }))); }, __safe, __objSafe = __obj.safe, __escape = __obj.escape; __safe = __obj.safe = function(value) { if (value && value.ecoSafe) { diff --git a/src/preprocessor.coffee b/src/preprocessor.coffee index f698dd1..24d3d34 100644 --- a/src/preprocessor.coffee +++ b/src/preprocessor.coffee @@ -33,10 +33,12 @@ module.exports = class Preprocessor recordCode: (code) -> if code isnt "end" if @options.print - if @options.safe - @record "__out.push #{code}" - else - @record "__out.push __sanitize #{code}" + code = if @options.reactive + isSafe = if @options.safe then 'true' else 'false' + "__reactify #{code}, #{isSafe}" + else if not @options.safe + "__sanitize #{code}" + @record "__out.push #{code}" else @record code diff --git a/src/scanner.coffee b/src/scanner.coffee index 2b6e94a..d023f36 100644 --- a/src/scanner.coffee +++ b/src/scanner.coffee @@ -3,7 +3,7 @@ module.exports = class Scanner @modePatterns: - data: /(.*?)(<%%|<%\s*(\#)|<%(([=-])?)|\n|$)/ + data: /(.*?)(<%%|<%\s*(\#)|<%((~?[=-])?)|\n|$)/ code: /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/ comment: /(.*?)(%>|\n|$)/ @@ -70,7 +70,10 @@ module.exports = class Scanner @mode = "comment" else @mode = "code" - callback ["beginCode", print: @directive?, safe: @directive is "-"] + pr = @directive? + safe = pr and @directive.indexOf('-') isnt -1 + reactive = pr and @directive.indexOf('~') is 0 + callback ["beginCode", {print: pr, safe: safe, reactive: reactive}] scanCode: (callback) -> if @tail is "\n" From bf1a8f17f85dd48a394ad6b9fb8a320562fbd778 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 18 Jun 2014 03:34:54 +0300 Subject: [PATCH 2/6] rename --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e8730af..673cf38 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ -{ "name": "eco" +{ "name": "reactive-eco" , "description": "Embedded CoffeeScript templates" -, "author": "Sam Stephenson" -, "version": "1.1.0-rc-3" +, "author": "Paul miller, Sam Stephenson" +, "version": "0.1.0" , "licenses": [ { "type": "MIT" , "url": "https://github.com/sstephenson/eco/raw/master/LICENSE" @@ -14,7 +14,7 @@ } , "dependencies": { "strscan": ">=1.0.1" - , "coffee-script": ">=1.2.0" + , "coffee-script": "~1.7.1" } , "devDependencies": { "nodeunit": ">=0.6.4" From 5dca923bf781cd137660404100cf9f05d942c4b3 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 18 Jun 2014 03:39:08 +0300 Subject: [PATCH 3/6] new ver --- lib/command.js | 426 ++++++++++++++++++++++++-------------------- lib/compiler.js | 32 ++-- lib/index.js | 54 +++--- lib/preprocessor.js | 160 ++++++++--------- lib/scanner.js | 250 +++++++++++++------------- lib/util.js | 92 +++++----- 6 files changed, 524 insertions(+), 490 deletions(-) diff --git a/lib/command.js b/lib/command.js index 6fa10e8..7a2b1b2 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,223 +1,255 @@ -(function() { - var compile, compileToFile, each, eachFile, eco, exec, fs, indent, mkdir, parseOptions, path, preprocessArgs, printUsage, printVersion, read, stripExtension, sys; - var __slice = Array.prototype.slice; +// Generated by CoffeeScript 1.7.1 +var compile, compileToFile, each, eachFile, eco, exec, fs, indent, mkdir, parseOptions, path, preprocessArgs, printUsage, printVersion, read, stripExtension, sys, + __slice = [].slice; - fs = require("fs"); +fs = require("fs"); - path = require("path"); +path = require("path"); - sys = require("util"); +sys = require("util"); - eco = require(".."); +eco = require(".."); - exec = require("child_process").exec; +exec = require("child_process").exec; - indent = require("./util").indent; +indent = require("./util").indent; - printUsage = function() { - console.error("\nUsage: eco [options] path/to/template.eco\n\n -i, --identifier [NAME] set the name of the global template object\n -o, --output [DIR] set the directory for compiled JavaScript\n -p, --print print the compiled JavaScript to stdout\n -s, --stdio listen for and compile templates over stdio\n -v, --version display Eco version\n -h, --help display this help message\n"); - return process.exit(1); - }; +printUsage = function() { + console.error("\nUsage: eco [options] path/to/template.eco\n\n -i, --identifier [NAME] set the name of the global template object\n -o, --output [DIR] set the directory for compiled JavaScript\n -p, --print print the compiled JavaScript to stdout\n -s, --stdio listen for and compile templates over stdio\n -v, --version display Eco version\n -h, --help display this help message\n"); + return process.exit(1); +}; - printVersion = function() { - var package; - package = JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")); - console.error("Eco version " + package.version); - return process.exit(0); - }; +printVersion = function() { + var pkg; + pkg = JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")); + console.error("Eco version " + pkg.version); + return process.exit(0); +}; - preprocessArgs = function(args) { - var arg, char, match, result, _i, _j, _len, _len2, _ref; - result = []; - for (_i = 0, _len = args.length; _i < _len; _i++) { - arg = args[_i]; - if (match = /^-([a-z]{2,})/.exec(arg)) { - _ref = match[1].split(''); - for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) { - char = _ref[_j]; - result.push("-" + char); - } - } else { - result.push(arg); +preprocessArgs = function(args) { + var arg, char, match, result, _i, _j, _len, _len1, _ref; + result = []; + for (_i = 0, _len = args.length; _i < _len; _i++) { + arg = args[_i]; + if (match = /^-([a-z]{2,})/.exec(arg)) { + _ref = match[1].split(''); + for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { + char = _ref[_j]; + result.push("-" + char); } + } else { + result.push(arg); } - return result; + } + return result; +}; + +parseOptions = function(args) { + var arg, option, options, _i, _len, _ref; + options = { + files: [], + identifier: "ecoTemplates" }; - - parseOptions = function(args) { - var arg, option, options, _i, _len, _ref; - options = { - files: [], - identifier: "ecoTemplates" - }; - option = null; - _ref = preprocessArgs(args); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - arg = _ref[_i]; - if (option) { - options[option] = arg; - option = null; - } else { - switch (arg) { - case "-i": - case "--identifier": - option = "identifier"; - break; - case "-o": - case "--output": - option = "output"; - break; - case "-p": - case "--print": - options.print = true; - break; - case "-s": - case "--stdio": - options.stdio = true; - break; - case "-v": - case "--version": - printVersion(); - break; - default: - if (/^-/.test(arg)) { - printUsage(); - } else { - options.files.push(arg); - } - } + option = null; + _ref = preprocessArgs(args); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + arg = _ref[_i]; + if (option) { + options[option] = arg; + option = null; + } else { + switch (arg) { + case "-i": + case "--identifier": + option = "identifier"; + break; + case "-o": + case "--output": + option = "output"; + break; + case "-p": + case "--print": + options.print = true; + break; + case "-s": + case "--stdio": + options.stdio = true; + break; + case "-v": + case "--version": + printVersion(); + break; + default: + if (/^-/.test(arg)) { + printUsage(); + } else { + options.files.push(arg); + } } } - if (option) printUsage(); - return options; - }; - - read = function(stream, callback) { - var buffer; - buffer = ""; - stream.setEncoding("utf8"); - stream.on("data", function(data) { - return buffer += data; - }); - return stream.on("end", function() { - return typeof callback === "function" ? callback(buffer) : void 0; + } + if (option) { + printUsage(); + } + return options; +}; + +read = function(stream, callback) { + var buffer; + buffer = ""; + stream.setEncoding("utf8"); + stream.on("data", function(data) { + return buffer += data; + }); + return stream.on("end", function() { + return typeof callback === "function" ? callback(buffer) : void 0; + }); +}; + +each = function(_arg, callback) { + var proceed, values; + values = 1 <= _arg.length ? __slice.call(_arg, 0) : []; + return (proceed = function() { + return callback(values.shift(), proceed); + })(); +}; + +eachFile = function(files, callback) { + var traverse; + traverse = function(root, dir, done) { + return fs.readdir(dir, function(err, entries) { + if (err) { + return callback(err); + } + return each(entries, function(entry, proceed) { + var file; + if (entry == null) { + return done(); + } + file = path.join(dir, entry); + return fs.stat(file, function(err, stat) { + if (err) { + return callback(err); + } + if (stat.isFile() && /\.eco$/.test(file)) { + return callback(null, file, root, proceed); + } else if (stat.isDirectory()) { + return traverse(root, file, proceed); + } else { + return proceed(); + } + }); + }); }); }; + return each(files, function(file, proceed) { + if (file == null) { + return; + } + return fs.stat(file, function(err, stat) { + var root; + if (err) { + return callback(err); + } + if (stat.isDirectory()) { + return traverse(file, file, proceed); + } else { + root = path.dirname(file); + return callback(null, file, root, proceed); + } + }); + }); +}; - each = function(_arg, callback) { - var proceed, values; - values = 1 <= _arg.length ? __slice.call(_arg, 0) : []; - return (proceed = function() { - return callback(values.shift(), proceed); - })(); - }; +stripExtension = function(name) { + return name.replace(/(\.eco)?$/, ""); +}; - eachFile = function(files, callback) { - var traverse; - traverse = function(root, dir, done) { - return fs.readdir(dir, function(err, entries) { - if (err) return callback(err); - return each(entries, function(entry, proceed) { - var file; - if (entry == null) return done(); - file = path.join(dir, entry); - return fs.stat(file, function(err, stat) { - if (err) return callback(err); - if (stat.isFile() && /\.eco$/.test(file)) { - return callback(null, file, root, proceed); - } else if (stat.isDirectory()) { - return traverse(root, file, proceed); - } else { - return proceed(); - } - }); - }); - }); - }; - return each(files, function(file, proceed) { - if (file == null) return; - return fs.stat(file, function(err, stat) { - var root; - if (err) return callback(err); - if (stat.isDirectory()) { - return traverse(file, file, proceed); - } else { - root = path.dirname(file); - return callback(null, file, root, proceed); +compile = function(infile, identifier, name, callback) { + return fs.readFile(infile, "utf8", function(err, source) { + var template; + if (err) { + return callback(err); + } + template = indent(eco.precompile(source), 2); + return callback(null, "(function() {\n this." + identifier + " || (this." + identifier + " = {});\n this." + identifier + "[" + (JSON.stringify(name)) + "] = " + (template.slice(2)) + ";\n}).call(this);"); + }); +}; + +mkdir = function(dir, callback) { + return exec("mkdir -p " + (JSON.stringify(dir)), function(err, stdout, stderr) { + return callback(err); + }); +}; + +compileToFile = function(infile, identifier, root, outdir, callback) { + var name, outfile; + root = path.join(root, "/"); + if (root === infile.slice(0, root.length)) { + name = stripExtension(infile.slice(root.length)); + } else { + name = stripExtension(infile); + } + outfile = path.join(outdir != null ? outdir : root, name + ".js"); + return mkdir(path.dirname(outfile), function(err) { + if (err) { + return callback(err); + } + return compile(infile, identifier, name, function(err, result) { + return fs.writeFile(outfile, result + "\n", "utf8", function(err) { + if (err) { + return callback(err); } + return callback(null, outfile, name); }); }); - }; - - stripExtension = function(name) { - return name.replace(/(\.eco)?$/, ""); - }; - - compile = function(infile, identifier, name, callback) { - return fs.readFile(infile, "utf8", function(err, source) { - var template; - if (err) return callback(err); - template = indent(eco.precompile(source), 2); - return callback(null, "(function() {\n this." + identifier + " || (this." + identifier + " = {});\n this." + identifier + "[" + (JSON.stringify(name)) + "] = " + (template.slice(2)) + ";\n}).call(this);"); + }); +}; + +exports.run = function(args) { + var infile, name, options; + if (args == null) { + args = process.argv.slice(2); + } + options = parseOptions(args); + if (options.stdio) { + if (options.files.length || options.output) { + printUsage(); + } + process.openStdin(); + return read(process.stdin, function(source) { + return sys.puts(eco.precompile(source)); }); - }; - - mkdir = function(dir, callback) { - return exec("mkdir -p " + (JSON.stringify(dir)), function(err, stdout, stderr) { - return callback(err); + } else if (options.print) { + if (options.files.length !== 1 || options.output) { + printUsage(); + } + infile = options.files[0]; + name = stripExtension(path.basename(infile)); + return compile(infile, options.identifier, name, function(err, result) { + if (err) { + throw err; + } + return sys.puts(result); }); - }; - - compileToFile = function(infile, identifier, root, outdir, callback) { - var name, outfile; - root = path.join(root, "/"); - if (root === infile.slice(0, root.length)) { - name = stripExtension(infile.slice(root.length)); - } else { - name = stripExtension(infile); + } else { + if (!options.files.length) { + printUsage(); } - outfile = path.join(outdir != null ? outdir : root, name + ".js"); - return mkdir(path.dirname(outfile), function(err) { - if (err) return callback(err); - return compile(infile, identifier, name, function(err, result) { - return fs.writeFile(outfile, result + "\n", "utf8", function(err) { - if (err) return callback(err); - return callback(null, outfile, name); - }); + return eachFile(options.files, function(err, infile, root, proceed) { + if (err) { + throw err; + } + if (infile == null) { + return; + } + return compileToFile(infile, options.identifier, root, options.output, function(err, outfile, name) { + if (err) { + throw err; + } + console.error("" + (JSON.stringify(name)) + ": " + infile + " -> " + outfile); + return proceed(); }); }); - }; - - exports.run = function(args) { - var infile, name, options; - if (args == null) args = process.argv.slice(2); - options = parseOptions(args); - if (options.stdio) { - if (options.files.length || options.output) printUsage(); - process.openStdin(); - return read(process.stdin, function(source) { - return sys.puts(eco.precompile(source)); - }); - } else if (options.print) { - if (options.files.length !== 1 || options.output) printUsage(); - infile = options.files[0]; - name = stripExtension(path.basename(infile)); - return compile(infile, options.identifier, name, function(err, result) { - if (err) throw err; - return sys.puts(result); - }); - } else { - if (!options.files.length) printUsage(); - return eachFile(options.files, function(err, infile, root, proceed) { - if (err) throw err; - if (infile == null) return; - return compileToFile(infile, options.identifier, root, options.output, function(err, outfile, name) { - if (err) throw err; - console.error("" + (JSON.stringify(name)) + ": " + infile + " -> " + outfile); - return proceed(); - }); - }); - } - }; - -}).call(this); + } +}; diff --git a/lib/compiler.js b/lib/compiler.js index 0eff0e5..6f54a17 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -1,22 +1,20 @@ -(function() { - var CoffeeScript, indent, precompile, preprocess; +// Generated by CoffeeScript 1.7.1 +var CoffeeScript, indent, precompile, preprocess; - CoffeeScript = require("coffee-script"); +CoffeeScript = require("coffee-script"); - preprocess = require("./preprocessor").preprocess; +preprocess = require("./preprocessor").preprocess; - indent = require("./util").indent; +indent = require("./util").indent; - exports.precompile = precompile = function(source) { - var script; - script = CoffeeScript.compile(preprocess(source), { - noWrap: true - }); - return "function(__obj) {\n if (!__obj) __obj = {};\n var __out = [], __capture = function(callback) {\n var out = __out, result;\n __out = [];\n callback.call(this);\n result = __out.join('');\n __out = out;\n return __safe(result);\n }, __sanitize = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else if (typeof value !== 'undefined' && value != null) {\n return __escape(value);\n } else {\n return '';\n }\n }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;\n __safe = __obj.safe = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else {\n if (!(typeof value !== 'undefined' && value != null)) value = '';\n var result = new String(value);\n result.ecoSafe = true;\n return result;\n }\n };\n if (!__escape) {\n __escape = __obj.escape = function(value) {\n return ('' + value)\n .replace(/&/g, '&')\n .replace(//g, '>')\n .replace(/\x22/g, '"');\n };\n }\n (function() {\n" + (indent(script, 4)) + "\n }).call(__obj);\n __obj.safe = __objSafe, __obj.escape = __escape;\n return __out.join('');\n}"; - }; +exports.precompile = precompile = function(source) { + var script; + script = CoffeeScript.compile(preprocess(source), { + noWrap: true + }); + return "function(__obj) {\n if (!__obj) __obj = {};\n if (window.__reactTagCount == null) window.__reactTagCount = 0;\n var __out = [], __capture = function(callback) {\n var out = __out, result;\n __out = [];\n callback.call(this);\n result = __out.join('');\n __out = out;\n return __safe(result);\n }, __sanitize = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else if (typeof value !== 'undefined' && value != null) {\n return __escape(value);\n } else {\n return '';\n }\n }, __getReactTagValue = function(tag) {\n var curr = window.__reactTagCount += 1;\n var sel = 'react-uniq-' + curr;\n setTimeout(function() {\n console.log('Append', sel, tag);\n jQuery('#' + sel).replaceWith(tag);\n }, 0);\n return '';\n }, __reactify = function(value, isSafe) {\n if (!(value instanceof rx.SrcCell)) {\n return isSafe ? value : __sanitize(value);\n }\n return __getReactTagValue(rx.rxt.tags.span(rx.bind(function() {\n var val = value.get();\n return isSafe ? rx.rxt.rawHtml(val) : __sanitize(val);\n })));\n }, __safe, __objSafe = __obj.safe, __escape = __obj.escape;\n __safe = __obj.safe = function(value) {\n if (value && value.ecoSafe) {\n return value;\n } else {\n if (!(typeof value !== 'undefined' && value != null)) value = '';\n var result = new String(value);\n result.ecoSafe = true;\n return result;\n }\n };\n if (!__escape) {\n __escape = __obj.escape = function(value) {\n return ('' + value)\n .replace(/&/g, '&')\n .replace(//g, '>')\n .replace(/\x22/g, '"');\n };\n }\n (function() {\n" + (indent(script, 4)) + "\n }).call(__obj);\n __obj.safe = __objSafe, __obj.escape = __escape;\n return __out.join('');\n}"; +}; - exports.compile = function(source) { - return new Function("return " + (precompile(source)))(); - }; - -}).call(this); +exports.compile = function(source) { + return new Function("return " + (precompile(source)))(); +}; diff --git a/lib/index.js b/lib/index.js index bca5ac8..080ca77 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,37 +1,35 @@ -(function() { - var compile, eco, precompile, preprocess, _ref; +// Generated by CoffeeScript 1.7.1 +var compile, eco, precompile, preprocess, _ref; - _ref = require("./compiler"), compile = _ref.compile, precompile = _ref.precompile; +_ref = require("./compiler"), compile = _ref.compile, precompile = _ref.precompile; - preprocess = require("./preprocessor").preprocess; +preprocess = require("./preprocessor").preprocess; - module.exports = eco = function(source) { - var _base, _ref2; - if (eco.cache) { - return (_ref2 = (_base = eco.cache)[source]) != null ? _ref2 : _base[source] = compile(source); - } else { - return compile(source); - } - }; - - eco.cache = {}; +module.exports = eco = function(source) { + var _base; + if (eco.cache) { + return (_base = eco.cache)[source] != null ? _base[source] : _base[source] = compile(source); + } else { + return compile(source); + } +}; - eco.preprocess = preprocess; +eco.cache = {}; - eco.precompile = precompile; +eco.preprocess = preprocess; - eco.compile = compile; +eco.precompile = precompile; - eco.render = function(source, data) { - return (eco(source))(data); - }; +eco.compile = compile; - if (require.extensions) { - require.extensions[".eco"] = function(module, filename) { - var source; - source = require("fs").readFileSync(filename, "utf-8"); - return module._compile("module.exports = " + (precompile(source)), filename); - }; - } +eco.render = function(source, data) { + return (eco(source))(data); +}; -}).call(this); +if (require.extensions) { + require.extensions[".eco"] = function(module, filename) { + var source; + source = require("fs").readFileSync(filename, "utf-8"); + return module._compile("module.exports = " + (precompile(source)), filename); + }; +} diff --git a/lib/preprocessor.js b/lib/preprocessor.js index 5a33c64..d9e8e91 100644 --- a/lib/preprocessor.js +++ b/lib/preprocessor.js @@ -1,89 +1,87 @@ -(function() { - var Preprocessor, Scanner, util; - - Scanner = require("./scanner"); - - util = require("./util"); - - module.exports = Preprocessor = (function() { - - Preprocessor.preprocess = function(source) { - var preprocessor; - preprocessor = new Preprocessor(source); - return preprocessor.preprocess(); - }; - - function Preprocessor(source) { - this.scanner = new Scanner(source); - this.output = ""; - this.level = 0; - this.options = {}; - this.captures = []; - } - - Preprocessor.prototype.preprocess = function() { - var _this = this; - while (!this.scanner.done) { - this.scanner.scan(function(token) { +// Generated by CoffeeScript 1.7.1 +var Preprocessor, Scanner, util; + +Scanner = require("./scanner"); + +util = require("./util"); + +module.exports = Preprocessor = (function() { + Preprocessor.preprocess = function(source) { + var preprocessor; + preprocessor = new Preprocessor(source); + return preprocessor.preprocess(); + }; + + function Preprocessor(source) { + this.scanner = new Scanner(source); + this.output = ""; + this.level = 0; + this.options = {}; + this.captures = []; + } + + Preprocessor.prototype.preprocess = function() { + while (!this.scanner.done) { + this.scanner.scan((function(_this) { + return function(token) { return _this[token[0]].apply(_this, token.slice(1)); - }); - } - return this.output; - }; - - Preprocessor.prototype.record = function(line) { - this.output += util.repeat(" ", this.level); - return this.output += line + "\n"; - }; - - Preprocessor.prototype.printString = function(string) { - if (string.length) { - return this.record("__out.push " + (util.inspectString(string))); - } - }; - - Preprocessor.prototype.beginCode = function(options) { - return this.options = options; - }; - - Preprocessor.prototype.recordCode = function(code) { - if (code !== "end") { - if (this.options.print) { - if (this.options.safe) { - return this.record("__out.push " + code); - } else { - return this.record("__out.push __sanitize " + code); - } - } else { - return this.record(code); - } - } - }; + }; + })(this)); + } + return this.output; + }; - Preprocessor.prototype.indent = function(capture) { - this.level++; - if (capture) { - this.record("__capture " + capture); - this.captures.unshift(this.level); - return this.indent(); - } - }; + Preprocessor.prototype.record = function(line) { + this.output += util.repeat(" ", this.level); + return this.output += line + "\n"; + }; - Preprocessor.prototype.dedent = function() { - this.level--; - if (this.level < 0) this.fail("unexpected dedent"); - if (this.captures[0] === this.level) { - this.captures.shift(); - return this.dedent(); + Preprocessor.prototype.printString = function(string) { + if (string.length) { + return this.record("__out.push " + (util.inspectString(string))); + } + }; + + Preprocessor.prototype.beginCode = function(options) { + return this.options = options; + }; + + Preprocessor.prototype.recordCode = function(code) { + var isSafe; + if (code !== "end") { + if (this.options.print) { + code = this.options.reactive ? (isSafe = this.options.safe ? 'true' : 'false', "__reactify " + code + ", " + isSafe) : !this.options.safe ? "__sanitize " + code : void 0; + return this.record("__out.push " + code); + } else { + return this.record(code); } - }; + } + }; + + Preprocessor.prototype.indent = function(capture) { + this.level++; + if (capture) { + this.record("__capture " + capture); + this.captures.unshift(this.level); + return this.indent(); + } + }; - Preprocessor.prototype.fail = function(message) { - throw "Parse error on line " + this.scanner.lineNo + ": " + message; - }; + Preprocessor.prototype.dedent = function() { + this.level--; + if (this.level < 0) { + this.fail("unexpected dedent"); + } + if (this.captures[0] === this.level) { + this.captures.shift(); + return this.dedent(); + } + }; - return Preprocessor; + Preprocessor.prototype.fail = function(message) { + throw "Parse error on line " + this.scanner.lineNo + ": " + message; + }; - })(); + return Preprocessor; -}).call(this); +})(); diff --git a/lib/scanner.js b/lib/scanner.js index cd7258d..0fcdad4 100644 --- a/lib/scanner.js +++ b/lib/scanner.js @@ -1,134 +1,142 @@ -(function() { - var Scanner, StringScanner, trim; - - StringScanner = require("strscan").StringScanner; - - trim = require("./util").trim; - - module.exports = Scanner = (function() { - - Scanner.modePatterns = { - data: /(.*?)(<%%|<%\s*(\#)|<%(([=-])?)|\n|$)/, - code: /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/, - comment: /(.*?)(%>|\n|$)/ - }; - - Scanner.dedentablePattern = /^(end|when|else|catch|finally)(?:\W|$)/; - - Scanner.scan = function(source) { - var scanner, tokens; - tokens = []; - scanner = new Scanner(source); - while (!scanner.done) { - scanner.scan(function(token) { - return tokens.push(token); - }); +// Generated by CoffeeScript 1.7.1 +var Scanner, StringScanner, trim; + +StringScanner = require("strscan").StringScanner; + +trim = require("./util").trim; + +module.exports = Scanner = (function() { + Scanner.modePatterns = { + data: /(.*?)(<%%|<%\s*(\#)|<%((~?[=-])?)|\n|$)/, + code: /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/, + comment: /(.*?)(%>|\n|$)/ + }; + + Scanner.dedentablePattern = /^(end|when|else|catch|finally)(?:\W|$)/; + + Scanner.scan = function(source) { + var scanner, tokens; + tokens = []; + scanner = new Scanner(source); + while (!scanner.done) { + scanner.scan(function(token) { + return tokens.push(token); + }); + } + return tokens; + }; + + function Scanner(source) { + this.source = source.replace(/\r\n?/g, "\n"); + this.scanner = new StringScanner(this.source); + this.mode = "data"; + this.buffer = ""; + this.lineNo = 1; + this.done = false; + } + + Scanner.prototype.scan = function(callback) { + if (this.done) { + return callback(); + } else if (this.scanner.hasTerminated()) { + this.done = true; + switch (this.mode) { + case "data": + return callback(["printString", this.flush()]); + case "code": + return callback(["fail", "unexpected end of template"]); + } + } else { + this.advance(); + switch (this.mode) { + case "data": + return this.scanData(callback); + case "code": + return this.scanCode(callback); + case "comment": + return this.scanComment(callback); } - return tokens; - }; - - function Scanner(source) { - this.source = source.replace(/\r\n?/g, "\n"); - this.scanner = new StringScanner(this.source); - this.mode = "data"; - this.buffer = ""; - this.lineNo = 1; - this.done = false; } - - Scanner.prototype.scan = function(callback) { - if (this.done) { - return callback(); - } else if (this.scanner.hasTerminated()) { - this.done = true; - switch (this.mode) { - case "data": - return callback(["printString", this.flush()]); - case "code": - return callback(["fail", "unexpected end of template"]); - } + }; + + Scanner.prototype.advance = function() { + this.scanner.scanUntil(Scanner.modePatterns[this.mode]); + this.buffer += this.scanner.getCapture(0); + this.tail = this.scanner.getCapture(1); + this.comment = this.scanner.getCapture(2); + this.directive = this.scanner.getCapture(4); + return this.arrow = this.scanner.getCapture(5); + }; + + Scanner.prototype.scanData = function(callback) { + var pr, reactive, safe; + if (this.tail === "<%%") { + this.buffer += "<%"; + return this.scan(callback); + } else if (this.tail === "\n") { + this.buffer += this.tail; + this.lineNo++; + return this.scan(callback); + } else if (this.tail) { + callback(["printString", this.flush()]); + if (this.comment) { + return this.mode = "comment"; } else { - this.advance(); - switch (this.mode) { - case "data": - return this.scanData(callback); - case "code": - return this.scanCode(callback); - case "comment": - return this.scanComment(callback); - } + this.mode = "code"; + pr = this.directive != null; + safe = pr && this.directive.indexOf('-') !== -1; + reactive = pr && this.directive.indexOf('~') === 0; + return callback([ + "beginCode", { + print: pr, + safe: safe, + reactive: reactive + } + ]); } - }; - - Scanner.prototype.advance = function() { - this.scanner.scanUntil(Scanner.modePatterns[this.mode]); - this.buffer += this.scanner.getCapture(0); - this.tail = this.scanner.getCapture(1); - this.comment = this.scanner.getCapture(2); - this.directive = this.scanner.getCapture(4); - return this.arrow = this.scanner.getCapture(5); - }; + } + }; - Scanner.prototype.scanData = function(callback) { - if (this.tail === "<%%") { - this.buffer += "<%"; - return this.scan(callback); - } else if (this.tail === "\n") { - this.buffer += this.tail; - this.lineNo++; - return this.scan(callback); - } else if (this.tail) { - callback(["printString", this.flush()]); - if (this.comment) { - return this.mode = "comment"; - } else { - this.mode = "code"; - return callback([ - "beginCode", { - print: this.directive != null, - safe: this.directive === "-" - } - ]); - } + Scanner.prototype.scanCode = function(callback) { + var code; + if (this.tail === "\n") { + return callback(["fail", "unexpected newline in code block"]); + } else if (this.tail) { + this.mode = "data"; + code = trim(this.flush()); + if (this.arrow) { + code += " " + this.arrow; } - }; - - Scanner.prototype.scanCode = function(callback) { - var code; - if (this.tail === "\n") { - return callback(["fail", "unexpected newline in code block"]); - } else if (this.tail) { - this.mode = "data"; - code = trim(this.flush()); - if (this.arrow) code += " " + this.arrow; - if (this.isDedentable(code)) callback(["dedent"]); - callback(["recordCode", code]); - if (this.directive) return callback(["indent", this.arrow]); + if (this.isDedentable(code)) { + callback(["dedent"]); } - }; - - Scanner.prototype.scanComment = function(callback) { - if (this.tail === "\n") { - return callback(["fail", "unexpected newline in code block"]); - } else if (this.tail) { - this.mode = "data"; - return this.buffer = ""; + callback(["recordCode", code]); + if (this.directive) { + return callback(["indent", this.arrow]); } - }; + } + }; - Scanner.prototype.flush = function() { - var buffer; - buffer = this.buffer; - this.buffer = ""; - return buffer; - }; + Scanner.prototype.scanComment = function(callback) { + if (this.tail === "\n") { + return callback(["fail", "unexpected newline in code block"]); + } else if (this.tail) { + this.mode = "data"; + return this.buffer = ""; + } + }; - Scanner.prototype.isDedentable = function(code) { - return code.match(Scanner.dedentablePattern); - }; + Scanner.prototype.flush = function() { + var buffer; + buffer = this.buffer; + this.buffer = ""; + return buffer; + }; - return Scanner; + Scanner.prototype.isDedentable = function(code) { + return code.match(Scanner.dedentablePattern); + }; - })(); + return Scanner; -}).call(this); +})(); diff --git a/lib/util.js b/lib/util.js index 80fed3e..8cc40f1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,52 +1,52 @@ -(function() { - var repeat, specialCharacters; +// Generated by CoffeeScript 1.7.1 +var repeat, specialCharacters; - exports.repeat = repeat = function(string, count) { - return Array(count + 1).join(string); - }; +exports.repeat = repeat = function(string, count) { + return Array(count + 1).join(string); +}; - exports.indent = function(string, width) { - var line, lines, space; - space = repeat(" ", width); - lines = (function() { - var _i, _len, _ref, _results; - _ref = string.split("\n"); - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - line = _ref[_i]; - _results.push(space + line); - } - return _results; - })(); - return lines.join("\n"); - }; +exports.indent = function(string, width) { + var line, lines, space; + space = repeat(" ", width); + lines = (function() { + var _i, _len, _ref, _results; + _ref = string.split("\n"); + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + line = _ref[_i]; + _results.push(space + line); + } + return _results; + })(); + return lines.join("\n"); +}; - exports.trim = function(string) { - return string.replace(/^\s+/, "").replace(/\s+$/, ""); - }; +exports.trim = function(string) { + return string.replace(/^\s+/, "").replace(/\s+$/, ""); +}; - specialCharacters = { - '\\': '\\\\', - '\b': '\\b', - '\f': '\\f', - '\n': '\\n', - '\r': '\\r', - '\t': '\\t' - }; +specialCharacters = { + '\\': '\\\\', + '\b': '\\b', + '\f': '\\f', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t' +}; - exports.inspectString = function(string) { - var contents; - contents = string.replace(/[\x00-\x1f\\]/g, function(character) { - var code; - if (character in specialCharacters) { - return specialCharacters[character]; - } else { - code = character.charCodeAt(0).toString(16); - if (code.length === 1) code = "0" + code; - return "\\u00" + code; +exports.inspectString = function(string) { + var contents; + contents = string.replace(/[\x00-\x1f\\]/g, function(character) { + var code; + if (character in specialCharacters) { + return specialCharacters[character]; + } else { + code = character.charCodeAt(0).toString(16); + if (code.length === 1) { + code = "0" + code; } - }); - return "'" + contents.replace(/'/g, '\\\'') + "'"; - }; - -}).call(this); + return "\\u00" + code; + } + }); + return "'" + contents.replace(/'/g, '\\\'') + "'"; +}; From f7d32e7c0a6b2e501bd0000cd5bd766802916686 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 18 Jun 2014 03:39:22 +0300 Subject: [PATCH 4/6] Release 0.1.1. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 673cf38..81efa83 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "reactive-eco" , "description": "Embedded CoffeeScript templates" , "author": "Paul miller, Sam Stephenson" -, "version": "0.1.0" +, "version": "0.1.1" , "licenses": [ { "type": "MIT" , "url": "https://github.com/sstephenson/eco/raw/master/LICENSE" From 0154c536aa8d2c546be0e540d8831fddcb704eab Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 18 Jun 2014 03:43:57 +0300 Subject: [PATCH 5/6] fuck --- lib/scanner.js | 2 +- src/scanner.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/scanner.js b/lib/scanner.js index 0fcdad4..ed1b6a0 100644 --- a/lib/scanner.js +++ b/lib/scanner.js @@ -7,7 +7,7 @@ trim = require("./util").trim; module.exports = Scanner = (function() { Scanner.modePatterns = { - data: /(.*?)(<%%|<%\s*(\#)|<%((~?[=-])?)|\n|$)/, + data: /(.*?)(<%%|<%\s*(\#)|<%((~?[=-]?)?)|\n|$)/, code: /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/, comment: /(.*?)(%>|\n|$)/ }; diff --git a/src/scanner.coffee b/src/scanner.coffee index d023f36..4b11081 100644 --- a/src/scanner.coffee +++ b/src/scanner.coffee @@ -3,7 +3,7 @@ module.exports = class Scanner @modePatterns: - data: /(.*?)(<%%|<%\s*(\#)|<%((~?[=-])?)|\n|$)/ + data: /(.*?)(<%%|<%\s*(\#)|<%((~?[=-]?)?)|\n|$)/ code: /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/ comment: /(.*?)(%>|\n|$)/ From aa04b0989d8be4d962d713113f3469b5d4523798 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 18 Jun 2014 03:44:08 +0300 Subject: [PATCH 6/6] Release 0.1.2. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 81efa83..6f59618 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "reactive-eco" , "description": "Embedded CoffeeScript templates" , "author": "Paul miller, Sam Stephenson" -, "version": "0.1.1" +, "version": "0.1.2" , "licenses": [ { "type": "MIT" , "url": "https://github.com/sstephenson/eco/raw/master/LICENSE"