1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-07 04:24:22 +00:00

Refactored macro parsing

So that we can more easily improve the way that dependencies are tracked
This commit is contained in:
Jeremy Ruston 2012-02-06 17:16:45 +00:00
parent 9dfa0f4edd
commit 10774a724f

View File

@ -102,26 +102,36 @@ var enclosedTextHelper = function(w) {
} }
}; };
var parseMacroCall = function(w,name,paramString) { var insertMacroCall = function(w,output,name,params,children) {
var macro = w.store.macros[name], var macro = w.store.macros[name],
params = {},
dependencies = []; dependencies = [];
if(macro) { if(macro) {
if(macro.dependentAll) { if(macro.dependentAll) {
dependencies = null; dependencies = null;
} }
var args = new ArgParser(paramString,{defaultName: "anon"}), for(var m in macro.params) {
insertParam = function(param,name,arg) { var param = macro.params[m];
if(param.dependentAll) { if(m in params) {
dependencies = null;
}
if(param.type === "tiddler") { if(param.type === "tiddler") {
if(arg.evaluated) { if(params[m].type === "eval") {
dependencies = null; dependencies = null;
} else if(dependencies !== null) { } else if(dependencies !== null) {
dependencies.push(arg.string); dependencies.push(params[m].value);
} }
} }
}
}
w.addDependencies(dependencies);
output.push(HTML.macro(name,params,children,dependencies));
}
};
var parseMacroParams = function(w,name,paramString) {
var macro = w.store.macros[name],
params = {};
if(macro) {
var args = new ArgParser(paramString,{defaultName: "anon"}),
insertParam = function(param,name,arg) {
params[name] = {type: arg.evaluated ? "eval" : "string", value: arg.string}; params[name] = {type: arg.evaluated ? "eval" : "string", value: arg.string};
}; };
for(var m in macro.params) { for(var m in macro.params) {
@ -141,8 +151,7 @@ var parseMacroCall = function(w,name,paramString) {
} }
} }
} }
w.addDependencies(dependencies); return params;
return HTML.macro(name,params,null,dependencies);
}; };
var rules = [ var rules = [
@ -451,7 +460,8 @@ var rules = [
var lookaheadMatch = this.lookaheadRegExp.exec(w.source); var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart && lookaheadMatch[1]) { if(lookaheadMatch && lookaheadMatch.index == w.matchStart && lookaheadMatch[1]) {
w.nextMatch = this.lookaheadRegExp.lastIndex; w.nextMatch = this.lookaheadRegExp.lastIndex;
w.output.push(parseMacroCall(w,lookaheadMatch[1],lookaheadMatch[2])); var name = lookaheadMatch[1];
insertMacroCall(w,w.output,name,parseMacroParams(w,name,lookaheadMatch[2]));
} }
} }
}, },
@ -465,26 +475,15 @@ var rules = [
this.lookaheadRegExp.lastIndex = w.matchStart; this.lookaheadRegExp.lastIndex = w.matchStart;
var lookaheadMatch = this.lookaheadRegExp.exec(w.source); var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
var e = HTML.macro("link",{ var text = lookaheadMatch[1],
target: {type: "string", value: null} link = text;
},
[HTML.text("")],
[]),
text = lookaheadMatch[1];
if(lookaheadMatch[3]) { if(lookaheadMatch[3]) {
// Pretty bracketted link // Pretty bracketted link
var link = lookaheadMatch[3]; link = lookaheadMatch[3];
e.params.target.value = link;
e.dependencies.push(link);
w.addDependency(link);
} else {
// Simple bracketted link
e.params.target.value = text;
e.dependencies.push(text);
w.addDependency(text);
} }
e.children[0].value = text; insertMacroCall(w,w.output,"link",{
w.output.push(e); target: {type: "string", value: link}
},[HTML.text(text)]);
w.nextMatch = this.lookaheadRegExp.lastIndex; w.nextMatch = this.lookaheadRegExp.lastIndex;
} }
} }
@ -509,16 +508,9 @@ var rules = [
} }
} }
if(w.autoLinkWikiWords) { if(w.autoLinkWikiWords) {
var link = HTML.macro("link",{ insertMacroCall(w,w.output,"link",{
target: {type: "string", value: w.matchText} target: {type: "string", value: w.matchText}
}, },[HTML.text(w.source.substring(w.matchStart,w.nextMatch))]);
[
HTML.text(w.source.substring(w.matchStart,w.nextMatch))
],[
w.matchText
]);
w.addDependency(w.matchText);
w.output.push(link);
} else { } else {
w.outputText(w.output,w.matchStart,w.nextMatch); w.outputText(w.output,w.matchStart,w.nextMatch);
} }
@ -530,15 +522,9 @@ var rules = [
match: textPrimitives.urlPattern, match: textPrimitives.urlPattern,
handler: function(w) handler: function(w)
{ {
var e = HTML.macro("link",{ insertMacroCall(w,w.output,"link",{
target: {type: "string", value: w.matchText} target: {type: "string", value: w.matchText}
},[ },[HTML.text(w.source.substring(w.matchStart,w.nextMatch))]);
HTML.text(w.source.substring(w.matchStart,w.nextMatch))
],[
w.matchText
]);
w.addDependency(w.matchText);
w.output.push(e);
} }
}, },
@ -551,31 +537,29 @@ var rules = [
{ {
this.lookaheadRegExp.lastIndex = w.matchStart; this.lookaheadRegExp.lastIndex = w.matchStart;
var lookaheadMatch = this.lookaheadRegExp.exec(w.source), var lookaheadMatch = this.lookaheadRegExp.exec(w.source),
image = HTML.macro("image",{ imageParams = {
src: {type: "string", value: ""} src: {type: "string", value: ""}
},[],[]), },
link = HTML.macro("link",{ linkParams = {
target: {type: "string", value: ""} target: {type: "string", value: ""}
},[ };
image
],[]);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
if(lookaheadMatch[1]) { if(lookaheadMatch[1]) {
image.params.alignment = {type: "string", value: "left"}; imageParams.alignment = {type: "string", value: "left"};
} else if(lookaheadMatch[2]) { } else if(lookaheadMatch[2]) {
image.params.alignment = {type: "string", value: "right"}; imageParams.alignment = {type: "string", value: "right"};
} }
if(lookaheadMatch[3]) { if(lookaheadMatch[3]) {
image.params.text = {type: "string", value: lookaheadMatch[3]}; imageParams.text = {type: "string", value: lookaheadMatch[3]};
} }
image.params.src.value = lookaheadMatch[4]; imageParams.src.value = lookaheadMatch[4];
image.dependencies = [lookaheadMatch[4]];
if(lookaheadMatch[5]) { if(lookaheadMatch[5]) {
link.params.target.value = lookaheadMatch[5]; linkParams.target.value = lookaheadMatch[5];
link.dependencies = [lookaheadMatch[5]]; var linkChildren = [];
w.output.push(link); insertMacroCall(w,w.output,"link",linkParams,linkChildren);
insertMacroCall(w,linkChildren,"image",imageParams);
} else { } else {
w.output.push(image); insertMacroCall(w,w.output,"image",imageParams);
} }
w.nextMatch = this.lookaheadRegExp.lastIndex; w.nextMatch = this.lookaheadRegExp.lastIndex;
} }