PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/gulp-clean/node_modules/gulp-util/node_modules/lodash.template/index.js

https://gitlab.com/sheep-fish-vert/geon
JavaScript | 216 lines | 79 code | 19 blank | 118 comment | 12 complexity | a4d595696483dca5484fc5c71163ed59 MD5 | raw file
  1. /**
  2. * Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/>
  3. * Build: `lodash modularize modern exports="npm" -o ./npm/`
  4. * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <http://lodash.com/license>
  8. */
  9. var defaults = require('lodash.defaults'),
  10. escape = require('lodash.escape'),
  11. escapeStringChar = require('lodash._escapestringchar'),
  12. keys = require('lodash.keys'),
  13. reInterpolate = require('lodash._reinterpolate'),
  14. templateSettings = require('lodash.templatesettings'),
  15. values = require('lodash.values');
  16. /** Used to match empty string literals in compiled template source */
  17. var reEmptyStringLeading = /\b__p \+= '';/g,
  18. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  19. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  20. /**
  21. * Used to match ES6 template delimiters
  22. * http://people.mozilla.org/~jorendorff/es6-draft.html#sec-literals-string-literals
  23. */
  24. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  25. /** Used to ensure capturing order of template delimiters */
  26. var reNoMatch = /($^)/;
  27. /** Used to match unescaped characters in compiled string literals */
  28. var reUnescapedString = /['\n\r\t\u2028\u2029\\]/g;
  29. /**
  30. * A micro-templating method that handles arbitrary delimiters, preserves
  31. * whitespace, and correctly escapes quotes within interpolated code.
  32. *
  33. * Note: In the development build, `_.template` utilizes sourceURLs for easier
  34. * debugging. See http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl
  35. *
  36. * For more information on precompiling templates see:
  37. * http://lodash.com/custom-builds
  38. *
  39. * For more information on Chrome extension sandboxes see:
  40. * http://developer.chrome.com/stable/extensions/sandboxingEval.html
  41. *
  42. * @static
  43. * @memberOf _
  44. * @category Utilities
  45. * @param {string} text The template text.
  46. * @param {Object} data The data object used to populate the text.
  47. * @param {Object} [options] The options object.
  48. * @param {RegExp} [options.escape] The "escape" delimiter.
  49. * @param {RegExp} [options.evaluate] The "evaluate" delimiter.
  50. * @param {Object} [options.imports] An object to import into the template as local variables.
  51. * @param {RegExp} [options.interpolate] The "interpolate" delimiter.
  52. * @param {string} [sourceURL] The sourceURL of the template's compiled source.
  53. * @param {string} [variable] The data object variable name.
  54. * @returns {Function|string} Returns a compiled function when no `data` object
  55. * is given, else it returns the interpolated text.
  56. * @example
  57. *
  58. * // using the "interpolate" delimiter to create a compiled template
  59. * var compiled = _.template('hello <%= name %>');
  60. * compiled({ 'name': 'fred' });
  61. * // => 'hello fred'
  62. *
  63. * // using the "escape" delimiter to escape HTML in data property values
  64. * _.template('<b><%- value %></b>', { 'value': '<script>' });
  65. * // => '<b>&lt;script&gt;</b>'
  66. *
  67. * // using the "evaluate" delimiter to generate HTML
  68. * var list = '<% _.forEach(people, function(name) { %><li><%- name %></li><% }); %>';
  69. * _.template(list, { 'people': ['fred', 'barney'] });
  70. * // => '<li>fred</li><li>barney</li>'
  71. *
  72. * // using the ES6 delimiter as an alternative to the default "interpolate" delimiter
  73. * _.template('hello ${ name }', { 'name': 'pebbles' });
  74. * // => 'hello pebbles'
  75. *
  76. * // using the internal `print` function in "evaluate" delimiters
  77. * _.template('<% print("hello " + name); %>!', { 'name': 'barney' });
  78. * // => 'hello barney!'
  79. *
  80. * // using a custom template delimiters
  81. * _.templateSettings = {
  82. * 'interpolate': /{{([\s\S]+?)}}/g
  83. * };
  84. *
  85. * _.template('hello {{ name }}!', { 'name': 'mustache' });
  86. * // => 'hello mustache!'
  87. *
  88. * // using the `imports` option to import jQuery
  89. * var list = '<% jq.each(people, function(name) { %><li><%- name %></li><% }); %>';
  90. * _.template(list, { 'people': ['fred', 'barney'] }, { 'imports': { 'jq': jQuery } });
  91. * // => '<li>fred</li><li>barney</li>'
  92. *
  93. * // using the `sourceURL` option to specify a custom sourceURL for the template
  94. * var compiled = _.template('hello <%= name %>', null, { 'sourceURL': '/basic/greeting.jst' });
  95. * compiled(data);
  96. * // => find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector
  97. *
  98. * // using the `variable` option to ensure a with-statement isn't used in the compiled template
  99. * var compiled = _.template('hi <%= data.name %>!', null, { 'variable': 'data' });
  100. * compiled.source;
  101. * // => function(data) {
  102. * var __t, __p = '', __e = _.escape;
  103. * __p += 'hi ' + ((__t = ( data.name )) == null ? '' : __t) + '!';
  104. * return __p;
  105. * }
  106. *
  107. * // using the `source` property to inline compiled templates for meaningful
  108. * // line numbers in error messages and a stack trace
  109. * fs.writeFileSync(path.join(cwd, 'jst.js'), '\
  110. * var JST = {\
  111. * "main": ' + _.template(mainText).source + '\
  112. * };\
  113. * ');
  114. */
  115. function template(text, data, options) {
  116. // based on John Resig's `tmpl` implementation
  117. // http://ejohn.org/blog/javascript-micro-templating/
  118. // and Laura Doktorova's doT.js
  119. // https://github.com/olado/doT
  120. var settings = templateSettings.imports._.templateSettings || templateSettings;
  121. text = String(text || '');
  122. // avoid missing dependencies when `iteratorTemplate` is not defined
  123. options = defaults({}, options, settings);
  124. var imports = defaults({}, options.imports, settings.imports),
  125. importsKeys = keys(imports),
  126. importsValues = values(imports);
  127. var isEvaluating,
  128. index = 0,
  129. interpolate = options.interpolate || reNoMatch,
  130. source = "__p += '";
  131. // compile the regexp to match each delimiter
  132. var reDelimiters = RegExp(
  133. (options.escape || reNoMatch).source + '|' +
  134. interpolate.source + '|' +
  135. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  136. (options.evaluate || reNoMatch).source + '|$'
  137. , 'g');
  138. text.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  139. interpolateValue || (interpolateValue = esTemplateValue);
  140. // escape characters that cannot be included in string literals
  141. source += text.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  142. // replace delimiters with snippets
  143. if (escapeValue) {
  144. source += "' +\n__e(" + escapeValue + ") +\n'";
  145. }
  146. if (evaluateValue) {
  147. isEvaluating = true;
  148. source += "';\n" + evaluateValue + ";\n__p += '";
  149. }
  150. if (interpolateValue) {
  151. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  152. }
  153. index = offset + match.length;
  154. // the JS engine embedded in Adobe products requires returning the `match`
  155. // string in order to produce the correct `offset` value
  156. return match;
  157. });
  158. source += "';\n";
  159. // if `variable` is not specified, wrap a with-statement around the generated
  160. // code to add the data object to the top of the scope chain
  161. var variable = options.variable,
  162. hasVariable = variable;
  163. if (!hasVariable) {
  164. variable = 'obj';
  165. source = 'with (' + variable + ') {\n' + source + '\n}\n';
  166. }
  167. // cleanup code by stripping empty strings
  168. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  169. .replace(reEmptyStringMiddle, '$1')
  170. .replace(reEmptyStringTrailing, '$1;');
  171. // frame code as the function body
  172. source = 'function(' + variable + ') {\n' +
  173. (hasVariable ? '' : variable + ' || (' + variable + ' = {});\n') +
  174. "var __t, __p = '', __e = _.escape" +
  175. (isEvaluating
  176. ? ', __j = Array.prototype.join;\n' +
  177. "function print() { __p += __j.call(arguments, '') }\n"
  178. : ';\n'
  179. ) +
  180. source +
  181. 'return __p\n}';
  182. try {
  183. var result = Function(importsKeys, 'return ' + source ).apply(undefined, importsValues);
  184. } catch(e) {
  185. e.source = source;
  186. throw e;
  187. }
  188. if (data) {
  189. return result(data);
  190. }
  191. // provide the compiled function's source by its `toString` method, in
  192. // supported environments, or the `source` property as a convenience for
  193. // inlining compiled templates during the build process
  194. result.source = source;
  195. return result;
  196. }
  197. module.exports = template;