You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.3 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. var __hasProp = {}.hasOwnProperty,
  3. __extends = 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. module.exports = function(BasePlugin) {
  5. var HandlebarsPlugin;
  6. return HandlebarsPlugin = (function(_super) {
  7. __extends(HandlebarsPlugin, _super);
  8. HandlebarsPlugin.prototype.name = 'handlebars';
  9. HandlebarsPlugin.prototype.handlebars = null;
  10. function HandlebarsPlugin() {
  11. var config, docpad, handlebars, helper, name, partial, _ref, _ref1;
  12. HandlebarsPlugin.__super__.constructor.apply(this, arguments);
  13. docpad = this.docpad;
  14. config = this.config;
  15. handlebars = this.handlebars = require('handlebars');
  16. this.precompileOpts = this.config.precompileOpts || {};
  17. if (this.config.helpers) {
  18. _ref = this.config.helpers;
  19. for (name in _ref) {
  20. if (!__hasProp.call(_ref, name)) continue;
  21. helper = _ref[name];
  22. handlebars.registerHelper(name, helper);
  23. }
  24. }
  25. if (this.config.partials) {
  26. _ref1 = this.config.partials;
  27. for (name in _ref1) {
  28. if (!__hasProp.call(_ref1, name)) continue;
  29. partial = _ref1[name];
  30. handlebars.registerPartial(name, partial);
  31. }
  32. }
  33. }
  34. HandlebarsPlugin.prototype.render = function(opts) {
  35. var content, handlebars, inExtension, outExtension, output, templateData;
  36. inExtension = opts.inExtension, outExtension = opts.outExtension, templateData = opts.templateData, content = opts.content;
  37. handlebars = this.handlebars;
  38. if (inExtension === 'hb' || inExtension === 'hbs' || inExtension === 'handlebars') {
  39. if (outExtension === 'js' || outExtension === 'inlinejs') {
  40. output = this.handlePrecompileOpts(opts);
  41. } else {
  42. output = handlebars.compile(opts.content)(templateData);
  43. }
  44. return opts.content = output;
  45. }
  46. };
  47. HandlebarsPlugin.prototype.handlePrecompileOpts = function(opts) {
  48. var argv, post, pre, templateName, _ref, _ref1, _ref2;
  49. argv = this.precompileOpts;
  50. if ((_ref = argv.wrapper) == null) {
  51. argv.wrapper = "default";
  52. }
  53. if ((_ref1 = argv.amdPath) == null) {
  54. argv.amdPath = "";
  55. }
  56. pre = post = "";
  57. templateName = opts.file.attributes.slug;
  58. if (argv.wrapper === "amd") {
  59. pre += 'define([\'' + argv.amdPath + 'handlebars\'], function(Handlebars) {\n';
  60. }
  61. if (argv.wrapper === "default") {
  62. pre += '(function() {\n';
  63. }
  64. if (((_ref2 = argv.wrapper) === "default" || _ref2 === "amd")) {
  65. pre += ' var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n';
  66. pre += 'templates[\'' + templateName + '\'] = template(';
  67. post += ');\n';
  68. }
  69. if (argv.wrapper === "amd") {
  70. post += '});';
  71. }
  72. if (argv.wrapper === "default") {
  73. post += '})();';
  74. }
  75. return pre + this.handlebars.precompile(opts.content) + post;
  76. };
  77. return HandlebarsPlugin;
  78. })(BasePlugin);
  79. };