pug.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (function() {
  2. var async, fs, path, pug,
  3. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  4. hasProp = {}.hasOwnProperty;
  5. async = require('async');
  6. fs = require('fs');
  7. pug = require('pug');
  8. path = require('path');
  9. module.exports = function(env, callback) {
  10. var PugTemplate;
  11. PugTemplate = (function(superClass) {
  12. extend(PugTemplate, superClass);
  13. function PugTemplate(fn) {
  14. this.fn = fn;
  15. }
  16. PugTemplate.prototype.render = function(locals, callback) {
  17. var error;
  18. try {
  19. return callback(null, Buffer.from(this.fn(locals)));
  20. } catch (error1) {
  21. error = error1;
  22. return callback(error);
  23. }
  24. };
  25. return PugTemplate;
  26. })(env.TemplatePlugin);
  27. PugTemplate.fromFile = function(filepath, callback) {
  28. return async.waterfall([
  29. function(callback) {
  30. return fs.readFile(filepath.full, callback);
  31. }, (function(_this) {
  32. return function(buffer, callback) {
  33. var conf, error, rv;
  34. conf = env.config.pug || {};
  35. conf.filename = filepath.full;
  36. try {
  37. rv = pug.compile(buffer.toString(), conf);
  38. return callback(null, new _this(rv));
  39. } catch (error1) {
  40. error = error1;
  41. return callback(error);
  42. }
  43. };
  44. })(this)
  45. ], callback);
  46. };
  47. env.registerTemplatePlugin('**/*.*(pug|jade)', PugTemplate);
  48. return callback();
  49. };
  50. }).call(this);