Permalink
Browse files

initial local commit

  • Loading branch information...
1 parent f34f969 commit b479e6f35ebc7b6d2e7d9152f9a80b405344e766 @fshost committed Feb 18, 2013
Showing with 2,663 additions and 4 deletions.
  1. +7 −0 LICENSE.txt
  2. +24 −4 README.md
  3. +204 −0 lib/ejs.js
  4. +198 −0 lib/filters.js
  5. +30 −0 lib/utils.js
  6. 0 node_modules/ejs/.gitmodules
  7. +4 −0 node_modules/ejs/.npmignore
  8. +4 −0 node_modules/ejs/.travis.yml
  9. +124 −0 node_modules/ejs/History.md
  10. +23 −0 node_modules/ejs/Makefile
  11. +183 −0 node_modules/ejs/Readme.md
  12. +14 −0 node_modules/ejs/benchmark.js
  13. +581 −0 node_modules/ejs/ejs.js
  14. +1 −0 node_modules/ejs/ejs.min.js
  15. +24 −0 node_modules/ejs/examples/client.html
  16. +9 −0 node_modules/ejs/examples/functions.ejs
  17. +22 −0 node_modules/ejs/examples/functions.js
  18. +7 −0 node_modules/ejs/examples/list.ejs
  19. +14 −0 node_modules/ejs/examples/list.js
  20. +2 −0 node_modules/ejs/index.js
  21. +352 −0 node_modules/ejs/lib/ejs.js
  22. +198 −0 node_modules/ejs/lib/filters.js
  23. +23 −0 node_modules/ejs/lib/utils.js
  24. +30 −0 node_modules/ejs/package.json
  25. +176 −0 node_modules/ejs/support/compile.js
  26. +241 −0 node_modules/ejs/test/ejs.js
  27. +1 −0 node_modules/ejs/test/fixtures/backslash.ejs
  28. +1 −0 node_modules/ejs/test/fixtures/backslash.html
  29. +5 −0 node_modules/ejs/test/fixtures/comments.ejs
  30. +4 −0 node_modules/ejs/test/fixtures/comments.html
  31. +1 −0 node_modules/ejs/test/fixtures/double-quote.ejs
  32. +1 −0 node_modules/ejs/test/fixtures/double-quote.html
  33. +5 −0 node_modules/ejs/test/fixtures/error.ejs
  34. +8 −0 node_modules/ejs/test/fixtures/error.out
  35. +1 −0 node_modules/ejs/test/fixtures/fail.ejs
  36. +1 −0 node_modules/ejs/test/fixtures/include.css.ejs
  37. +3 −0 node_modules/ejs/test/fixtures/include.css.html
  38. +5 −0 node_modules/ejs/test/fixtures/include.ejs
  39. +9 −0 node_modules/ejs/test/fixtures/include.html
  40. +1 −0 node_modules/ejs/test/fixtures/includes/menu-item.ejs
  41. +1 −0 node_modules/ejs/test/fixtures/includes/menu/item.ejs
  42. +11 −0 node_modules/ejs/test/fixtures/menu.ejs
  43. +3 −0 node_modules/ejs/test/fixtures/menu.html
  44. +1 −0 node_modules/ejs/test/fixtures/messed.ejs
  45. +1 −0 node_modules/ejs/test/fixtures/messed.html
  46. +5 −0 node_modules/ejs/test/fixtures/newlines.ejs
  47. +9 −0 node_modules/ejs/test/fixtures/newlines.html
  48. +5 −0 node_modules/ejs/test/fixtures/no.newlines.ejs
  49. +5 −0 node_modules/ejs/test/fixtures/no.newlines.html
  50. +1 −0 node_modules/ejs/test/fixtures/para.ejs
  51. +1 −0 node_modules/ejs/test/fixtures/pet.ejs
  52. +1 −0 node_modules/ejs/test/fixtures/single-quote.ejs
  53. +1 −0 node_modules/ejs/test/fixtures/single-quote.html
  54. +3 −0 node_modules/ejs/test/fixtures/style.css
  55. +1 −0 node_modules/ejs/test/fixtures/user.ejs
  56. +29 −0 package.json
  57. +32 −0 require-ejs.js
  58. +10 −0 test/test.js
  59. +1 −0 test/views/index.ejs
  60. +1 −0 test/views/layout.ejs
