jade.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (function() {
  2. var async, fs, jade, path,
  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. jade = require('jade-legacy');
  8. path = require('path');
  9. module.exports = function(env, callback) {
  10. var JadeTemplate;
  11. JadeTemplate = (function(superClass) {
  12. extend(JadeTemplate, superClass);
  13. function JadeTemplate(fn) {
  14. this.fn = fn;
  15. }
  16. JadeTemplate.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 JadeTemplate;
  26. })(env.TemplatePlugin);
  27. JadeTemplate.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.jade || {};
  35. conf.filename = filepath.full;
  36. try {
  37. rv = jade.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('**/*.jade', JadeTemplate);
  48. return callback();
  49. };
  50. }).call(this);