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.
 
 
 

142 lines
4.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 TextPlugin, _ref;
return TextPlugin = (function(_super) {
__extends(TextPlugin, _super);
function TextPlugin() {
_ref = TextPlugin.__super__.constructor.apply(this, arguments);
return _ref;
}
TextPlugin.prototype.name = 'text';
TextPlugin.prototype.getText = function(opts) {
var err, key, result, source, store, _ref1;
source = opts.source, store = opts.store;
key = source || '';
key = 'store.' + key.replace(/[#\{\(\n]/g, '').trim();
try {
result = (_ref1 = eval(key)) != null ? _ref1 : source;
} catch (_error) {
err = _error;
result = source;
}
return result;
};
TextPlugin.prototype.populateText = function(opts, next) {
var TaskGroup, balUtil, docpad, file, me, replaceElementCallback, source, templateData;
me = this;
docpad = this.docpad;
balUtil = require('bal-util');
TaskGroup = require('taskgroup').TaskGroup;
source = opts.source, templateData = opts.templateData, file = opts.file;
replaceElementCallback = function(outerHTML, elementName, attributes, innerHTML, replaceElementCompleteCallback) {
var extensions, replaceElementTasks, result;
result = me.getText({
source: innerHTML,
store: templateData
});
replaceElementTasks = new TaskGroup().once('complete', function(err) {
return replaceElementCompleteCallback(err, result);
});
replaceElementTasks.addTask(function(complete) {
return me.populateText({
file: file,
templateData: templateData,
source: result
}, function(err, populateTextResult) {
if (err) {
return complete(err);
}
result = populateTextResult;
return complete();
});
});
extensions = balUtil.getAttribute(attributes, 'type') || balUtil.getAttribute(attributes, 'render') || '';
if (extensions) {
replaceElementTasks.addTask(function(complete) {
var filename, parentExtension, parentFilename, renderTextOpts;
filename = 'docpad-text-plugin';
parentExtension = file.get('outExtension') || file.get('extensionRendered');
parentFilename = file.get('filename');
if (extensions.indexOf('.') === -1 && (parentExtension || parentFilename)) {
filename += '.' + (parentExtension || parentFilename);
}
filename += '.' + extensions;
renderTextOpts = {
filename: filename,
templateData: templateData,
renderSingleExtensions: 'auto',
actions: ['renderExtensions']
};
return docpad.renderText(result, renderTextOpts, function(err, renderTextResult, document) {
if (err) {
return complete(err);
}
result = renderTextResult;
return complete();
});
});
}
return replaceElementTasks.run();
};
balUtil.replaceElementAsync(source, 't(?:ext)?', replaceElementCallback, next);
return this;
};
TextPlugin.prototype.extendTemplateData = function(opts) {
var me, templateData;
me = this;
templateData = opts.templateData;
templateData.t = function(str, opts) {
var attrs, id, text;
id = Math.random().toString().replace('.', '');
attrs = '';
if (opts.render) {
opts.render = opts.render.toString().replace(/^['"]+|['"]+$/g, '');
attrs += " render=\"" + opts.render + "\"";
}
text = "<text:" + id + attrs + ">" + str + "</text:" + id + ">";
return text;
};
return this;
};
TextPlugin.prototype.renderDocument = function(opts, next) {
var file, me, templateData;
me = this;
templateData = opts.templateData, file = opts.file;
if (file.isText()) {
return me.populateText({
file: file,
templateData: templateData,
source: opts.content
}, function(err, result) {
if (err) {
return next(err);
}
opts.content = result;
return next();
});
} else {
return next();
}
};
return TextPlugin;
})(BasePlugin);
};