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.

246 lines
8.7 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. module.exports = function(BasePlugin) {
  5. var PagedPlugin, TaskGroup, _ref;
  6. TaskGroup = require('taskgroup').TaskGroup;
  7. return PagedPlugin = (function(_super) {
  8. __extends(PagedPlugin, _super);
  9. function PagedPlugin() {
  10. _ref = PagedPlugin.__super__.constructor.apply(this, arguments);
  11. return _ref;
  12. }
  13. PagedPlugin.prototype.name = 'paged';
  14. PagedPlugin.prototype.docpadReady = function(opts, next) {
  15. var DocumentModel, docpad;
  16. docpad = opts.docpad;
  17. DocumentModel = docpad.DocumentModel;
  18. DocumentModel.prototype.getPagedUrl = function(pageNumber) {
  19. var baseName, cleanUrls, firstPage, firstPageUrl, newUrl, outExtension;
  20. firstPage = this.get('firstPageDoc');
  21. outExtension = firstPage.get('outExtension');
  22. baseName = firstPage.get('basename');
  23. if (pageNumber === 0) {
  24. return firstPage.get('url');
  25. }
  26. firstPageUrl = firstPage.get('firstPageUrl');
  27. if (firstPageUrl === '/') {
  28. newUrl = '/index.' + pageNumber + '.html';
  29. } else {
  30. newUrl = firstPageUrl.replace(/\.html/, '.' + pageNumber + '.html');
  31. }
  32. cleanUrls = docpad.getPlugin('cleanurls');
  33. if (cleanUrls) {
  34. newUrl = newUrl.replace(/\.html$/, '');
  35. }
  36. return newUrl;
  37. };
  38. DocumentModel.prototype.hasNextPage = function() {
  39. var page;
  40. page = this.get('page');
  41. if (page.number < page.count - 1) {
  42. return true;
  43. }
  44. return false;
  45. };
  46. DocumentModel.prototype.getNextPage = function() {
  47. var page;
  48. page = this.get('page');
  49. if (page.number < page.count - 1) {
  50. return this.getPagedUrl(page.number + 1);
  51. }
  52. return '#';
  53. };
  54. DocumentModel.prototype.hasPrevPage = function() {
  55. var page;
  56. page = this.get('page');
  57. if (page.number > 0) {
  58. return true;
  59. }
  60. return false;
  61. };
  62. DocumentModel.prototype.getPrevPage = function() {
  63. var page;
  64. page = this.get('page');
  65. if (page.number > 0) {
  66. return this.getPagedUrl(page.number - 1);
  67. }
  68. return '#';
  69. };
  70. return next();
  71. };
  72. PagedPlugin.prototype.renderBefore = function(opts, next) {
  73. var collection, docpad, getCleanOutPathFromUrl, pagesToRender, tasks, templateData;
  74. docpad = this.docpad;
  75. debugger;
  76. collection = opts.collection, templateData = opts.templateData;
  77. pagesToRender = new docpad.FilesCollection();
  78. collection.forEach(function(document) {
  79. var lastDoc, meta, n, numberOfPages, pageSize, pagedCollection, pagedCollectionName, pagedDoc, pagedDocData, _i, _ref1;
  80. meta = document.getMeta();
  81. if (!meta.get('isPaged')) {
  82. docpad.log('debug', 'Document ' + document.get('basename') + ' is not paged');
  83. return;
  84. }
  85. numberOfPages = meta.get('pageCount') || 1;
  86. pageSize = meta.get('pageSize') || 5;
  87. lastDoc = pageSize * numberOfPages;
  88. if (meta.get('pagedCollection')) {
  89. pagedCollectionName = meta.get('pagedCollection');
  90. pagedCollection = docpad.getCollection(pagedCollectionName);
  91. numberOfPages = Math.ceil(pagedCollection.length / pageSize);
  92. lastDoc = pagedCollection.length;
  93. }
  94. docpad.log('debug', 'Document ' + document.get('basename') + ' has ' + numberOfPages + ' pages');
  95. document.set({
  96. page: {
  97. count: numberOfPages,
  98. number: 0,
  99. size: pageSize,
  100. startIdx: 0,
  101. endIdx: Math.min(pageSize, lastDoc)
  102. }
  103. });
  104. document.set({
  105. firstPageDoc: document
  106. });
  107. document.set({
  108. firstPageUrl: document.get('url')
  109. });
  110. if (numberOfPages > 1) {
  111. for (n = _i = 1, _ref1 = numberOfPages - 1; 1 <= _ref1 ? _i <= _ref1 : _i >= _ref1; n = 1 <= _ref1 ? ++_i : --_i) {
  112. pagedDocData = document.toJSON();
  113. pagedDoc = docpad.createDocument(pagedDocData);
  114. pagedDoc.set({
  115. page: {
  116. count: numberOfPages,
  117. number: n,
  118. size: pageSize,
  119. startIdx: n * pageSize,
  120. endIdx: Math.min((n * pageSize) + pageSize, lastDoc)
  121. }
  122. });
  123. pagedDoc.set({
  124. firstPageDoc: document
  125. });
  126. pagesToRender.add(pagedDoc);
  127. }
  128. }
  129. return this;
  130. });
  131. tasks = new TaskGroup().setConfig({
  132. concurrency: 0
  133. }).once('complete', next);
  134. getCleanOutPathFromUrl = function(url) {
  135. url = url.replace(/\/+$/, '');
  136. if (/index.html$/.test(url)) {
  137. return pathUtil.join(docpadConfig.outPath, url);
  138. } else {
  139. return pathUtil.join(docpadConfig.outPath, url.replace(/\.html$/, '') + '/index.html');
  140. }
  141. };
  142. pagesToRender.forEach(function(document) {
  143. tasks.addTask(function(complete) {
  144. docpad.log('debug', 'Normalizing paging document ' + document.get('basename'));
  145. return document.normalize({}, complete);
  146. });
  147. tasks.addTask(function(complete) {
  148. var basename, page;
  149. page = document.get('page');
  150. basename = document.get('basename');
  151. basename = basename + '.' + page.number;
  152. docpad.log('debug', 'Renaming paging document ' + document.get('basename') + ' to ' + basename);
  153. document.id = document.id.replace(/\.html/, '.' + page.number + '.html');
  154. document.set('basename', document.get('basename') + '.' + page.number);
  155. return complete();
  156. });
  157. tasks.addTask(function(complete) {
  158. docpad.log('debug', 'Contextualizing paging document ' + document.get('basename'));
  159. return document.contextualize({}, complete);
  160. });
  161. return tasks.addTask(function(complete) {
  162. var basename, outFilename, outPath, page;
  163. page = document.get('page');
  164. basename = document.get('basename');
  165. outFilename = document.get('outFilename');
  166. outPath = document.get('outPath');
  167. outFilename = outFilename.replace(basename, basename + '.' + page.number);
  168. outPath = outPath.replace(basename, basename + '.' + page.number);
  169. basename = basename + '.' + page.number;
  170. /*
  171. docpad.log('debug','Renaming paging document ' + document.get('basename') + ' to ' + basename)
  172. document.set('basename',basename)
  173. document.set('outFilename', outFilename)
  174. document.set('outPath', outPath)
  175. document.id = document.id.replace(/\.html/,'.'+page.number+'.html');
  176. #update urls
  177. urls = document.get('urls')
  178. for n in [0..urls.length-1]
  179. urls[n] = urls[n].replace(/\.html$/,'.'+page.number+'.html')
  180. document.set('urls',urls)
  181. document.set('url',document.get('url').replace(/\.html$/,'.'+page.number+'.html'))
  182. */
  183. return complete();
  184. });
  185. });
  186. this.pagesToRender = pagesToRender;
  187. return tasks.run();
  188. };
  189. PagedPlugin.prototype.renderAfter = function(opts, next) {
  190. var cleanUrls, collection, database, docpad, pagesToRender, tasks;
  191. docpad = this.docpad;
  192. collection = opts.collection;
  193. pagesToRender = this.pagesToRender;
  194. database = docpad.getDatabase('html');
  195. cleanUrls = docpad.getPlugin('cleanurls');
  196. if (pagesToRender.length > 0) {
  197. docpad.log('debug', 'Rendering ' + pagesToRender.length + ' paged documents');
  198. tasks = new TaskGroup().setConfig({
  199. concurrency: 0
  200. }).once('complete', next);
  201. pagesToRender.forEach(function(document) {
  202. tasks.addTask(function(complete) {
  203. return document.render({
  204. templateData: docpad.getTemplateData()
  205. }, complete);
  206. });
  207. return tasks.addTask(function(complete) {
  208. if (cleanUrls) {
  209. cleanUrls.cleanUrlsForDocument(document);
  210. }
  211. database.add(document);
  212. return complete();
  213. });
  214. });
  215. return tasks.run();
  216. } else {
  217. return next();
  218. }
  219. };
  220. return PagedPlugin;
  221. })(BasePlugin);
  222. };