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

  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 TextPlugin, _ref;
  6. return TextPlugin = (function(_super) {
  7. __extends(TextPlugin, _super);
  8. function TextPlugin() {
  9. _ref = TextPlugin.__super__.constructor.apply(this, arguments);
  10. return _ref;
  11. }
  12. TextPlugin.prototype.name = 'text';
  13. TextPlugin.prototype.getText = function(opts) {
  14. var err, key, result, source, store, _ref1;
  15. source = opts.source, store = opts.store;
  16. key = source || '';
  17. key = 'store.' + key.replace(/[#\{\(\n]/g, '').trim();
  18. try {
  19. result = (_ref1 = eval(key)) != null ? _ref1 : source;
  20. } catch (_error) {
  21. err = _error;
  22. result = source;
  23. }
  24. return result;
  25. };
  26. TextPlugin.prototype.populateText = function(opts, next) {
  27. var TaskGroup, balUtil, docpad, file, me, replaceElementCallback, source, templateData;
  28. me = this;
  29. docpad = this.docpad;
  30. balUtil = require('bal-util');
  31. TaskGroup = require('taskgroup').TaskGroup;
  32. source = opts.source, templateData = opts.templateData, file = opts.file;
  33. replaceElementCallback = function(outerHTML, elementName, attributes, innerHTML, replaceElementCompleteCallback) {
  34. var extensions, replaceElementTasks, result;
  35. result = me.getText({
  36. source: innerHTML,
  37. store: templateData
  38. });
  39. replaceElementTasks = new TaskGroup().once('complete', function(err) {
  40. return replaceElementCompleteCallback(err, result);
  41. });
  42. replaceElementTasks.addTask(function(complete) {
  43. return me.populateText({
  44. file: file,
  45. templateData: templateData,
  46. source: result
  47. }, function(err, populateTextResult) {
  48. if (err) {
  49. return complete(err);
  50. }
  51. result = populateTextResult;
  52. return complete();
  53. });
  54. });
  55. extensions = balUtil.getAttribute(attributes, 'type') || balUtil.getAttribute(attributes, 'render') || '';
  56. if (extensions) {
  57. replaceElementTasks.addTask(function(complete) {
  58. var filename, parentExtension, parentFilename, renderTextOpts;
  59. filename = 'docpad-text-plugin';
  60. parentExtension = file.get('outExtension') || file.get('extensionRendered');
  61. parentFilename = file.get('filename');
  62. if (extensions.indexOf('.') === -1 && (parentExtension || parentFilename)) {
  63. filename += '.' + (parentExtension || parentFilename);
  64. }
  65. filename += '.' + extensions;
  66. renderTextOpts = {
  67. filename: filename,
  68. templateData: templateData,
  69. renderSingleExtensions: 'auto',
  70. actions: ['renderExtensions']
  71. };
  72. return docpad.renderText(result, renderTextOpts, function(err, renderTextResult, document) {
  73. if (err) {
  74. return complete(err);
  75. }
  76. result = renderTextResult;
  77. return complete();
  78. });
  79. });
  80. }
  81. return replaceElementTasks.run();
  82. };
  83. balUtil.replaceElementAsync(source, 't(?:ext)?', replaceElementCallback, next);
  84. return this;
  85. };
  86. TextPlugin.prototype.extendTemplateData = function(opts) {
  87. var me, templateData;
  88. me = this;
  89. templateData = opts.templateData;
  90. templateData.t = function(str, opts) {
  91. var attrs, id, text;
  92. id = Math.random().toString().replace('.', '');
  93. attrs = '';
  94. if (opts.render) {
  95. opts.render = opts.render.toString().replace(/^['"]+|['"]+$/g, '');
  96. attrs += " render=\"" + opts.render + "\"";
  97. }
  98. text = "<text:" + id + attrs + ">" + str + "</text:" + id + ">";
  99. return text;
  100. };
  101. return this;
  102. };
  103. TextPlugin.prototype.renderDocument = function(opts, next) {
  104. var file, me, templateData;
  105. me = this;
  106. templateData = opts.templateData, file = opts.file;
  107. if (file.isText()) {
  108. return me.populateText({
  109. file: file,
  110. templateData: templateData,
  111. source: opts.content
  112. }, function(err, result) {
  113. if (err) {
  114. return next(err);
  115. }
  116. opts.content = result;
  117. return next();
  118. });
  119. } else {
  120. return next();
  121. }
  122. };
  123. return TextPlugin;
  124. })(BasePlugin);
  125. };