1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +00:00

refactor: use isBlock in macrodef to know inline or block

This commit is contained in:
linonetwo 2024-10-14 16:43:50 +08:00
parent 5fdc9f365e
commit 87dc05be9d
2 changed files with 7 additions and 7 deletions

View File

@ -55,10 +55,10 @@ exports.parse = function() {
} }
} }
// Is the remainder of the \define line blank after the parameter close paren? // Is the remainder of the \define line blank after the parameter close paren?
var reEnd,inline = true; var reEnd,isBlock = true;
if(this.match[3]) { if(this.match[3]) {
// If so, it is a multiline definition and the end of the body is marked with \end // If so, it is a multiline definition and the end of the body is marked with \end
inline = false; isBlock = false;
reEnd = new RegExp("((?:^|\\r?\\n)[^\\S\\n\\r]*\\\\end[^\\S\\n\\r]*(?:" + $tw.utils.escapeRegExp(this.match[1]) + ")?(?:$|\\r?\\n))","mg"); reEnd = new RegExp("((?:^|\\r?\\n)[^\\S\\n\\r]*\\\\end[^\\S\\n\\r]*(?:" + $tw.utils.escapeRegExp(this.match[1]) + ")?(?:$|\\r?\\n))","mg");
} else { } else {
// Otherwise, the end of the definition is marked by the end of the line // Otherwise, the end of the definition is marked by the end of the line
@ -84,7 +84,7 @@ exports.parse = function() {
children: [], children: [],
params: params, params: params,
isMacroDefinition: true, isMacroDefinition: true,
inline: inline && !!endMatch isBlock: isBlock && !!endMatch
}]; }];
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"name",this.match[1]); $tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"name",this.match[1]);
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"value",text); $tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"value",text);
@ -97,7 +97,7 @@ exports.serialize = function(tree,serialize) {
return param.name + (param.default ? ":" + param.default : ""); return param.name + (param.default ? ":" + param.default : "");
}).join(","); }).join(",");
var definition = tree.attributes.value.value; var definition = tree.attributes.value.value;
if(tree.inline) { if(tree.isBlock) {
return "\\define " + name + "(" + params + ") " + definition + "\n\n" + serialize(tree.children); return "\\define " + name + "(" + params + ") " + definition + "\n\n" + serialize(tree.children);
} }
return "\\define " + name + "(" + params + ")\n" + definition + "\n\\end\n\n" + serialize(tree.children); return "\\define " + name + "(" + params + ")\n" + definition + "\n\\end\n\n" + serialize(tree.children);

View File

@ -114,12 +114,12 @@ describe("WikiText parser tests", function() {
it("should parse macro definitions", function() { it("should parse macro definitions", function() {
expect(parse("\\define myMacro()\nnothing\n\\end\n")).toEqual( expect(parse("\\define myMacro()\nnothing\n\\end\n")).toEqual(
[{"type":"set","attributes":{"name":{"name":"name","type":"string","value":"myMacro"},"value":{"name":"value","type":"string","value":"nothing"}},"children":[],"params":[],"isMacroDefinition":true,"inline":false,"orderedAttributes":[{"name":"name","type":"string","value":"myMacro"},{"name":"value","type":"string","value":"nothing"}],"start":0,"end":30,"rule":"macrodef"}] [{"type":"set","attributes":{"name":{"name":"name","type":"string","value":"myMacro"},"value":{"name":"value","type":"string","value":"nothing"}},"children":[],"params":[],"isMacroDefinition":true,"isBlock":false,"orderedAttributes":[{"name":"name","type":"string","value":"myMacro"},{"name":"value","type":"string","value":"nothing"}],"start":0,"end":30,"rule":"macrodef"}]
); );
expect(parse("\\define myMacro() nothing\n\n")).toEqual( expect(parse("\\define myMacro() nothing\n\n")).toEqual(
[{"type":"set","attributes":{"name":{"name":"name","type":"string","value":"myMacro"},"value":{"name":"value","type":"string","value":"nothing"}},"children":[],"params":[],"isMacroDefinition":true,"inline":true,"orderedAttributes":[{"name":"name","type":"string","value":"myMacro"},{"name":"value","type":"string","value":"nothing"}],"start":0,"end":25,"rule":"macrodef"}] [{"type":"set","attributes":{"name":{"name":"name","type":"string","value":"myMacro"},"value":{"name":"value","type":"string","value":"nothing"}},"children":[],"params":[],"isMacroDefinition":true,"isBlock":true,"orderedAttributes":[{"name":"name","type":"string","value":"myMacro"},{"name":"value","type":"string","value":"nothing"}],"start":0,"end":25,"rule":"macrodef"}]
); );
}); });
@ -190,7 +190,7 @@ describe("WikiText parser tests", function() {
it("should parse comment in pragma area. Comment will be invisible", function() { it("should parse comment in pragma area. Comment will be invisible", function() {
expect(parse("<!-- comment in pragma area -->\n\\define aMacro()\nnothing\n\\end\n")).toEqual( expect(parse("<!-- comment in pragma area -->\n\\define aMacro()\nnothing\n\\end\n")).toEqual(
[{"type":"void","children":[{"type":"set","attributes":{"name":{"name":"name","type":"string","value":"aMacro"},"value":{"name":"value","type":"string","value":"nothing"}},"children":[],"params":[],"isMacroDefinition":true,"inline":false,"orderedAttributes":[{"name":"name","type":"string","value":"aMacro"},{"name":"value","type":"string","value":"nothing"}],"start":32,"end":61,"rule":"macrodef"}],"text":"<!-- comment in pragma area -->","start":0,"end":31,"rule":"commentblock"}] [{"type":"void","children":[{"type":"set","attributes":{"name":{"name":"name","type":"string","value":"aMacro"},"value":{"name":"value","type":"string","value":"nothing"}},"children":[],"params":[],"isMacroDefinition":true,"isBlock":false,"orderedAttributes":[{"name":"name","type":"string","value":"aMacro"},{"name":"value","type":"string","value":"nothing"}],"start":32,"end":61,"rule":"macrodef"}],"text":"<!-- comment in pragma area -->","start":0,"end":31,"rule":"commentblock"}]
); );
}); });