templates.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* templates.coffee */
  2. (function() {
  3. var TemplatePlugin, async, extend, fs, loadTemplates, minimatch, path, readdirRecursive, ref;
  4. async = require('async');
  5. fs = require('fs');
  6. minimatch = require('minimatch');
  7. path = require('path');
  8. ref = require('./utils'), extend = ref.extend, readdirRecursive = ref.readdirRecursive;
  9. TemplatePlugin = (function() {
  10. function TemplatePlugin() {}
  11. /* A template plugin subclass have to implement a `render` instance method and a `fromFile` class method. */
  12. TemplatePlugin.prototype.render = function(locals, callback) {
  13. /* Render template using *locals* and *callback* with a ReadStream or Buffer containing the result. */
  14. throw new Error('Not implemented.');
  15. };
  16. return TemplatePlugin;
  17. })();
  18. TemplatePlugin.fromFile = function(filepath, callback) {
  19. /* *callback* with a instance of <TemplatePlugin> created from *filepath*. Where *filepath* is
  20. an object containing the full and relative (to templates directory) path to the file.
  21. */
  22. throw new Error('Not implemented.');
  23. };
  24. loadTemplates = function(env, callback) {
  25. /* Load and any templates associated with the environment *env*. Calls *callback* with
  26. a map of templates as {<filename>: <TemplatePlugin instance>}
  27. */
  28. var loadTemplate, resolveFilenames, templates;
  29. templates = {};
  30. resolveFilenames = function(filenames, callback) {
  31. return async.map(filenames, function(filename, callback) {
  32. return callback(null, {
  33. full: path.join(env.templatesPath, filename),
  34. relative: filename
  35. });
  36. }, callback);
  37. };
  38. loadTemplate = function(filepath, callback) {
  39. /* Create an template plugin instance from *filepath*. */
  40. var i, j, plugin, ref1;
  41. plugin = null;
  42. for (i = j = ref1 = env.templatePlugins.length - 1; j >= 0; i = j += -1) {
  43. if (minimatch(filepath.relative, env.templatePlugins[i].pattern)) {
  44. plugin = env.templatePlugins[i];
  45. break;
  46. }
  47. }
  48. if (plugin != null) {
  49. return plugin["class"].fromFile(filepath, function(error, template) {
  50. if (error != null) {
  51. error.message = "template " + filepath.relative + ": " + error.message;
  52. }
  53. templates[filepath.relative] = template;
  54. return callback(error);
  55. });
  56. } else {
  57. return callback();
  58. }
  59. };
  60. return async.waterfall([
  61. function(callback) {
  62. return readdirRecursive(env.templatesPath, callback);
  63. }, resolveFilenames, function(filenames, callback) {
  64. return async.forEach(filenames, loadTemplate, callback);
  65. }
  66. ], function(error) {
  67. return callback(error, templates);
  68. });
  69. };
  70. /* Exports */
  71. module.exports = {
  72. TemplatePlugin: TemplatePlugin,
  73. loadTemplates: loadTemplates
  74. };
  75. }).call(this);