View
@@ -0,0 +1,7 @@
+Copyright (c) 2013 Mashdraggin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,4 +1,24 @@
-require-ejs
-===========
-
-Allows requiring EJS template files as modules in Node.js.
+### require-ejs
+
+Allows requiring [EJS](https://github.com/visionmedia/ejs) template files as modules in Node.js. Requiring any file with an .ejs extension returns a compiled EJS template function.
+
+#### installation
+
+npm install require-ejs
+
+#### about
+
+I thought this might be useful if using Node.js as part of a build framework, and after noticing that in the [original EJS](https://github.com/visionmedia/ejs) source there was code for registering an EJS file, but that it did not work on my PC, I tweaked the EJS compiler source and wrote this little extension.
+
+#### todo
+
+Stack traces need to be mapped correctly for include file templates.
+
+#### MIT License
+Copyright (c) 2013 Nathan Cartwright
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,204 @@
+
+var path = require('path'),
+ utils = require(path.join(__dirname, 'utils')),
+ read = utils.read,
+ resolveInclude = utils.resolvePath,
+ filters = exports.filters = require(path.join(__dirname, 'filters'));
+
+exports.utils = utils;
+
+/**
+ * Translate filtered code into function calls.
+ *
+ * @param {String} js
+ * @return {String}
+ * @api private
+ */
+function filtered(js) {
+ return js.substr(1).split('|').reduce(function(js, filter){
+ var parts = filter.split(':')
+ , name = parts.shift()
+ , args = parts.shift() || '';
+ if (args) args = ', ' + args;
+ return 'filters.' + name + '(' + js + args + ')';
+ });
+};
+
+/**
+ * Re-throw the given `err` in context to the
+ * `str` of ejs, `filename`, and `lineno`.
+ *
+ * @param {Error} err
+ * @param {String} str
+ * @param {String} filename
+ * @param {String} lineno
+ * @api private
+ */
+function rethrow(err, str, filename, lineno){
+ var lines = str.split('\n')
+ , start = Math.max(lineno - 3, 0)
+ , end = Math.min(lines.length, lineno + 3);
+
+ // Error context
+ var context = lines.slice(start, end).map(function(line, i){
+ var curr = i + start + 1;
+ return (curr == lineno ? ' >> ' : ' ')
+ + curr
+ + '| '
+ + line;
+ }).join('\n');
+
+ // Alter exception message
+ err.path = filename;
+ err.message = (filename || 'ejs') + ':'
+ + lineno + '\n'
+ + context + '\n\n'
+ + err.message;
+
+ throw err;
+}
+
+/**
+ * Parse the given `str` of ejs, returning the function body.
+ *
+ * @param {String} str
+ * @return {String}
+ * @api public
+ */
+var parse = exports.parse = function(str, options){
+ var options = options || {}
+ , open = options.open || exports.open || '<%'
+ , close = options.close || exports.close || '%>'
+ , filename = options.filename
+ , compileDebug = options.compileDebug !== false
+ , buf = [];
+
+ buf.push('var buf = [];');
+ if (false !== options._with) buf.push('\nwith (locals || {}) { (function(){ ');
+ buf.push('\n buf.push(\'');
+
+ var lineno = 1;
+
+ var consumeEOL = false;
+ for (var i = 0, len = str.length; i < len; ++i) {
+ if (str.slice(i, open.length + i) == open) {
+ i += open.length
+
+ var prefix, postfix, line = (compileDebug ? '__stack.lineno=' : '') + lineno;
+ switch (str.substr(i, 1)) {
+ case '=':
+ prefix = "', escape((" + line + ', ';
+ postfix = ")), '";
+ ++i;
+ break;
+ case '-':
+ prefix = "', (" + line + ', ';
+ postfix = "), '";
+ ++i;
+ break;
+ default:
+ prefix = "');" + line + ';';
+ postfix = "; buf.push('";
+ }
+
+ var end = str.indexOf(close, i)
+ , js = str.substring(i, end)
+ , start = i
+ , include = null
+ , n = 0;
+
+ if ('-' == js[js.length-1]){
+ js = js.substring(0, js.length - 2);
+ consumeEOL = true;
+ }
+
+ if (0 == js.trim().indexOf('include')) {
+ var name = js.trim().slice(7).trim();
+ if (!filename) throw new Error('filename option is required for includes');
+ var path = resolveInclude(name, filename);
+ include = read(path);
+ include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
+ buf.push("' + (function(){" + include + "})() + '");
+ js = '';
+ }
+
+ while (~(n = js.indexOf("\n", n))) n++, lineno++;
+ if (js.substr(0, 1) == ':') js = filtered(js);
+ if (js) {
+ if (js.lastIndexOf('//') > js.lastIndexOf('\n')) js += '\n';
+ buf.push(prefix, js, postfix);
+ }
+ i += end - start + close.length - 1;
+
+ } else if (str.substr(i, 1) == "\\") {
+ buf.push("\\\\");
+ } else if (str.substr(i, 1) == "'") {
+ buf.push("\\'");
+ } else if (str.substr(i, 1) == "\r") {
+ // ignore
+ } else if (str.substr(i, 1) == "\n") {
+ if (consumeEOL) {
+ consumeEOL = false;
+ } else {
+ buf.push("\\n");
+ lineno++;
+ }
+ } else {
+ buf.push(str.substr(i, 1));
+ }
+ }
+
+ if (false !== options._with) buf.push("'); })();\n} \nreturn buf.join('');")
+ else buf.push("');\nreturn buf.join('');");
+ return buf.join('').replace(/^\/n$/g, '');
+};
+
+/**
+ * Compile the given `str` of ejs into a `Function`.
+ *
+ * @param {String} str
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+var compile = exports.compile = function(str, options){
+ options = options || {};
+ var escape = options.escape || utils.escape;
+
+ var input = JSON.stringify(str)
+ , compileDebug = options.compileDebug !== false
+ , filename = options.filename
+ ? JSON.stringify(options.filename)
+ : 'undefined';
+
+ if (compileDebug) {
+ // Adds the fancy stack trace meta info
+ str = [
+ 'var __stack = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };',
+ rethrow.toString(),
+ 'try {',
+ exports.parse(str, options),
+ '} catch (err) {',
+ ' rethrow(err, __stack.input, __stack.filename, __stack.lineno);',
+ '}'
+ ].join("\n");
+ } else {
+ str = exports.parse(str, options);
+ }
+
+ str = 'escape = escape || ' + escape.toString() + ';\n' + str;
+
+ try {
+ var fn = new Function('locals, filters, escape', str);
+ } catch (err) {
+ if ('SyntaxError' == err.name) {
+ err.message += options.filename
+ ? ' in ' + filename
+ : ' while compiling ejs';
+ }
+ throw err;
+ }
+
+ return fn;
+
+};
Oops, something went wrong.

0 comments on commit b479e6f

Please sign in to comment.