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

// 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; },
__slice = [].slice;
module.exports = function(BasePlugin) {
var PartialsPlugin, Task, TaskGroup, extendr, pathUtil, util, _ref;
extendr = require('extendr');
_ref = require('taskgroup'), Task = _ref.Task, TaskGroup = _ref.TaskGroup;
pathUtil = require('path');
util = require('util');
return PartialsPlugin = (function(_super) {
__extends(PartialsPlugin, _super);
PartialsPlugin.prototype.name = 'partials';
PartialsPlugin.prototype.config = {
partialsPath: 'partials'
};
PartialsPlugin.prototype.locale = {
addingPartial: "Adding partial: %s",
partialNotFound: "The partial \"%s\" was not found, as such it will not be rendered.",
renderPartial: "Rendering partial: %s",
renderedPartial: "Rendered partial: %s",
renderPartialFailed: "Rendering partial failed: %s. The error follows:"
};
PartialsPlugin.prototype.foundPartials = null;
PartialsPlugin.prototype.partialsCache = null;
function PartialsPlugin() {
PartialsPlugin.__super__.constructor.apply(this, arguments);
this.partialsCache = {};
this.foundPartials = {};
this.config.partialsPath = pathUtil.resolve(this.docpad.getConfig().srcPath, this.config.partialsPath);
}
PartialsPlugin.prototype.setConfig = function() {
PartialsPlugin.__super__.setConfig.apply(this, arguments);
this.config.partialsPath = pathUtil.resolve(this.docpad.getConfig().srcPath, this.config.partialsPath);
return this;
};
PartialsPlugin.prototype.populateCollections = function(opts, next) {
var config, docpad;
config = this.config;
docpad = this.docpad;
docpad.parseDocumentDirectory({
path: config.partialsPath
}, next);
return this;
};
PartialsPlugin.prototype.extendCollections = function(opts) {
var config, docpad, locale;
config = this.config;
docpad = this.docpad;
locale = this.locale;
docpad.setCollection('partials', docpad.database.createLiveChildCollection().setQuery('isLayout', {
$or: {
isPartial: true,
fullPath: {
$startsWith: config.partialsPath
}
}
}).on('add', function(model) {
var _base, _base1, _base2, _ref1, _ref2, _ref3;
docpad.log('debug', util.format(locale.addingPartial, model.attributes.fullPath));
if ((_ref1 = (_base = model.attributes).isPartial) == null) {
_base.isPartial = true;
}
if ((_ref2 = (_base1 = model.attributes).render) == null) {
_base1.render = false;
}
return (_ref3 = (_base2 = model.attributes).write) != null ? _ref3 : _base2.write = false;
}));
return this;
};
PartialsPlugin.prototype.renderPartial = function(partial, next) {
var cacheable, docpad, locale, partialsCache, result, _ref1, _ref2;
docpad = this.docpad;
locale = this.locale;
partialsCache = this.partialsCache;
result = null;
cacheable = (_ref1 = partial.document.getMeta().get('cacheable')) != null ? _ref1 : false;
if (cacheable === true) {
result = (_ref2 = partialsCache[partial.code]) != null ? _ref2 : null;
}
if (result != null) {
return next(null, result);
}
docpad.renderDocument(partial.document, {
templateData: partial.data
}, function(err, result, document) {
if (err) {
return next(err);
}
if (cacheable === true) {
partialsCache[partial.code] = result;
}
return next(null, result);
});
return this;
};
PartialsPlugin.prototype.extendTemplateData = function(_arg) {
var config, docpad, locale, me, templateData;
templateData = _arg.templateData;
me = this;
docpad = this.docpad, locale = this.locale, config = this.config;
templateData.partial = function() {
var err, file, message, objs, partial, partialFuzzyPath, partialName, _ref1, _ref2, _ref3;
partialName = arguments[0], objs = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (typeof this.referencesOthers === "function") {
this.referencesOthers();
}
file = this.documentModel;
partial = {};
partialFuzzyPath = pathUtil.join(config.partialsPath, partialName);
if ((_ref1 = partial.document) == null) {
partial.document = docpad.getCollection('partials').fuzzyFindOne(partialFuzzyPath);
}
if (!partial.document) {
message = util.format(locale.partialNotFound, partialName);
err = new Error(message);
partial.err = err;
return message;
}
partial.data = objs.length >= 2 ? (objs.unshift({}), extendr.shallowExtendPlainObjects.apply(extendr, objs)) : (_ref2 = objs[0]) != null ? _ref2 : {};
partial.code = partial.document.id;
partial.id = Math.random();
partial.container = '[partial:' + partial.id + ']';
if (me.foundPartials[partial.id]) {
return partial.container;
}
me.foundPartials[partial.id] = partial;
if ((_ref3 = partial.task) == null) {
partial.task = new Task(function(complete) {
return me.renderPartial(partial, function(err, result) {
var _ref4;
partial.err = err;
partial.result = (_ref4 = result != null ? result : err != null ? err.toString() : void 0) != null ? _ref4 : '???';
return complete();
});
});
}
partial.task.run();
return partial.container;
};
return this;
};
PartialsPlugin.prototype.renderDocument = function(opts, next) {
var file, me, partialContainerRegex, partialContainers, tasks, templateData;
templateData = opts.templateData, file = opts.file;
partialContainerRegex = /\[partial:([^\]]+)\]/g;
partialContainers = (opts.content || '').match(partialContainerRegex) || [];
if (partialContainers.length === 0) {
return next();
}
me = this;
tasks = new TaskGroup().setConfig({
concurrency: 0
}).once('complete', function() {
opts.content = opts.content.replace(partialContainerRegex, function(match, partialId) {
var partial;
partial = me.foundPartials[partialId];
return partial.result;
});
return next();
});
partialContainers.forEach(function(partialContainer) {
var partial, partialId;
partialId = partialContainer.replace(partialContainerRegex, '$1');
partial = me.foundPartials[partialId];
return tasks.addTask(function(complete) {
return partial.task.once('complete', complete).complete();
});
});
tasks.run();
return this;
};
PartialsPlugin.prototype.generateAfter = function() {
this.foundPartials = {};
return this.partialsCache = {};
};
return PartialsPlugin;
})(BasePlugin);
};