index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 'use strict';
  2. var pug_has_own_property = Object.prototype.hasOwnProperty;
  3. /**
  4. * Merge two attribute objects giving precedence
  5. * to values in object `b`. Classes are special-cased
  6. * allowing for arrays and merging/joining appropriately
  7. * resulting in a string.
  8. *
  9. * @param {Object} a
  10. * @param {Object} b
  11. * @return {Object} a
  12. * @api private
  13. */
  14. exports.merge = pug_merge;
  15. function pug_merge(a, b) {
  16. if (arguments.length === 1) {
  17. var attrs = a[0];
  18. for (var i = 1; i < a.length; i++) {
  19. attrs = pug_merge(attrs, a[i]);
  20. }
  21. return attrs;
  22. }
  23. for (var key in b) {
  24. if (key === 'class') {
  25. var valA = a[key] || [];
  26. a[key] = (Array.isArray(valA) ? valA : [valA]).concat(b[key] || []);
  27. } else if (key === 'style') {
  28. var valA = pug_style(a[key]);
  29. valA = valA && valA[valA.length - 1] !== ';' ? valA + ';' : valA;
  30. var valB = pug_style(b[key]);
  31. valB = valB && valB[valB.length - 1] !== ';' ? valB + ';' : valB;
  32. a[key] = valA + valB;
  33. } else {
  34. a[key] = b[key];
  35. }
  36. }
  37. return a;
  38. };
  39. /**
  40. * Process array, object, or string as a string of classes delimited by a space.
  41. *
  42. * If `val` is an array, all members of it and its subarrays are counted as
  43. * classes. If `escaping` is an array, then whether or not the item in `val` is
  44. * escaped depends on the corresponding item in `escaping`. If `escaping` is
  45. * not an array, no escaping is done.
  46. *
  47. * If `val` is an object, all the keys whose value is truthy are counted as
  48. * classes. No escaping is done.
  49. *
  50. * If `val` is a string, it is counted as a class. No escaping is done.
  51. *
  52. * @param {(Array.<string>|Object.<string, boolean>|string)} val
  53. * @param {?Array.<string>} escaping
  54. * @return {String}
  55. */
  56. exports.classes = pug_classes;
  57. function pug_classes_array(val, escaping) {
  58. var classString = '', className, padding = '', escapeEnabled = Array.isArray(escaping);
  59. for (var i = 0; i < val.length; i++) {
  60. className = pug_classes(val[i]);
  61. if (!className) continue;
  62. escapeEnabled && escaping[i] && (className = pug_escape(className));
  63. classString = classString + padding + className;
  64. padding = ' ';
  65. }
  66. return classString;
  67. }
  68. function pug_classes_object(val) {
  69. var classString = '', padding = '';
  70. for (var key in val) {
  71. if (key && val[key] && pug_has_own_property.call(val, key)) {
  72. classString = classString + padding + key;
  73. padding = ' ';
  74. }
  75. }
  76. return classString;
  77. }
  78. function pug_classes(val, escaping) {
  79. if (Array.isArray(val)) {
  80. return pug_classes_array(val, escaping);
  81. } else if (val && typeof val === 'object') {
  82. return pug_classes_object(val);
  83. } else {
  84. return val || '';
  85. }
  86. }
  87. /**
  88. * Convert object or string to a string of CSS styles delimited by a semicolon.
  89. *
  90. * @param {(Object.<string, string>|string)} val
  91. * @return {String}
  92. */
  93. exports.style = pug_style;
  94. function pug_style(val) {
  95. if (!val) return '';
  96. if (typeof val === 'object') {
  97. var out = '';
  98. for (var style in val) {
  99. /* istanbul ignore else */
  100. if (pug_has_own_property.call(val, style)) {
  101. out = out + style + ':' + val[style] + ';';
  102. }
  103. }
  104. return out;
  105. } else {
  106. return val + '';
  107. }
  108. };
  109. /**
  110. * Render the given attribute.
  111. *
  112. * @param {String} key
  113. * @param {String} val
  114. * @param {Boolean} escaped
  115. * @param {Boolean} terse
  116. * @return {String}
  117. */
  118. exports.attr = pug_attr;
  119. function pug_attr(key, val, escaped, terse) {
  120. if (val === false || val == null || !val && (key === 'class' || key === 'style')) {
  121. return '';
  122. }
  123. if (val === true) {
  124. return ' ' + (terse ? key : key + '="' + key + '"');
  125. }
  126. var type = typeof val;
  127. if ((type === 'object' || type === 'function') && typeof val.toJSON === 'function') {
  128. val = val.toJSON();
  129. }
  130. if (typeof val !== 'string') {
  131. val = JSON.stringify(val);
  132. if (!escaped && val.indexOf('"') !== -1) {
  133. return ' ' + key + '=\'' + val.replace(/'/g, '&#39;') + '\'';
  134. }
  135. }
  136. if (escaped) val = pug_escape(val);
  137. return ' ' + key + '="' + val + '"';
  138. };
  139. /**
  140. * Render the given attributes object.
  141. *
  142. * @param {Object} obj
  143. * @param {Object} terse whether to use HTML5 terse boolean attributes
  144. * @return {String}
  145. */
  146. exports.attrs = pug_attrs;
  147. function pug_attrs(obj, terse){
  148. var attrs = '';
  149. for (var key in obj) {
  150. if (pug_has_own_property.call(obj, key)) {
  151. var val = obj[key];
  152. if ('class' === key) {
  153. val = pug_classes(val);
  154. attrs = pug_attr(key, val, false, terse) + attrs;
  155. continue;
  156. }
  157. if ('style' === key) {
  158. val = pug_style(val);
  159. }
  160. attrs += pug_attr(key, val, false, terse);
  161. }
  162. }
  163. return attrs;
  164. };
  165. /**
  166. * Escape the given string of `html`.
  167. *
  168. * @param {String} html
  169. * @return {String}
  170. * @api private
  171. */
  172. var pug_match_html = /["&<>]/;
  173. exports.escape = pug_escape;
  174. function pug_escape(_html){
  175. var html = '' + _html;
  176. var regexResult = pug_match_html.exec(html);
  177. if (!regexResult) return _html;
  178. var result = '';
  179. var i, lastIndex, escape;
  180. for (i = regexResult.index, lastIndex = 0; i < html.length; i++) {
  181. switch (html.charCodeAt(i)) {
  182. case 34: escape = '&quot;'; break;
  183. case 38: escape = '&amp;'; break;
  184. case 60: escape = '&lt;'; break;
  185. case 62: escape = '&gt;'; break;
  186. default: continue;
  187. }
  188. if (lastIndex !== i) result += html.substring(lastIndex, i);
  189. lastIndex = i + 1;
  190. result += escape;
  191. }
  192. if (lastIndex !== i) return result + html.substring(lastIndex, i);
  193. else return result;
  194. };
  195. /**
  196. * Re-throw the given `err` in context to the
  197. * the pug in `filename` at the given `lineno`.
  198. *
  199. * @param {Error} err
  200. * @param {String} filename
  201. * @param {String} lineno
  202. * @param {String} str original source
  203. * @api private
  204. */
  205. exports.rethrow = pug_rethrow;
  206. function pug_rethrow(err, filename, lineno, str){
  207. if (!(err instanceof Error)) throw err;
  208. if ((typeof window != 'undefined' || !filename) && !str) {
  209. err.message += ' on line ' + lineno;
  210. throw err;
  211. }
  212. try {
  213. str = str || require('fs').readFileSync(filename, 'utf8')
  214. } catch (ex) {
  215. pug_rethrow(err, null, lineno)
  216. }
  217. var context = 3
  218. , lines = str.split('\n')
  219. , start = Math.max(lineno - context, 0)
  220. , end = Math.min(lines.length, lineno + context);
  221. // Error context
  222. var context = lines.slice(start, end).map(function(line, i){
  223. var curr = i + start + 1;
  224. return (curr == lineno ? ' > ' : ' ')
  225. + curr
  226. + '| '
  227. + line;
  228. }).join('\n');
  229. // Alter exception message
  230. err.path = filename;
  231. err.message = (filename || 'Pug') + ':' + lineno
  232. + '\n' + context + '\n\n' + err.message;
  233. throw err;
  234. };