mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-24 00:50:28 +00:00
Refactoring the wikitext parser
To match the structure of the JavaScript parser, and make it less complicated
This commit is contained in:
parent
b1b1b84b3e
commit
57e3143d69
@ -13,7 +13,7 @@ var WikiStore = require("./WikiStore.js").WikiStore,
|
||||
Tiddler = require("./Tiddler.js").Tiddler,
|
||||
tiddlerInput = require("./TiddlerInput.js"),
|
||||
tiddlerOutput = require("./TiddlerOutput.js"),
|
||||
WikiTextProcessor = require("./WikiTextProcessor.js").WikiTextProcessor,
|
||||
WikiTextParser = require("./WikiTextParser.js").WikiTextParser,
|
||||
JavaScriptParser = require("./JavaScriptParser.js").JavaScriptParser,
|
||||
Navigators = require("./Navigators.js").Navigators,
|
||||
StoryNavigator = require("./StoryNavigator.js").StoryNavigator;
|
||||
@ -24,8 +24,8 @@ var App = function() {
|
||||
this.isBrowser = typeof window !== "undefined";
|
||||
// Create the main store
|
||||
this.store = new WikiStore();
|
||||
// Register the wikitext processor
|
||||
this.store.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
|
||||
// Register the wikitext parser
|
||||
this.store.registerParser("text/x-tiddlywiki",new WikiTextParser({
|
||||
store: this.store
|
||||
}));
|
||||
// Register the standard tiddler serializers and deserializers
|
||||
|
@ -19,7 +19,7 @@ Available options are:
|
||||
var WikiStore = function WikiStore(options) {
|
||||
options = options || {};
|
||||
this.tiddlers = {};
|
||||
this.textProcessors = {};
|
||||
this.parsers = {};
|
||||
this.tiddlerSerializers = {};
|
||||
this.tiddlerDeserializers = {};
|
||||
this.sandbox = options.sandbox;
|
||||
@ -28,8 +28,8 @@ var WikiStore = function WikiStore(options) {
|
||||
});
|
||||
};
|
||||
|
||||
WikiStore.prototype.registerTextProcessor = function(type,processor) {
|
||||
this.textProcessors[type] = processor;
|
||||
WikiStore.prototype.registerParser = function(type,parser) {
|
||||
this.parsers[type] = parser;
|
||||
};
|
||||
|
||||
WikiStore.prototype.registerTiddlerSerializer = function(extension,mimeType,serializer) {
|
||||
@ -255,12 +255,12 @@ WikiStore.prototype.listTiddlers = function(type,template,emptyMessage) {
|
||||
*/
|
||||
|
||||
WikiStore.prototype.parseText = function(type,text) {
|
||||
var processor = this.textProcessors[type];
|
||||
if(!processor) {
|
||||
processor = this.textProcessors["text/x-tiddlywiki"];
|
||||
var parser = this.parsers[type];
|
||||
if(!parser) {
|
||||
parser = this.parsers["text/x-tiddlywiki"];
|
||||
}
|
||||
if(processor) {
|
||||
return processor.parse(text);
|
||||
if(parser) {
|
||||
return parser.parse(text);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
250
js/WikiTextParseTree.js
Normal file
250
js/WikiTextParseTree.js
Normal file
@ -0,0 +1,250 @@
|
||||
/*\
|
||||
title: js/WikiTextParseTree.js
|
||||
|
||||
Compile a wikitext parse tree into a JavaScript function that renders the required
|
||||
representation of the tree.
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true */
|
||||
"use strict";
|
||||
|
||||
var ArgParser = require("./ArgParser.js").ArgParser,
|
||||
JavaScriptParseTree = require("./JavaScriptParseTree.js").JavaScriptParseTree,
|
||||
utils = require("./Utils.js"),
|
||||
util = require("util");
|
||||
|
||||
// Intialise the parse tree object
|
||||
var WikiTextParseTree = function(tree,store) {
|
||||
this.tree = tree;
|
||||
this.store = store;
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.render = function(type,treenode,store,title) {
|
||||
/*jslint evil: true */
|
||||
var code = this.compile(type,treenode);
|
||||
var fn = eval(code);
|
||||
var tiddler = store.getTiddler(title);
|
||||
return fn(tiddler,store,utils);
|
||||
};
|
||||
|
||||
// Compile the parse tree into a JavaScript function that returns the required
|
||||
// representation of the tree
|
||||
WikiTextParseTree.prototype.compile = function(type,treenode) {
|
||||
treenode = treenode || this.tree;
|
||||
this.output = [];
|
||||
if(type === "text/html") {
|
||||
this.compileSubTreeHtml(treenode);
|
||||
} else if(type === "text/plain") {
|
||||
this.compileSubTreePlain(treenode);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
// And then wrap the javascript tree and render it back into JavaScript code
|
||||
var parseTree = this.store.jsParser.createTree(
|
||||
[
|
||||
{
|
||||
type: "Function",
|
||||
name: null,
|
||||
params: ["tiddler","store","utils"],
|
||||
elements: [
|
||||
{
|
||||
type: "ReturnStatement",
|
||||
value: {
|
||||
type: "FunctionCall",
|
||||
name: {
|
||||
type: "PropertyAccess",
|
||||
base: {
|
||||
type: "ArrayLiteral",
|
||||
elements: this.output
|
||||
},
|
||||
name: "join"
|
||||
},
|
||||
"arguments": [ {
|
||||
type: "StringLiteral",
|
||||
value: ""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
return parseTree.render();
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.pushString = function(s) {
|
||||
var last = this.output[this.output.length-1];
|
||||
if(this.output.length > 0 && last.type === "StringLiterals") {
|
||||
last.value.push(s);
|
||||
} else if (this.output.length > 0 && last.type === "StringLiteral") {
|
||||
last.type = "StringLiterals";
|
||||
last.value = [last.value,s];
|
||||
} else {
|
||||
this.output.push({type: "StringLiteral", value: s});
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
|
||||
var me = this,
|
||||
macro = this.store.macros[name];
|
||||
if(macro) {
|
||||
var args = new ArgParser(params,{defaultName: "anon"}),
|
||||
paramsProps = {};
|
||||
var insertParam = function(name,arg) {
|
||||
if(arg.evaluated) {
|
||||
paramsProps[name] = me.store.jsParser.parse(arg.string).tree.elements[0];
|
||||
} else {
|
||||
paramsProps[name] = {type: "StringLiteral", value: arg.string};
|
||||
}
|
||||
};
|
||||
for(var m in macro.params) {
|
||||
var param = macro.params[m];
|
||||
if("byPos" in param && args.byPos[param.byPos]) {
|
||||
insertParam(m,args.byPos[param.byPos].v);
|
||||
} else if("byName" in param) {
|
||||
var arg = args.getValueByName(m);
|
||||
if(!arg && param.byName === "default") {
|
||||
arg = args.getValueByName("anon");
|
||||
}
|
||||
if(arg) {
|
||||
insertParam(m,arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
var macroCall = {
|
||||
type: "FunctionCall",
|
||||
name: {
|
||||
type: "Function",
|
||||
name: null,
|
||||
params: ["params"],
|
||||
elements: []},
|
||||
"arguments": [ {
|
||||
type: "ObjectLiteral",
|
||||
properties: []
|
||||
}]
|
||||
};
|
||||
macroCall.name.elements = macro.code[type].tree.elements;
|
||||
for(m in paramsProps) {
|
||||
macroCall["arguments"][0].properties.push({
|
||||
type: "PropertyAssignment",
|
||||
name: m,
|
||||
value: paramsProps[m]
|
||||
});
|
||||
}
|
||||
this.output.push(macroCall);
|
||||
} else {
|
||||
this.pushString("<span class='error errorUnknownMacro'>Unknown macro '" + name + "'</span>");
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.compileElementHtml = function(element, options) {
|
||||
options = options || {};
|
||||
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) + "\"");
|
||||
}
|
||||
}
|
||||
this.pushString("<" + tagBits.join(" ") + (options.selfClosing ? " /" : ""));
|
||||
if(options.insertAfterAttributes) {
|
||||
this.pushString(" ");
|
||||
this.output.push(options.insertAfterAttributes);
|
||||
}
|
||||
this.pushString(">");
|
||||
if(!options.selfClosing) {
|
||||
if(element.children) {
|
||||
this.compileSubTreeHtml(element.children);
|
||||
}
|
||||
this.pushString("</" + element.type + ">");
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.compileSubTreeHtml = function(tree) {
|
||||
for(var t=0; t<tree.length; t++) {
|
||||
switch(tree[t].type) {
|
||||
case "text":
|
||||
this.pushString(utils.htmlEncode(tree[t].value));
|
||||
break;
|
||||
case "entity":
|
||||
this.pushString(tree[t].value);
|
||||
break;
|
||||
case "br":
|
||||
case "img":
|
||||
this.compileElementHtml(tree[t],{selfClosing: true}); // Self closing elements
|
||||
break;
|
||||
case "context":
|
||||
//compileSubTree(tree[t].children);
|
||||
break;
|
||||
case "macro":
|
||||
this.compileMacroCall("text/html",tree[t].name,tree[t].params);
|
||||
break;
|
||||
case "a":
|
||||
this.compileElementHtml(tree[t],{
|
||||
insertAfterAttributes: {
|
||||
"type": "FunctionCall",
|
||||
"name": {
|
||||
"type": "PropertyAccess",
|
||||
"base": {
|
||||
"type": "Variable",
|
||||
"name": "store"},
|
||||
"name": "classesForLink"},
|
||||
"arguments":[{
|
||||
"type": "StringLiteral",
|
||||
"value": tree[t].attributes.href}]}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
this.compileElementHtml(tree[t]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.compileElementPlain = function(element, options) {
|
||||
options = options || {};
|
||||
if(!options.selfClosing) {
|
||||
if(element.children) {
|
||||
this.compileSubTreePlain(element.children);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextParseTree.prototype.compileSubTreePlain = function(tree) {
|
||||
for(var t=0; t<tree.length; t++) {
|
||||
switch(tree[t].type) {
|
||||
case "text":
|
||||
this.pushString(utils.htmlEncode(tree[t].value));
|
||||
break;
|
||||
case "entity":
|
||||
this.pushString(tree[t].value);
|
||||
break;
|
||||
case "br":
|
||||
case "img":
|
||||
this.compileElementPlain(tree[t],{selfClosing: true}); // Self closing elements
|
||||
break;
|
||||
case "context":
|
||||
//compileSubTree(tree[t].children);
|
||||
break;
|
||||
case "macro":
|
||||
this.compileMacroCall("text/plain",tree[t].name,tree[t].params);
|
||||
break;
|
||||
default:
|
||||
this.compileElementPlain(tree[t]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.WikiTextParseTree = WikiTextParseTree;
|
||||
|
||||
})();
|
@ -9,18 +9,44 @@ Parses a block of tiddlywiki-format wiki text into a parse tree object.
|
||||
/*jslint node: true */
|
||||
"use strict";
|
||||
|
||||
var WikiTextCompiler = require("./WikiTextCompiler.js").WikiTextCompiler,
|
||||
var WikiTextRules = require("./WikiTextRules.js"),
|
||||
WikiTextParseTree = require("./WikiTextParseTree.js").WikiTextParseTree,
|
||||
utils = require("./Utils.js"),
|
||||
util = require("util");
|
||||
|
||||
var WikiTextParser = function(text,processor) {
|
||||
this.processor = processor;
|
||||
/*
|
||||
Creates a new instance of the wiki text parser with the specified options. The
|
||||
options are a hashmap of mandatory members as follows:
|
||||
|
||||
store: The store object to use to parse any cascaded content (eg transclusion)
|
||||
|
||||
Planned:
|
||||
|
||||
enableRules: An array of names of wiki text rules to enable. If not specified, all rules are available
|
||||
extraRules: An array of additional rule handlers to add
|
||||
enableMacros: An array of names of macros to enable. If not specified, all macros are available
|
||||
extraMacros: An array of additional macro handlers to add
|
||||
*/
|
||||
|
||||
var WikiTextParser = function(options) {
|
||||
this.store = options.store;
|
||||
this.autoLinkWikiWords = true;
|
||||
this.rules = WikiTextRules.rules;
|
||||
var pattern = [];
|
||||
for(var n=0; n<this.rules.length; n++) {
|
||||
pattern.push("(" + this.rules[n].match + ")");
|
||||
}
|
||||
this.rulesRegExp = new RegExp(pattern.join("|"),"mg");
|
||||
|
||||
};
|
||||
|
||||
WikiTextParser.prototype.parse = function(text) {
|
||||
this.source = text;
|
||||
this.nextMatch = 0;
|
||||
this.children = [];
|
||||
this.output = null;
|
||||
this.subWikify(this.children);
|
||||
return new WikiTextParseTree(this.children,this.store);
|
||||
};
|
||||
|
||||
WikiTextParser.prototype.outputText = function(place,startPos,endPos) {
|
||||
@ -42,8 +68,8 @@ WikiTextParser.prototype.subWikifyUnterm = function(output) {
|
||||
var oldOutput = this.output;
|
||||
this.output = output;
|
||||
// Get the first match
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.processor.rulesRegExp.exec(this.source);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.rulesRegExp.exec(this.source);
|
||||
while(ruleMatch) {
|
||||
// Output any text before the match
|
||||
if(ruleMatch.index > this.nextMatch)
|
||||
@ -52,18 +78,18 @@ WikiTextParser.prototype.subWikifyUnterm = function(output) {
|
||||
this.matchStart = ruleMatch.index;
|
||||
this.matchLength = ruleMatch[0].length;
|
||||
this.matchText = ruleMatch[0];
|
||||
this.nextMatch = this.processor.rulesRegExp.lastIndex;
|
||||
this.nextMatch = this.rulesRegExp.lastIndex;
|
||||
// Figure out which rule matched and call its handler
|
||||
var t;
|
||||
for(t=1; t<ruleMatch.length; t++) {
|
||||
if(ruleMatch[t]) {
|
||||
this.processor.rules[t-1].handler(this);
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
this.rules[t-1].handler(this);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Get the next match
|
||||
ruleMatch = this.processor.rulesRegExp.exec(this.source);
|
||||
ruleMatch = this.rulesRegExp.exec(this.source);
|
||||
}
|
||||
// Output any text after the last match
|
||||
if(this.nextMatch < this.source.length) {
|
||||
@ -81,8 +107,8 @@ WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
||||
// Get the first matches for the rule and terminator RegExps
|
||||
terminatorRegExp.lastIndex = this.nextMatch;
|
||||
var terminatorMatch = terminatorRegExp.exec(this.source);
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.processor.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
var ruleMatch = this.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
while(terminatorMatch || ruleMatch) {
|
||||
// Check for a terminator match before the next rule match
|
||||
if(terminatorMatch && (!ruleMatch || terminatorMatch.index <= ruleMatch.index)) {
|
||||
@ -105,20 +131,20 @@ WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
||||
this.matchStart = ruleMatch.index;
|
||||
this.matchLength = ruleMatch[0].length;
|
||||
this.matchText = ruleMatch[0];
|
||||
this.nextMatch = this.processor.rulesRegExp.lastIndex;
|
||||
this.nextMatch = this.rulesRegExp.lastIndex;
|
||||
// Figure out which rule matched and call its handler
|
||||
var t;
|
||||
for(t=1; t<ruleMatch.length; t++) {
|
||||
if(ruleMatch[t]) {
|
||||
this.processor.rules[t-1].handler(this);
|
||||
this.processor.rulesRegExp.lastIndex = this.nextMatch;
|
||||
this.rules[t-1].handler(this);
|
||||
this.rulesRegExp.lastIndex = this.nextMatch;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Get the next match
|
||||
terminatorRegExp.lastIndex = this.nextMatch;
|
||||
terminatorMatch = terminatorRegExp.exec(this.source);
|
||||
ruleMatch = this.processor.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
ruleMatch = this.rulesRegExp.exec(terminatorMatch ? this.source.substr(0,terminatorMatch.index) : this.source);
|
||||
}
|
||||
// Output any text after the last match
|
||||
if(this.nextMatch < this.source.length) {
|
||||
@ -129,15 +155,6 @@ WikiTextParser.prototype.subWikifyTerm = function(output,terminatorRegExp) {
|
||||
this.output = oldOutput;
|
||||
};
|
||||
|
||||
WikiTextParser.prototype.render = function(type,treenode,store,title) {
|
||||
/*jslint evil: true */
|
||||
var compiler = new WikiTextCompiler(store,title,this);
|
||||
var code = compiler.compile(type,treenode);
|
||||
var fn = eval(code);
|
||||
var tiddler = store.getTiddler(title);
|
||||
return fn(tiddler,store,utils);
|
||||
};
|
||||
|
||||
exports.WikiTextParser = WikiTextParser;
|
||||
|
||||
})();
|
||||
|
@ -1,43 +0,0 @@
|
||||
/*\
|
||||
title: js/WikiTextProcessor.js
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true */
|
||||
"use strict";
|
||||
|
||||
var WikiTextRules = require("./WikiTextRules.js"),
|
||||
WikiTextParser = require("./WikiTextParser.js").WikiTextParser;
|
||||
|
||||
/*
|
||||
Creates a new instance of the wiki text processor with the specified options. The
|
||||
options are a hashmap of mandatory members as follows:
|
||||
|
||||
store: The store object to use to parse any cascaded content (eg transclusion)
|
||||
|
||||
Planned:
|
||||
|
||||
enableRules: An array of names of wiki text rules to enable. If not specified, all rules are available
|
||||
extraRules: An array of additional rule handlers to add
|
||||
enableMacros: An array of names of macros to enable. If not specified, all macros are available
|
||||
extraMacros: An array of additional macro handlers to add
|
||||
*/
|
||||
var WikiTextProcessor = function(options) {
|
||||
options = options || {};
|
||||
this.store = options.store;
|
||||
this.rules = WikiTextRules.rules;
|
||||
var pattern = [];
|
||||
for(var n=0; n<this.rules.length; n++) {
|
||||
pattern.push("(" + this.rules[n].match + ")");
|
||||
}
|
||||
this.rulesRegExp = new RegExp(pattern.join("|"),"mg");
|
||||
};
|
||||
|
||||
WikiTextProcessor.prototype.parse = function(text) {
|
||||
return new WikiTextParser(text,this);
|
||||
};
|
||||
|
||||
exports.WikiTextProcessor = WikiTextProcessor;
|
||||
|
||||
})();
|
@ -1,387 +0,0 @@
|
||||
/*\
|
||||
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("</" + element.type + ">");
|
||||
}
|
||||
};
|
||||
renderSubTree = function(tree) {
|
||||
for(var t=0; t<tree.length; t++) {
|
||||
switch(tree[t].type) {
|
||||
case "text":
|
||||
output.push(utils.htmlEncode(tree[t].value));
|
||||
break;
|
||||
case "entity":
|
||||
output.push(tree[t].value);
|
||||
break;
|
||||
case "br":
|
||||
case "img":
|
||||
renderElement(tree[t],true); // Self closing elements
|
||||
break;
|
||||
case "context":
|
||||
renderSubTree(tree[t].children);
|
||||
break;
|
||||
case "macro":
|
||||
renderSubTree(tree[t].output);
|
||||
break;
|
||||
default:
|
||||
renderElement(tree[t]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
this.executeMacros(treenode,this.title);
|
||||
renderSubTree(treenode);
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
WikiTextRenderer.prototype.renderAsText = function(treenode) {
|
||||
var output = [];
|
||||
var renderSubTree = function(tree) {
|
||||
for(var t=0; t<tree.length; t++) {
|
||||
switch(tree[t].type) {
|
||||
case "text":
|
||||
output.push(tree[t].value);
|
||||
break;
|
||||
case "entity":
|
||||
var c = utils.entityDecode(tree[t].value);
|
||||
if(c) {
|
||||
output.push(c);
|
||||
} else {
|
||||
output.push(tree[t].value);
|
||||
}
|
||||
break;
|
||||
case "macro":
|
||||
renderSubTree(tree[t].output);
|
||||
break;
|
||||
}
|
||||
if(tree[t].children) {
|
||||
renderSubTree(tree[t].children);
|
||||
}
|
||||
}
|
||||
};
|
||||
this.executeMacros(treenode,this.title);
|
||||
renderSubTree(treenode);
|
||||
return output.join("");
|
||||
};
|
||||
|
||||
WikiTextRenderer.prototype.executeMacros = function(tree,title) {
|
||||
for(var t=0; t<tree.length; t++) {
|
||||
if(tree[t].type === "macro") {
|
||||
this.executeMacro(tree[t],title);
|
||||
}
|
||||
if(tree[t].children) {
|
||||
var contextForChildren = tree[t].type === "context" ? tree[t].tiddler : title;
|
||||
this.executeMacros(tree[t].children,contextForChildren);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextRenderer.prototype.executeMacro = function(macroNode,title) {
|
||||
var macroInfo = WikiTextRenderer.macros[macroNode.name];
|
||||
macroNode.output = [];
|
||||
if(macroInfo) {
|
||||
var args;
|
||||
if(macroInfo.argOptions) {
|
||||
var argOptions = {
|
||||
globals: {
|
||||
title: title
|
||||
},
|
||||
sandbox: this.store.jsProcessor
|
||||
};
|
||||
for(var g in macroInfo.argOptions) {
|
||||
argOptions[g] = macroInfo.argOptions[g];
|
||||
}
|
||||
args = new ArgParser(macroNode.params,argOptions);
|
||||
}
|
||||
macroInfo.handler.call(this,macroNode,args,title);
|
||||
} else {
|
||||
macroNode.output.push({type: "text", value: "Unknown macro " + macroNode.name});
|
||||
}
|
||||
};
|
||||
|
||||
WikiTextRenderer.versionTiddlyWiki = "2.6.5";
|
||||
|
||||
WikiTextRenderer.macros = {
|
||||
allTags: {
|
||||
handler: function(macroNode,args,title) {
|
||||
}
|
||||
},
|
||||
br: {
|
||||
handler: function(macroNode,args,title) {
|
||||
macroNode.output.push({type: "br"});
|
||||
}
|
||||
},
|
||||
echo: {
|
||||
argOptions: {defaultName: "anon"},
|
||||
handler: function(macroNode,args,title) {
|
||||
var globals = {title: title};
|
||||
macroNode.output.push({type: "text", value: args.byPos[0].v});
|
||||
}
|
||||
},
|
||||
timeline: {
|
||||
argOptions: {defaultName:"anon"},
|
||||
handler: function(macroNode,args,title) {
|
||||
var anonByPos = args.getValuesByName("anon",[]),
|
||||
field = anonByPos[0] || "modified",
|
||||
limit = anonByPos[1] || null,
|
||||
dateformat = anonByPos[2] || "DD MMM YYYY",
|
||||
template = args.getValueByName("template",null),
|
||||
templateType = "text/x-tiddlywiki",
|
||||
templateText = "<<view title link>>",
|
||||
groupTemplate = args.getValueByName("groupTemplate",null),
|
||||
groupTemplateType = "text/x-tiddlywiki",
|
||||
groupTemplateText = "<<view " + field + " date '" + dateformat + "'>>",
|
||||
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.store.parseText(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.store.parseText(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 = "<<view title link>>",
|
||||
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<tiddlers.length; t++) {
|
||||
var li = {
|
||||
type: "li",
|
||||
children: [ {
|
||||
type: "context",
|
||||
tiddler: tiddlers[t],
|
||||
children: []
|
||||
} ]
|
||||
};
|
||||
li.children[0].children = this.store.parseText(templateType,templateText).children;
|
||||
ul.children.push(li);
|
||||
}
|
||||
if(ul.children.length > 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<withTokens.length; t++) {
|
||||
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
|
||||
text = text.replace(placeholderRegExp,withTokens[t]);
|
||||
}
|
||||
macroNode.output = this.store.parseText(tiddler.fields.type,text).children;
|
||||
// Execute any macros in the copy
|
||||
this.executeMacros(macroNode.output,title);
|
||||
}
|
||||
},
|
||||
today: {
|
||||
argOptions: {noNames:true},
|
||||
handler: function(macroNode,args,title) {
|
||||
var now = new Date(),
|
||||
value = args.byPos[0] ? utils.formatDateString(now,args.byPos[0].v) : now.toLocaleString();
|
||||
macroNode.output.push({type: "text", value: value});
|
||||
}
|
||||
},
|
||||
version: {
|
||||
handler: function(macroNode,args,title) {
|
||||
macroNode.output.push({type: "text", value: WikiTextRenderer.versionTiddlyWiki});
|
||||
}
|
||||
},
|
||||
view: {
|
||||
argOptions: {noNames:true},
|
||||
handler: function(macroNode,args,title) {
|
||||
var field = args.byPos[0] ? args.byPos[0].v : null,
|
||||
format = args.byPos[1] ? args.byPos[1].v : "text",
|
||||
tiddler = this.store.getTiddler(title),
|
||||
value = tiddler ? tiddler.fields[field] : null;
|
||||
if(tiddler && field && value) {
|
||||
switch(format) {
|
||||
case "text":
|
||||
macroNode.output.push({type: "text", value: value});
|
||||
break;
|
||||
case "link":
|
||||
macroNode.output.push({
|
||||
type: "a",
|
||||
attributes: {
|
||||
href: value
|
||||
},
|
||||
children: [
|
||||
{type: "text", value: value}
|
||||
]
|
||||
});
|
||||
break;
|
||||
case "wikified":
|
||||
macroNode.output = this.store.parseText("text/x-tiddlywiki",value).children;
|
||||
// Execute any macros in the copy
|
||||
this.executeMacros(macroNode.output,title);
|
||||
break;
|
||||
case "date":
|
||||
var template = args.byPos[2] ? args.byPos[2].v : "DD MMM YYYY";
|
||||
macroNode.output.push({type: "text", value: utils.formatDateString(value,template)});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.WikiTextRenderer = WikiTextRenderer;
|
||||
|
||||
})();
|
@ -13,7 +13,6 @@ var App = require("./js/App.js").App,
|
||||
Recipe = require("./js/Recipe.js").Recipe,
|
||||
tiddlerInput = require("./js/TiddlerInput.js"),
|
||||
tiddlerOutput = require("./js/TiddlerOutput.js"),
|
||||
WikiTextProcessor = require("./js/WikiTextProcessor.js").WikiTextProcessor,
|
||||
util = require("util"),
|
||||
fs = require("fs"),
|
||||
url = require("url"),
|
||||
|
@ -16,11 +16,9 @@ jsmodule: ../js/Tiddler.js
|
||||
jsmodule: ../js/TiddlerInput.js
|
||||
jsmodule: ../js/TiddlerOutput.js
|
||||
jsmodule: ../js/WikiStore.js
|
||||
jsmodule: ../js/WikiTextProcessor.js
|
||||
jsmodule: ../js/WikiTextParser.js
|
||||
jsmodule: ../js/WikiTextRules.js
|
||||
jsmodule: ../js/WikiTextRenderer.js
|
||||
jsmodule: ../js/WikiTextCompiler.js
|
||||
jsmodule: ../js/WikiTextParseTree.js
|
||||
jsmodule: ../js/Navigators.js
|
||||
jsmodule: ../js/StoryNavigator.js
|
||||
jsmodule: ../js/App.js
|
||||
|
Loading…
Reference in New Issue
Block a user