Switched to \end <name> instead of all those repeated backslashes

Thanks @kookma. See https://github.com/Jermolene/TiddlyWiki5/pull/7004#issuecomment-1286429236
This commit is contained in:
jeremy@jermolene.com 2022-10-22 09:00:59 +01:00
parent b3982efcd6
commit f23ae21260
3 changed files with 10 additions and 26 deletions

View File

@ -27,7 +27,7 @@ Instantiate parse rule
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /^(\\+)define\s+([^(\s]+)\(\s*([^)]*)\)(\s*\r?\n)?/mg;
this.matchRegExp = /^\\define\s+([^(\s]+)\(\s*([^)]*)\)(\s*\r?\n)?/mg;
};
/*
@ -37,7 +37,7 @@ exports.parse = function() {
// Move past the macro name and parameters
this.parser.pos = this.matchRegExp.lastIndex;
// Parse the parameters
var paramString = this.match[3],
var paramString = this.match[2],
params = [];
if(paramString !== "") {
var reParam = /\s*([A-Za-z0-9\-_]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|\[\[([^\]]*)\]\]|([^"'\s]+)))?/mg,
@ -56,9 +56,9 @@ exports.parse = function() {
}
// Is this a multiline definition?
var reEnd;
if(this.match[4]) {
if(this.match[3]) {
// If so, the end of the body is marked with \end
reEnd = new RegExp("(\\r?\\n" + $tw.utils.repeat("\\\\",this.match[1].length) + "end[^\\S\\n\\r]*(?:$|\\r?\\n))","mg");
reEnd = new RegExp("(\\r?\\n\\\\end[^\\S\\n\\r]*(?:" + $tw.utils.escapeRegExp(this.match[1]) + ")?(?:$|\\r?\\n))","mg");
} else {
// Otherwise, the end of the definition is marked by the end of the line
reEnd = /($|\r?\n)/mg;
@ -80,7 +80,7 @@ exports.parse = function() {
return [{
type: "set",
attributes: {
name: {type: "string", value: this.match[2]},
name: {type: "string", value: this.match[1]},
value: {type: "string", value: text}
},
children: [],

View File

@ -10,23 +10,23 @@ title: Output
\define outer()
\whitespace trim
\\define middle()
\define middle()
\whitespace trim
\\\\define inner()
\define inner()
\whitespace trim
Jaguar
\\\\end
\end inner
<<inner>>
\\end
\end middle
<<middle>>
\end
\end outer
<<outer>>

View File

@ -119,22 +119,6 @@ describe("WikiText parser tests", function() {
);
});
it("should parse macro definitions with two backslashes", function() {
expect(parse("\\\\define myMacro()\nnothing\n\\\\end\n")).toEqual(
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], params : [ ], isMacroDefinition : true } ]
);
});
it("should parse macro definitions with four backslashes", function() {
expect(parse("\\\\\\\\define myMacro()\nnothing\n\\\\\\\\end\n")).toEqual(
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], params : [ ], isMacroDefinition : true } ]
);
});
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(