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

// Generated by CoffeeScript 1.6.2
var __hasProp = {}.hasOwnProperty,
__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; };
module.exports = function(BasePlugin) {
var PagedPlugin, TaskGroup, _ref;
TaskGroup = require('taskgroup').TaskGroup;
return PagedPlugin = (function(_super) {
__extends(PagedPlugin, _super);
function PagedPlugin() {
_ref = PagedPlugin.__super__.constructor.apply(this, arguments);
return _ref;
}
PagedPlugin.prototype.name = 'paged';
PagedPlugin.prototype.docpadReady = function(opts, next) {
var DocumentModel, docpad;
docpad = opts.docpad;
DocumentModel = docpad.DocumentModel;
DocumentModel.prototype.getPagedUrl = function(pageNumber) {
var baseName, cleanUrls, firstPage, firstPageUrl, newUrl, outExtension;
firstPage = this.get('firstPageDoc');
outExtension = firstPage.get('outExtension');
baseName = firstPage.get('basename');
if (pageNumber === 0) {
return firstPage.get('url');
}
firstPageUrl = firstPage.get('firstPageUrl');
if (firstPageUrl === '/') {
newUrl = '/index.' + pageNumber + '.html';
} else {
newUrl = firstPageUrl.replace(/\.html/, '.' + pageNumber + '.html');
}
cleanUrls = docpad.getPlugin('cleanurls');
if (cleanUrls) {
newUrl = newUrl.replace(/\.html$/, '');
}
return newUrl;
};
DocumentModel.prototype.hasNextPage = function() {
var page;
page = this.get('page');
if (page.number < page.count - 1) {
return true;
}
return false;
};
DocumentModel.prototype.getNextPage = function() {
var page;
page = this.get('page');
if (page.number < page.count - 1) {
return this.getPagedUrl(page.number + 1);
}
return '#';
};
DocumentModel.prototype.hasPrevPage = function() {
var page;
page = this.get('page');
if (page.number > 0) {
return true;
}
return false;
};
DocumentModel.prototype.getPrevPage = function() {
var page;
page = this.get('page');
if (page.number > 0) {
return this.getPagedUrl(page.number - 1);
}
return '#';
};
return next();
};
PagedPlugin.prototype.renderBefore = function(opts, next) {
var collection, docpad, getCleanOutPathFromUrl, pagesToRender, tasks, templateData;
docpad = this.docpad;
debugger;
collection = opts.collection, templateData = opts.templateData;
pagesToRender = new docpad.FilesCollection();
collection.forEach(function(document) {
var lastDoc, meta, n, numberOfPages, pageSize, pagedCollection, pagedCollectionName, pagedDoc, pagedDocData, _i, _ref1;
meta = document.getMeta();
if (!meta.get('isPaged')) {
docpad.log('debug', 'Document ' + document.get('basename') + ' is not paged');
return;
}
numberOfPages = meta.get('pageCount') || 1;
pageSize = meta.get('pageSize') || 5;
lastDoc = pageSize * numberOfPages;
if (meta.get('pagedCollection')) {
pagedCollectionName = meta.get('pagedCollection');
pagedCollection = docpad.getCollection(pagedCollectionName);
numberOfPages = Math.ceil(pagedCollection.length / pageSize);
lastDoc = pagedCollection.length;
}
docpad.log('debug', 'Document ' + document.get('basename') + ' has ' + numberOfPages + ' pages');
document.set({
page: {
count: numberOfPages,
number: 0,
size: pageSize,
startIdx: 0,
endIdx: Math.min(pageSize, lastDoc)
}
});
document.set({
firstPageDoc: document
});
document.set({
firstPageUrl: document.get('url')
});
if (numberOfPages > 1) {
for (n = _i = 1, _ref1 = numberOfPages - 1; 1 <= _ref1 ? _i <= _ref1 : _i >= _ref1; n = 1 <= _ref1 ? ++_i : --_i) {
pagedDocData = document.toJSON();
pagedDoc = docpad.createDocument(pagedDocData);
pagedDoc.set({
page: {
count: numberOfPages,
number: n,
size: pageSize,
startIdx: n * pageSize,
endIdx: Math.min((n * pageSize) + pageSize, lastDoc)
}
});
pagedDoc.set({
firstPageDoc: document
});
pagesToRender.add(pagedDoc);
}
}
return this;
});
tasks = new TaskGroup().setConfig({
concurrency: 0
}).once('complete', next);
getCleanOutPathFromUrl = function(url) {
url = url.replace(/\/+$/, '');
if (/index.html$/.test(url)) {
return pathUtil.join(docpadConfig.outPath, url);
} else {
return pathUtil.join(docpadConfig.outPath, url.replace(/\.html$/, '') + '/index.html');
}
};
pagesToRender.forEach(function(document) {
tasks.addTask(function(complete) {
docpad.log('debug', 'Normalizing paging document ' + document.get('basename'));
return document.normalize({}, complete);
});
tasks.addTask(function(complete) {
var basename, page;
page = document.get('page');
basename = document.get('basename');
basename = basename + '.' + page.number;
docpad.log('debug', 'Renaming paging document ' + document.get('basename') + ' to ' + basename);
document.id = document.id.replace(/\.html/, '.' + page.number + '.html');
document.set('basename', document.get('basename') + '.' + page.number);
return complete();
});
tasks.addTask(function(complete) {
docpad.log('debug', 'Contextualizing paging document ' + document.get('basename'));
return document.contextualize({}, complete);
});
return tasks.addTask(function(complete) {
var basename, outFilename, outPath, page;
page = document.get('page');
basename = document.get('basename');
outFilename = document.get('outFilename');
outPath = document.get('outPath');
outFilename = outFilename.replace(basename, basename + '.' + page.number);
outPath = outPath.replace(basename, basename + '.' + page.number);
basename = basename + '.' + page.number;
/*
docpad.log('debug','Renaming paging document ' + document.get('basename') + ' to ' + basename)
document.set('basename',basename)
document.set('outFilename', outFilename)
document.set('outPath', outPath)
document.id = document.id.replace(/\.html/,'.'+page.number+'.html');
#update urls
urls = document.get('urls')
for n in [0..urls.length-1]
urls[n] = urls[n].replace(/\.html$/,'.'+page.number+'.html')
document.set('urls',urls)
document.set('url',document.get('url').replace(/\.html$/,'.'+page.number+'.html'))
*/
return complete();
});
});
this.pagesToRender = pagesToRender;
return tasks.run();
};
PagedPlugin.prototype.renderAfter = function(opts, next) {
var cleanUrls, collection, database, docpad, pagesToRender, tasks;
docpad = this.docpad;
collection = opts.collection;
pagesToRender = this.pagesToRender;
database = docpad.getDatabase('html');
cleanUrls = docpad.getPlugin('cleanurls');
if (pagesToRender.length > 0) {
docpad.log('debug', 'Rendering ' + pagesToRender.length + ' paged documents');
tasks = new TaskGroup().setConfig({
concurrency: 0
}).once('complete', next);
pagesToRender.forEach(function(document) {
tasks.addTask(function(complete) {
return document.render({
templateData: docpad.getTemplateData()
}, complete);
});
return tasks.addTask(function(complete) {
if (cleanUrls) {
cleanUrls.cleanUrlsForDocument(document);
}
database.add(document);
return complete();
});
});
return tasks.run();
} else {
return next();
}
};
return PagedPlugin;
})(BasePlugin);
};