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.

206 lines
7.3 KiB

  1. // Generated by CoffeeScript 1.6.2
  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. __slice = [].slice;
  5. module.exports = function(BasePlugin) {
  6. var PartialsPlugin, Task, TaskGroup, extendr, pathUtil, util, _ref;
  7. extendr = require('extendr');
  8. _ref = require('taskgroup'), Task = _ref.Task, TaskGroup = _ref.TaskGroup;
  9. pathUtil = require('path');
  10. util = require('util');
  11. return PartialsPlugin = (function(_super) {
  12. __extends(PartialsPlugin, _super);
  13. PartialsPlugin.prototype.name = 'partials';
  14. PartialsPlugin.prototype.config = {
  15. partialsPath: 'partials'
  16. };
  17. PartialsPlugin.prototype.locale = {
  18. addingPartial: "Adding partial: %s",
  19. partialNotFound: "The partial \"%s\" was not found, as such it will not be rendered.",
  20. renderPartial: "Rendering partial: %s",
  21. renderedPartial: "Rendered partial: %s",
  22. renderPartialFailed: "Rendering partial failed: %s. The error follows:"
  23. };
  24. PartialsPlugin.prototype.foundPartials = null;
  25. PartialsPlugin.prototype.partialsCache = null;
  26. function PartialsPlugin() {
  27. PartialsPlugin.__super__.constructor.apply(this, arguments);
  28. this.partialsCache = {};
  29. this.foundPartials = {};
  30. this.config.partialsPath = pathUtil.resolve(this.docpad.getConfig().srcPath, this.config.partialsPath);
  31. }
  32. PartialsPlugin.prototype.setConfig = function() {
  33. PartialsPlugin.__super__.setConfig.apply(this, arguments);
  34. this.config.partialsPath = pathUtil.resolve(this.docpad.getConfig().srcPath, this.config.partialsPath);
  35. return this;
  36. };
  37. PartialsPlugin.prototype.populateCollections = function(opts, next) {
  38. var config, docpad;
  39. config = this.config;
  40. docpad = this.docpad;
  41. docpad.parseDocumentDirectory({
  42. path: config.partialsPath
  43. }, next);
  44. return this;
  45. };
  46. PartialsPlugin.prototype.extendCollections = function(opts) {
  47. var config, docpad, locale;
  48. config = this.config;
  49. docpad = this.docpad;
  50. locale = this.locale;
  51. docpad.setCollection('partials', docpad.database.createLiveChildCollection().setQuery('isLayout', {
  52. $or: {
  53. isPartial: true,
  54. fullPath: {
  55. $startsWith: config.partialsPath
  56. }
  57. }
  58. }).on('add', function(model) {
  59. var _base, _base1, _base2, _ref1, _ref2, _ref3;
  60. docpad.log('debug', util.format(locale.addingPartial, model.attributes.fullPath));
  61. if ((_ref1 = (_base = model.attributes).isPartial) == null) {
  62. _base.isPartial = true;
  63. }
  64. if ((_ref2 = (_base1 = model.attributes).render) == null) {
  65. _base1.render = false;
  66. }
  67. return (_ref3 = (_base2 = model.attributes).write) != null ? _ref3 : _base2.write = false;
  68. }));
  69. return this;
  70. };
  71. PartialsPlugin.prototype.renderPartial = function(partial, next) {
  72. var cacheable, docpad, locale, partialsCache, result, _ref1, _ref2;
  73. docpad = this.docpad;
  74. locale = this.locale;
  75. partialsCache = this.partialsCache;
  76. result = null;
  77. cacheable = (_ref1 = partial.document.getMeta().get('cacheable')) != null ? _ref1 : false;
  78. if (cacheable === true) {
  79. result = (_ref2 = partialsCache[partial.code]) != null ? _ref2 : null;
  80. }
  81. if (result != null) {
  82. return next(null, result);
  83. }
  84. docpad.renderDocument(partial.document, {
  85. templateData: partial.data
  86. }, function(err, result, document) {
  87. if (err) {
  88. return next(err);
  89. }
  90. if (cacheable === true) {
  91. partialsCache[partial.code] = result;
  92. }
  93. return next(null, result);
  94. });
  95. return this;
  96. };
  97. PartialsPlugin.prototype.extendTemplateData = function(_arg) {
  98. var config, docpad, locale, me, templateData;
  99. templateData = _arg.templateData;
  100. me = this;
  101. docpad = this.docpad, locale = this.locale, config = this.config;
  102. templateData.partial = function() {
  103. var err, file, message, objs, partial, partialFuzzyPath, partialName, _ref1, _ref2, _ref3;
  104. partialName = arguments[0], objs = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  105. if (typeof this.referencesOthers === "function") {
  106. this.referencesOthers();
  107. }
  108. file = this.documentModel;
  109. partial = {};
  110. partialFuzzyPath = pathUtil.join(config.partialsPath, partialName);
  111. if ((_ref1 = partial.document) == null) {
  112. partial.document = docpad.getCollection('partials').fuzzyFindOne(partialFuzzyPath);
  113. }
  114. if (!partial.document) {
  115. message = util.format(locale.partialNotFound, partialName);
  116. err = new Error(message);
  117. partial.err = err;
  118. return message;
  119. }
  120. partial.data = objs.length >= 2 ? (objs.unshift({}), extendr.shallowExtendPlainObjects.apply(extendr, objs)) : (_ref2 = objs[0]) != null ? _ref2 : {};
  121. partial.code = partial.document.id;
  122. partial.id = Math.random();
  123. partial.container = '[partial:' + partial.id + ']';
  124. if (me.foundPartials[partial.id]) {
  125. return partial.container;
  126. }
  127. me.foundPartials[partial.id] = partial;
  128. if ((_ref3 = partial.task) == null) {
  129. partial.task = new Task(function(complete) {
  130. return me.renderPartial(partial, function(err, result) {
  131. var _ref4;
  132. partial.err = err;
  133. partial.result = (_ref4 = result != null ? result : err != null ? err.toString() : void 0) != null ? _ref4 : '???';
  134. return complete();
  135. });
  136. });
  137. }
  138. partial.task.run();
  139. return partial.container;
  140. };
  141. return this;
  142. };
  143. PartialsPlugin.prototype.renderDocument = function(opts, next) {
  144. var file, me, partialContainerRegex, partialContainers, tasks, templateData;
  145. templateData = opts.templateData, file = opts.file;
  146. partialContainerRegex = /\[partial:([^\]]+)\]/g;
  147. partialContainers = opts.content.match(partialContainerRegex) || [];
  148. if (partialContainers.length === 0) {
  149. return next();
  150. }
  151. me = this;
  152. tasks = new TaskGroup().setConfig({
  153. concurrency: 0
  154. }).once('complete', function() {
  155. opts.content = opts.content.replace(partialContainerRegex, function(match, partialId) {
  156. var partial;
  157. partial = me.foundPartials[partialId];
  158. return partial.result;
  159. });
  160. return next();
  161. });
  162. partialContainers.forEach(function(partialContainer) {
  163. var partial, partialId;
  164. partialId = partialContainer.replace(partialContainerRegex, '$1');
  165. partial = me.foundPartials[partialId];
  166. return tasks.addTask(function(complete) {
  167. return partial.task.once('complete', complete).complete();
  168. });
  169. });
  170. tasks.run();
  171. return this;
  172. };
  173. PartialsPlugin.prototype.generateAfter = function() {
  174. this.foundPartials = {};
  175. return this.partialsCache = {};
  176. };
  177. return PartialsPlugin;
  178. })(BasePlugin);
  179. };