2012-01-03 11:07:09 +00:00
|
|
|
/*\
|
|
|
|
title: js/WikiTextCompiler.js
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var ArgParser = require("./ArgParser.js").ArgParser,
|
2012-01-04 18:31:19 +00:00
|
|
|
JavaScriptParseTree = require("./JavaScriptParseTree.js").JavaScriptParseTree,
|
2012-01-03 11:07:09 +00:00
|
|
|
utils = require("./Utils.js"),
|
|
|
|
util = require("util");
|
|
|
|
|
|
|
|
var WikiTextCompiler = function(store,title,parser) {
|
|
|
|
this.parser = parser;
|
|
|
|
this.store = store;
|
|
|
|
this.title = title;
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiTextCompiler.prototype.compile = function(type,treenode) {
|
|
|
|
if(type === "text/html") {
|
|
|
|
return this.compileAsHtml(treenode);
|
|
|
|
} else if(type === "text/plain") {
|
2012-01-04 11:58:08 +00:00
|
|
|
return this.compileAsText(treenode);
|
2012-01-03 11:07:09 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WikiTextCompiler.prototype.compileAsHtml = function(treenode) {
|
|
|
|
var me = this,
|
|
|
|
output = [],
|
|
|
|
compileSubTree;
|
|
|
|
var pushString = function(s) {
|
|
|
|
var last = output[output.length-1];
|
|
|
|
if(output.length > 0 && last.type === "StringLiterals") {
|
|
|
|
last.value.push(s);
|
|
|
|
} else if (output.length > 0 && last.type === "StringLiteral") {
|
|
|
|
last.type = "StringLiterals";
|
|
|
|
last.value = [last.value,s];
|
|
|
|
} else {
|
|
|
|
output.push({type: "StringLiteral", value: s});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var compileElement = 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) + "\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pushString("<" + tagBits.join(" ") + (options.selfClosing ? " /" : ""));
|
|
|
|
if(options.insertAfterAttributes) {
|
2012-01-03 12:42:34 +00:00
|
|
|
pushString(" ");
|
2012-01-03 11:07:09 +00:00
|
|
|
output.push(options.insertAfterAttributes);
|
|
|
|
}
|
|
|
|
pushString(">");
|
|
|
|
if(!options.selfClosing) {
|
|
|
|
if(element.children) {
|
|
|
|
compileSubTree(element.children);
|
|
|
|
}
|
|
|
|
pushString("</" + element.type + ">");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var compileMacroCall = function(name,params) {
|
|
|
|
var macro = me.store.macros[name];
|
|
|
|
if(macro) {
|
|
|
|
var args = new ArgParser(params,{defaultName: "anon"}),
|
|
|
|
paramsProps = {};
|
|
|
|
var insertParam = function(name,arg) {
|
|
|
|
if(arg.evaluated) {
|
2012-01-04 18:31:19 +00:00
|
|
|
var prog = me.store.jsParser.parse(arg.string);
|
|
|
|
paramsProps[name] = prog.tree.elements[0];
|
2012-01-03 11:07:09 +00:00
|
|
|
} else {
|
2012-01-03 11:38:15 +00:00
|
|
|
paramsProps[name] = {type: "StringLiteral", value: arg.string};
|
2012-01-03 11:07:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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: []
|
|
|
|
}]
|
|
|
|
};
|
2012-01-04 18:31:19 +00:00
|
|
|
macroCall.name.elements = macro.code["text/html"].tree.elements;
|
2012-01-03 11:07:09 +00:00
|
|
|
for(m in paramsProps) {
|
|
|
|
macroCall["arguments"][0].properties.push({
|
|
|
|
type: "PropertyAssignment",
|
|
|
|
name: m,
|
|
|
|
value: paramsProps[m]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
output.push(macroCall);
|
|
|
|
} else {
|
|
|
|
pushString("<span class='error errorUnknownMacro'>Unknown macro '" + name + "'</span>");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
compileSubTree = function(tree) {
|
|
|
|
for(var t=0; t<tree.length; t++) {
|
|
|
|
switch(tree[t].type) {
|
|
|
|
case "text":
|
|
|
|
pushString(utils.htmlEncode(tree[t].value));
|
|
|
|
break;
|
|
|
|
case "entity":
|
|
|
|
pushString(tree[t].value);
|
|
|
|
break;
|
|
|
|
case "br":
|
|
|
|
case "img":
|
|
|
|
compileElement(tree[t],{selfClosing: true}); // Self closing elements
|
|
|
|
break;
|
|
|
|
case "context":
|
|
|
|
//compileSubTree(tree[t].children);
|
|
|
|
break;
|
|
|
|
case "macro":
|
|
|
|
compileMacroCall(tree[t].name,tree[t].params);
|
|
|
|
break;
|
|
|
|
case "a":
|
|
|
|
compileElement(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:
|
|
|
|
compileElement(tree[t]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Compile the wiki parse tree into a javascript parse tree
|
|
|
|
compileSubTree(treenode);
|
|
|
|
// And then render the javascript parse tree back into JavaScript code
|
2012-01-04 18:31:19 +00:00
|
|
|
var parseTree = this.store.jsParser.createTree(
|
2012-01-03 11:07:09 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
type: "Function",
|
|
|
|
name: null,
|
|
|
|
params: ["tiddler","store","utils"],
|
|
|
|
elements: [
|
|
|
|
{
|
|
|
|
type: "ReturnStatement",
|
|
|
|
value: {
|
|
|
|
type: "FunctionCall",
|
|
|
|
name: {
|
|
|
|
type: "PropertyAccess",
|
|
|
|
base: {
|
|
|
|
type: "ArrayLiteral",
|
|
|
|
elements: output
|
|
|
|
},
|
|
|
|
name: "join"
|
|
|
|
},
|
|
|
|
"arguments": [ {
|
|
|
|
type: "StringLiteral",
|
|
|
|
value: ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2012-01-04 18:31:19 +00:00
|
|
|
]);
|
|
|
|
return parseTree.render();
|
2012-01-03 11:07:09 +00:00
|
|
|
};
|
|
|
|
|
2012-01-04 11:58:08 +00:00
|
|
|
WikiTextCompiler.prototype.compileAsText = function(treenode) {
|
|
|
|
var me = this,
|
|
|
|
output = [],
|
|
|
|
compileSubTree;
|
|
|
|
var pushString = function(s) {
|
|
|
|
var last = output[output.length-1];
|
|
|
|
if(output.length > 0 && last.type === "StringLiterals") {
|
|
|
|
last.value.push(s);
|
|
|
|
} else if (output.length > 0 && last.type === "StringLiteral") {
|
|
|
|
last.type = "StringLiterals";
|
|
|
|
last.value = [last.value,s];
|
|
|
|
} else {
|
|
|
|
output.push({type: "StringLiteral", value: s});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var compileElement = function(element, options) {
|
|
|
|
options = options || {};
|
|
|
|
if(!options.selfClosing) {
|
|
|
|
if(element.children) {
|
|
|
|
compileSubTree(element.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var compileMacroCall = function(name,params) {
|
|
|
|
var macro = me.store.macros[name];
|
|
|
|
if(macro) {
|
|
|
|
var args = new ArgParser(params,{defaultName: "anon"}),
|
|
|
|
paramsProps = {};
|
|
|
|
var insertParam = function(name,arg) {
|
|
|
|
if(arg.evaluated) {
|
2012-01-04 18:31:19 +00:00
|
|
|
var prog = me.store.jsParser.parse(arg.string);
|
|
|
|
paramsProps[name] = prog.tree.elements[0];
|
2012-01-04 11:58:08 +00:00
|
|
|
} 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: []
|
|
|
|
}]
|
|
|
|
};
|
2012-01-04 18:31:19 +00:00
|
|
|
macroCall.name.elements = macro.code["text/plain"].tree.elements;
|
2012-01-04 11:58:08 +00:00
|
|
|
for(m in paramsProps) {
|
|
|
|
macroCall["arguments"][0].properties.push({
|
|
|
|
type: "PropertyAssignment",
|
|
|
|
name: m,
|
|
|
|
value: paramsProps[m]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
output.push(macroCall);
|
|
|
|
} else {
|
|
|
|
pushString("{{** Unknown macro '" + name + "' **}}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
compileSubTree = function(tree) {
|
|
|
|
for(var t=0; t<tree.length; t++) {
|
|
|
|
switch(tree[t].type) {
|
|
|
|
case "text":
|
|
|
|
pushString(utils.htmlEncode(tree[t].value));
|
|
|
|
break;
|
|
|
|
case "entity":
|
|
|
|
var c = utils.entityDecode(tree[t].value);
|
|
|
|
if(c) {
|
|
|
|
pushString(c);
|
|
|
|
} else {
|
|
|
|
pushString(tree[t].value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "br":
|
|
|
|
case "img":
|
|
|
|
compileElement(tree[t],{selfClosing: true}); // Self closing elements
|
|
|
|
break;
|
|
|
|
case "context":
|
|
|
|
//compileSubTree(tree[t].children);
|
|
|
|
break;
|
|
|
|
case "macro":
|
|
|
|
compileMacroCall(tree[t].name,tree[t].params);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
compileElement(tree[t]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Compile the wiki parse tree into a javascript parse tree
|
|
|
|
compileSubTree(treenode);
|
|
|
|
// And then render the javascript parse tree back into JavaScript code
|
2012-01-04 18:31:19 +00:00
|
|
|
var parseTree = this.store.jsParser.createTree(
|
2012-01-04 11:58:08 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
type: "Function",
|
|
|
|
name: null,
|
|
|
|
params: ["tiddler","store","utils"],
|
|
|
|
elements: [
|
|
|
|
{
|
|
|
|
type: "ReturnStatement",
|
|
|
|
value: {
|
|
|
|
type: "FunctionCall",
|
|
|
|
name: {
|
|
|
|
type: "PropertyAccess",
|
|
|
|
base: {
|
|
|
|
type: "ArrayLiteral",
|
|
|
|
elements: output
|
|
|
|
},
|
|
|
|
name: "join"
|
|
|
|
},
|
|
|
|
"arguments": [ {
|
|
|
|
type: "StringLiteral",
|
|
|
|
value: ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2012-01-04 18:31:19 +00:00
|
|
|
]);
|
|
|
|
return parseTree.render();
|
2012-01-04 11:58:08 +00:00
|
|
|
};
|
|
|
|
|
2012-01-03 11:07:09 +00:00
|
|
|
exports.WikiTextCompiler = WikiTextCompiler;
|
|
|
|
|
|
|
|
})();
|