/*\ title: js/WikiTextRenderer.js \*/ (function(){ /*jslint node: true */ "use strict"; var ArgParser = require("./ArgParser.js").ArgParser, utils = require("./Utils.js"), util = require("util"); var WikiTextRenderer = function(store,title,parser) { this.parser = parser; this.store = store; this.title = title; }; WikiTextRenderer.prototype.render = function(type,treenode) { if(type === "text/html") { return this.renderAsHtml(treenode); } else if (type === "text/plain") { return this.renderAsText(treenode); } else { return null; } }; WikiTextRenderer.prototype.renderAsHtml = function(treenode) { var output = [], renderSubTree; var renderElement = function(element, selfClosing) { var tagBits = [element.type]; if(element.attributes) { for(var a in element.attributes) { var r = element.attributes[a]; if(a === "style") { var s = []; for(var t in r) { s.push(t + ":" + r[t] + ";"); } r = s.join(""); } tagBits.push(a + "=\"" + utils.htmlEncode(r) + "\""); } } output.push("<" + tagBits.join(" ") + (selfClosing ? " /" : "") + ">"); if(!selfClosing) { if(element.children) { renderSubTree(element.children); } output.push(""); } }; renderSubTree = function(tree) { for(var t=0; t>", filter = args.getValueByName("filter",null), tiddlers, lastGroup = "", ul, last = 0, t; limit = limit ? parseInt(limit,10) : null; template = template ? this.store.getTiddler(template) : null; if(template) { templateType = template.fields.type; templateText = template.fields.text; } groupTemplate = groupTemplate ? this.store.getTiddler(groupTemplate) : null; if(groupTemplate) { groupTemplateType = groupTemplate.fields.type; groupTemplateText = groupTemplate.fields.text; } if(filter) { // Filtering not implemented yet tiddlers = this.store.getTitles(field,"excludeLists"); } else { tiddlers = this.store.getTitles(field,"excludeLists"); } if(limit !== null) { last = tiddlers.length - Math.min(tiddlers.length,limit); } for(t=tiddlers.length-1; t>=last; t--) { var tiddler = tiddlers[t], theGroupParseTree = this.parser.processor.textProcessors.parse(groupTemplateType,groupTemplateText), theGroup = theGroupParseTree.render("text/plain",theGroupParseTree.children,this.store,tiddler); if(theGroup !== "") { if(ul === undefined || theGroup !== lastGroup) { ul = {type: "ul", attributes: {"class": "timeline"}, children: []}; macroNode.output.push(ul); ul.children.push({type: "li", attributes: {"class": "listTitle"}, children: [{type: "text", value: theGroup}]}); lastGroup = theGroup; } var item = { type: "li", attributes: { "class": "listLink"}, children: [ { type: "context", tiddler: tiddler, children: [] }]}; ul.children.push(item); item.children[0].children = this.parser.processor.textProcessors.parse(templateType,templateText).children; } } this.executeMacros(macroNode.output,title); } }, list: { argOptions: {defaultName:"type"}, handler: function(macroNode,args,title) { var type = args.getValueByName("type","all"), template = args.getValueByName("template",null), templateType = "text/x-tiddlywiki", templateText = "<>", emptyMessage = args.getValueByName("emptyMessage",null); // Get the template to use template = template ? this.store.getTiddler(template) : null; if(template) { templateType = template.fields.type; templateText = template.fields.text; } // Get the handler and the tiddlers var handler = WikiTextRenderer.macros.list.types[type]; handler = handler || WikiTextRenderer.macros.list.types.all; var tiddlers = handler.call(this); // Render them as a list var ul = {type: "ul", children: []}; for(var t=0; t 0) { macroNode.output.push(ul); this.executeMacros(macroNode.output,title); } else if (emptyMessage) { macroNode.output.push({type: "text", value: emptyMessage}); } }, types: { all: function() { return this.store.getTitles("title","excludeLists"); }, missing: function() { return this.store.getMissingTitles(); }, orphans: function() { return this.store.getOrphanTitles(); }, shadowed: function() { return this.store.getShadowTitles(); }, touched: function() { // Server syncing isn't implemented yet return []; }, filter: function() { // Filters aren't implemented yet return []; } } }, slider: { handler: function(macroNode,args,title) { } }, tabs: { handler: function(macroNode,args,title) { } }, tag: { handler: function(macroNode,args,title) { } }, tagging: { handler: function(macroNode,args,title) { } }, tags: { handler: function(macroNode,args,title) { } }, tiddler: { argOptions: {defaultName:"name",cascadeDefaults:true}, handler: function(macroNode,args,title) { var targetTitle = args.getValueByName("name",null), withTokens = args.getValuesByName("with",[]), tiddler = this.store.getTiddler(targetTitle), text = this.store.getTiddlerText(targetTitle,""), t; for(t=0; t