diff --git a/core/modules/parsers/wikiparser/rules/functiondef.js b/core/modules/parsers/wikiparser/rules/functiondef.js index 665b939f2..9e748ef48 100644 --- a/core/modules/parsers/wikiparser/rules/functiondef.js +++ b/core/modules/parsers/wikiparser/rules/functiondef.js @@ -27,7 +27,7 @@ Instantiate parse rule exports.init = function(parser) { this.parser = parser; // 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 this.parser.pos = this.matchRegExp.lastIndex; // Parse the parameters - var paramString = this.match[2], + var paramString = this.match[3], params = []; - if(paramString !== "") { + if(this.match[2]) { var reParam = /\s*([A-Za-z0-9\-_]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^"'\s]+)))?/mg, paramMatch = reParam.exec(paramString); while(paramMatch) { @@ -56,7 +56,7 @@ exports.parse = function() { } // Is this a multiline definition? var reEnd; - if(this.match[3]) { + if(this.match[4]) { // If so, the end of the body is marked with \end reEnd = /(\r?\n\\end[^\S\n\r]*(?:$|\r?\n))/mg; } else { diff --git a/editions/test/tiddlers/tests/test-wikitext-parser.js b/editions/test/tiddlers/tests/test-wikitext-parser.js index 7daf3e76f..6c3d15ac6 100644 --- a/editions/test/tiddlers/tests/test-wikitext-parser.js +++ b/editions/test/tiddlers/tests/test-wikitext-parser.js @@ -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( [ { 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 } ]