mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-10-01 16:30:46 +00:00
Allow brackets to be omitted for function definitions with no parameters
This commit is contained in:
parent
c9bd1b5274
commit
99750d78b5
@ -27,7 +27,7 @@ Instantiate parse rule
|
|||||||
exports.init = function(parser) {
|
exports.init = function(parser) {
|
||||||
this.parser = parser;
|
this.parser = parser;
|
||||||
// Regexp to match
|
// Regexp to match
|
||||||
this.matchRegExp = /^\\function\s+([^(\s]+)\(\s*([^)]*)\)(\s*\r?\n)?/mg;
|
this.matchRegExp = /^\\function\s+([^(\s]+)(\(\s*([^)]*)\))?(\s*\r?\n)?/mg;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -37,9 +37,9 @@ exports.parse = function() {
|
|||||||
// Move past the macro name and parameters
|
// Move past the macro name and parameters
|
||||||
this.parser.pos = this.matchRegExp.lastIndex;
|
this.parser.pos = this.matchRegExp.lastIndex;
|
||||||
// Parse the parameters
|
// Parse the parameters
|
||||||
var paramString = this.match[2],
|
var paramString = this.match[3],
|
||||||
params = [];
|
params = [];
|
||||||
if(paramString !== "") {
|
if(this.match[2]) {
|
||||||
var reParam = /\s*([A-Za-z0-9\-_]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^"'\s]+)))?/mg,
|
var reParam = /\s*([A-Za-z0-9\-_]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^"'\s]+)))?/mg,
|
||||||
paramMatch = reParam.exec(paramString);
|
paramMatch = reParam.exec(paramString);
|
||||||
while(paramMatch) {
|
while(paramMatch) {
|
||||||
@ -56,7 +56,7 @@ exports.parse = function() {
|
|||||||
}
|
}
|
||||||
// Is this a multiline definition?
|
// Is this a multiline definition?
|
||||||
var reEnd;
|
var reEnd;
|
||||||
if(this.match[3]) {
|
if(this.match[4]) {
|
||||||
// If so, the end of the body is marked with \end
|
// If so, the end of the body is marked with \end
|
||||||
reEnd = /(\r?\n\\end[^\S\n\r]*(?:$|\r?\n))/mg;
|
reEnd = /(\r?\n\\end[^\S\n\r]*(?:$|\r?\n))/mg;
|
||||||
} else {
|
} else {
|
||||||
|
@ -119,7 +119,23 @@ describe("WikiText parser tests", function() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should parse function definitions", function() {
|
it("should parse function definitions with no parameters", function() {
|
||||||
|
expect(parse("\\function myMacro\nnothing\n\\end\n")).toEqual(
|
||||||
|
|
||||||
|
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ ], isFunctionDefinition : true } ]
|
||||||
|
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse single line function definitions with no parameters", function() {
|
||||||
|
expect(parse("\\function myMacro nothing\n")).toEqual(
|
||||||
|
|
||||||
|
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ ], isFunctionDefinition : true } ]
|
||||||
|
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse function definitions with parameters", function() {
|
||||||
expect(parse("\\function myMacro(one,two,three,four:elephant)\nnothing\n\\end\n")).toEqual(
|
expect(parse("\\function myMacro(one,two,three,four:elephant)\nnothing\n\\end\n")).toEqual(
|
||||||
|
|
||||||
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ { name: 'one' }, { name: 'two' }, { name: 'three' }, { name: 'four', default: 'elephant' } ], isFunctionDefinition : true } ]
|
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ { name: 'one' }, { name: 'two' }, { name: 'three' }, { name: 'four', default: 'elephant' } ], isFunctionDefinition : true } ]
|
||||||
|
Loading…
Reference in New Issue
Block a user