Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

template.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var underscore = require('./underscore.js');
  2. var defaults = require('./defaults.js');
  3. require('./templateSettings.js');
  4. // When customizing `_.templateSettings`, if you don't want to define an
  5. // interpolation, evaluation or escaping regex, we need one that is
  6. // guaranteed not to match.
  7. var noMatch = /(.)^/;
  8. // Certain characters need to be escaped so that they can be put into a
  9. // string literal.
  10. var escapes = {
  11. "'": "'",
  12. '\\': '\\',
  13. '\r': 'r',
  14. '\n': 'n',
  15. '\u2028': 'u2028',
  16. '\u2029': 'u2029'
  17. };
  18. var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
  19. function escapeChar(match) {
  20. return '\\' + escapes[match];
  21. }
  22. // JavaScript micro-templating, similar to John Resig's implementation.
  23. // Underscore templating handles arbitrary delimiters, preserves whitespace,
  24. // and correctly escapes quotes within interpolated code.
  25. // NB: `oldSettings` only exists for backwards compatibility.
  26. function template(text, settings, oldSettings) {
  27. if (!settings && oldSettings) settings = oldSettings;
  28. settings = defaults({}, settings, underscore.templateSettings);
  29. // Combine delimiters into one regular expression via alternation.
  30. var matcher = RegExp([
  31. (settings.escape || noMatch).source,
  32. (settings.interpolate || noMatch).source,
  33. (settings.evaluate || noMatch).source
  34. ].join('|') + '|$', 'g');
  35. // Compile the template source, escaping string literals appropriately.
  36. var index = 0;
  37. var source = "__p+='";
  38. text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
  39. source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
  40. index = offset + match.length;
  41. if (escape) {
  42. source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
  43. } else if (interpolate) {
  44. source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
  45. } else if (evaluate) {
  46. source += "';\n" + evaluate + "\n__p+='";
  47. }
  48. // Adobe VMs need the match returned to produce the correct offset.
  49. return match;
  50. });
  51. source += "';\n";
  52. // If a variable is not specified, place data values in local scope.
  53. if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
  54. source = "var __t,__p='',__j=Array.prototype.join," +
  55. "print=function(){__p+=__j.call(arguments,'');};\n" +
  56. source + 'return __p;\n';
  57. var render;
  58. try {
  59. render = new Function(settings.variable || 'obj', '_', source);
  60. } catch (e) {
  61. e.source = source;
  62. throw e;
  63. }
  64. var template = function(data) {
  65. return render.call(this, data, underscore);
  66. };
  67. // Provide the compiled source as a convenience for precompilation.
  68. var argument = settings.variable || 'obj';
  69. template.source = 'function(' + argument + '){\n' + source + '}';
  70. return template;
  71. }
  72. module.exports = template;