/*\ title: js/Recipe.js FileRetriever can asynchronously retrieve files from HTTP URLs or the local file system Recipe processing is in four parts: 1) The recipe file is parsed and any subrecipe files loaded recursively into this structure: this.recipe = [ {marker: , filepath: , contextPath: }, ... {marker: , filepath: , contextPath: }, [ {marker: , filepath: , contextPath: }, ... {marker: , filepath: , contextPath: }, ] ]; 2) The tiddler files referenced by the recipe structure are loaded into it as an additional 'tiddlers' member that contains an array of hashmaps of tiddler field values. 3) The recipe is scanned to create a hashmap of markers and their associated tiddlers. In cases where more than one tiddler with the same title is assigned to a marker, the one that is later in the recipe file wins. At this point tiddlers are placed in the store so that they can be referenced by title this.markers = { : [,,...], : [,,...], ... } 4) Finally, the template is processed by replacing the markers with the text of the associated tiddlers \*/ (function(){ /*jslint node: true */ "use strict"; var Tiddler = require("./Tiddler.js").Tiddler, utils = require("./Utils.js"), retrieveFile = require("./FileRetriever.js").retrieveFile, fs = require("fs"), path = require("path"), util = require("util"), async = require("async"); var Recipe = function(options,callback) { var me = this; this.filepath = options.filepath; this.store = options.store; this.tiddlerConverters = options.tiddlerConverters; this.textProcessors = options.textProcessors; this.callback = callback; this.recipe = []; this.markers = {}; this.recipeQueue = async.queue(function(task,callback) { retrieveFile(task.filepath,task.contextPath,function(err,data) { if(err) { callback(err); } else { me.processRecipeFile(task.recipe,data.text,data.path); callback(null); } }); },1); this.tiddlerQueue = async.queue(function(task,callback) { me.readTiddlerFile(task.filepath,task.contextPath,function(err,data) { if(err) { callback(err); } else { task.recipeLine.tiddlers = data; callback(null); } }); },1); this.recipeQueue.drain = function() { me.loadTiddlerFiles(me.recipe); }; this.tiddlerQueue.drain = function() { me.chooseTiddlers(me.recipe); me.sortTiddlersForMarker("tiddler"); me.callback(null); }; this.recipeQueue.push({filepath: this.filepath, contextPath: process.cwd(), recipe: this.recipe}); }; Recipe.prototype.loadTiddlerFiles = function(recipe) { for(var r=0; r)|(?:<!--@@(.*)@@-->)$/gi; var match = templateRegExp.exec(line); if(match) { var marker = match[1] === undefined ? match[2] : match[1]; me.outputTiddlersForMarker(out,marker); } else { out.push(line); } }); return out.join("\n"); }; // Output all the tiddlers in the recipe with a particular marker Recipe.prototype.outputTiddlersForMarker = function(out,marker) { var tiddlers = this.markers[marker], outputType = Recipe.tiddlerOutputMapper[marker] || "raw", outputter = Recipe.tiddlerOutputter[outputType]; if(!tiddlers) { tiddlers = []; } if(outputter) { outputter.call(this,out,tiddlers); } }; // Allows for specialised processing for certain markers Recipe.tiddlerOutputMapper = { tiddler: "div", js: "javascript", jsdeprecated: "javascript", jquery: "javascript", shadow: "shadow", title: "title", jsmodule: "jsmodule" }; Recipe.tiddlerOutputter = { raw: function(out,tiddlers) { // The default is just to output the raw text of the tiddler, ignoring any metadata for(var t=0; t for(var t=0; t"); out.push("define(\"" + title + "\",function(require,exports) {"); out.push(tid.fields.text); out.push("});") out.push(""); } } }; // Cook an RSS file of the most recent 20 tiddlers Recipe.prototype.cookRss = function() { var me = this, numRssItems = 20, s = [], d = new Date(), u = this.store.renderTiddler("text/plain","SiteUrl"), encodeTiddlyLink = function(title) { return title.indexOf(" ") == -1 ? title : "[[" + title + "]]"; }, tiddlerToRssItem = function(tiddler,uri) { var s = "" + utils.htmlEncode(tiddler.fields.title) + "\n"; s += "" + utils.htmlEncode(me.store.renderTiddler("text/html",tiddler.fields.title)) + "\n"; var i; if(tiddler.fields.tags) { for(i=0; i\n"; } } s += "" + uri + "#" + encodeURIComponent(encodeTiddlyLink(tiddler.fields.title)) + "\n"; if(tiddler.fields.modified) { s +="" + tiddler.fields.modified.toUTCString() + "\n"; } return s; }, getRssTiddlers = function(sortField,excludeTag) { var r = []; me.store.forEachTiddler(sortField,excludeTag,function(title,tiddler) { if(!tiddler.hasTag(excludeTag)) { r.push(tiddler); } }); return r; }; // Assemble the header s.push("<" + "?xml version=\"1.0\"?" + ">"); s.push(""); s.push(""); s.push("" + utils.htmlEncode(this.store.renderTiddler("text/plain","SiteTitle")) + ""); if(u) s.push("" + utils.htmlEncode(u) + ""); s.push("" + utils.htmlEncode(this.store.renderTiddler("text/plain","SiteSubtitle")) + ""); //s.push("" + config.locale + ""); s.push("" + d.toUTCString() + ""); s.push("" + d.toUTCString() + ""); s.push("http://blogs.law.harvard.edu/tech/rss"); s.push("https://github.com/Jermolene/cook.js"); // The body var tiddlers = getRssTiddlers("modified","excludeLists"); var i,n = numRssItems > tiddlers.length ? 0 : tiddlers.length-numRssItems; for(i=tiddlers.length-1; i>=n; i--) { s.push("\n" + tiddlerToRssItem(tiddlers[i],u) + "\n"); } // And footer s.push(""); s.push(""); // Save it all return s.join("\n"); }; exports.Recipe = Recipe; })();