diff --git a/core/modules/parsers/parseutils.js b/core/modules/parsers/parseutils.js index 4a25259c8..6a0902c6f 100644 --- a/core/modules/parsers/parseutils.js +++ b/core/modules/parsers/parseutils.js @@ -123,6 +123,36 @@ exports.parseStringLiteral = function(source,pos) { } }; +/* +Returns an array of {name:} with an optional "default" property. Options include: +requireParenthesis: require the parameter definition to be wrapped in parenthesis +*/ +exports.parseParameterDefinition = function(paramString,options) { + options = options || {}; + if(options.requireParenthesis) { + var parenMatch = /^\s*\((.*)\)\s*$/g.exec(paramString); + if(!parenMatch) { + return []; + } + paramString = parenMatch[1]; + } + var params = [], + reParam = /\s*([^:),\s]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^,"'\s]+)))?/mg, + paramMatch = reParam.exec(paramString); + while(paramMatch) { + // Save the parameter details + var paramInfo = {name: paramMatch[1]}, + defaultValue = paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5]; + if(defaultValue !== undefined) { + paramInfo["default"] = defaultValue; + } + params.push(paramInfo); + // Look for the next parameter + paramMatch = reParam.exec(paramString); + } + return params; +}; + exports.parseMacroParameters = function(node,source,pos) { // Process parameters var parameter = $tw.utils.parseMacroParameter(source,pos); diff --git a/core/modules/parsers/wikiparser/rules/fnprocdef.js b/core/modules/parsers/wikiparser/rules/fnprocdef.js index 940567eff..15443bc27 100644 --- a/core/modules/parsers/wikiparser/rules/fnprocdef.js +++ b/core/modules/parsers/wikiparser/rules/fnprocdef.js @@ -45,22 +45,9 @@ exports.parse = function() { // Move past the macro name and parameters this.parser.pos = this.matchRegExp.lastIndex; // Parse the parameters - var paramString = this.match[4], - params = []; + var params = []; if(this.match[3]) { - var reParam = /\s*([^:),\s]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^,"'\s]+)))?/mg, - paramMatch = reParam.exec(paramString); - while(paramMatch) { - // Save the parameter details - var paramInfo = {name: paramMatch[1]}, - defaultValue = paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5]; - if(defaultValue !== undefined) { - paramInfo["default"] = defaultValue; - } - params.push(paramInfo); - // Look for the next parameter - paramMatch = reParam.exec(paramString); - } + params = $tw.utils.parseParameterDefinition(this.match[4]); } // Is this a multiline definition? var reEnd; diff --git a/core/modules/parsers/wikiparser/rules/parameters.js b/core/modules/parsers/wikiparser/rules/parameters.js index 86b6074d7..745d7b7dd 100644 --- a/core/modules/parsers/wikiparser/rules/parameters.js +++ b/core/modules/parsers/wikiparser/rules/parameters.js @@ -36,20 +36,14 @@ exports.parse = function() { // Move past the macro name and parameters this.parser.pos = this.matchRegExp.lastIndex; // Parse the parameters - var paramString = this.match[1], - attributes = Object.create(null), - orderedAttributes = [], - reParam = /\s*([^:),\s]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^,"'\s]+)))?/mg, - paramMatch = reParam.exec(paramString); - while(paramMatch) { - // Save the parameter details - var name = paramMatch[1], - attribute = {name: name, type: "string", value: paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5]}; - attributes[name] = attribute; + var params = $tw.utils.parseParameterDefinition(this.match[1]); + var attributes = Object.create(null), + orderedAttributes = []; + $tw.utils.each(params,function(param) { + var attribute = {name: param.name, type: "string", value: param["default"] || ""}; + attributes[param.name] = attribute; orderedAttributes.push(attribute); - // Look for the next parameter - paramMatch = reParam.exec(paramString); - } + }); // Save the macro definition return [{ type: "parameters",