forked from apidoc/apidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change RegEx for apiParam. Add size and allowedValues. Add test cases…
… for apiParam.
- Loading branch information
Showing
1 changed file
with
122 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,136 @@ | ||
var group = ""; | ||
|
||
// Search: group, type, optional, fieldname, defaultValue, size, description | ||
// Example: {String{1..4}} [user.name="John Doe"] Users fullname. | ||
// | ||
// Naming convention: | ||
// b -> begin | ||
// e -> end | ||
// name -> the field value | ||
// oName -> optional field | ||
// wName -> wrapper for field | ||
|
||
var regExp = { | ||
b: "^", // start | ||
|
||
oGroup: { // optional group: (404) | ||
b: "\\s*(?:\\(\\s*", // starting with '(', optional surrounding spaces | ||
group: "(.+?)", // 1 | ||
e: "\\s*\\)\\s*)?" // ending with ')', optional surrounding spaces | ||
}, | ||
|
||
oType: { // optional type: {string} | ||
b: "\\s*(?:\\{\\s*", // starting with '{', optional surrounding spaces | ||
type: "([a-zA-Z0-9\.\/\\\\\\[\\]_-]+)", // 2 | ||
oSize: { // optional size within type: {string{1..4}} | ||
b: "\\s*(?:\\{\\s*", // starting with '{', optional surrounding spaces | ||
size: "(.+?)", // 3 | ||
e: "\\s*\\}\\s*)?" // ending with '}', optional surrounding spaces | ||
}, | ||
oAllowedValues: { // optional allowed values within type: {string="abc","def"} | ||
b: "\\s*(?:=\\s*", // starting with '=', optional surrounding spaces | ||
possibleValues: "(.+?)", // 4 | ||
e: "(?=\\s*\\}\\s*))?" // ending with '}', optional surrounding spaces | ||
}, | ||
e: "\\s*\\}\\s*)?" // ending with '}', optional surrounding spaces | ||
}, | ||
|
||
wName: { | ||
b: "(\\[?\\s*", // 5 optional optional-marker | ||
name: "([a-zA-Z0-9\.\/\\\\\\[\\]_-]+)", // 6 | ||
oDefaultValue: { // optional defaultValue | ||
b: "(?:\\s*=\\s*(?:", // starting with '=', optional surrounding spaces | ||
withDoubleQuote: "\"([^\"]*)\"", // 7 | ||
withQuote: "|'([^']*)'", // 8 | ||
withoutQuote: "|(.*?)(?:\\s|\\]|$)", // 9 | ||
e: "))?" | ||
}, | ||
e: "\\s*\\]?\\s*)" | ||
}, | ||
|
||
description: "(.*)?", // 10 | ||
e: "$|@" | ||
}; | ||
|
||
function _objectValuesToString(obj) | ||
{ | ||
var str = ""; | ||
for(var el in obj) { | ||
if(typeof obj[el] === "string") str += obj[el]; | ||
else str += _objectValuesToString(obj[el]); | ||
} // for | ||
return str; | ||
} // _objectValuesToString | ||
|
||
var parseRegExp = new RegExp(_objectValuesToString(regExp)); | ||
|
||
var allowedValuesWithDoubleQuoteRegExp = new RegExp(/\"[^\"]*[^\"]\"/g); | ||
var allowedValuesWithQuoteRegExp = new RegExp(/\'[^\']*[^\']\'/g); | ||
var allowedValuesRegExp = new RegExp(/[^,\s]/g); | ||
|
||
function parse(content, source, defaultGroup) | ||
{ | ||
// Trim | ||
content = content.replace(/^\s+|\s+$/g, ""); | ||
|
||
// Replace Linebreak with Unicode | ||
content = content.replace(/\n/g, "\uffff"); | ||
|
||
function _objectValuesToString(obj) | ||
{ | ||
var str = ""; | ||
for(var el in obj) { | ||
if(typeof obj[el] === "string") str += obj[el]; | ||
else str += _objectValuesToString(obj[el]); | ||
} // for | ||
return str; | ||
} // _objectValuesToString | ||
|
||
content = "{Number} param35=2 Type, parameter, default value and description."; | ||
|
||
// Search: group, type, fieldname, defaultValue, size, description | ||
// Example: {Boolean} [user.name="Default Value"] Users lastname. | ||
var regExp = { | ||
b: "^", | ||
vGroup: { | ||
b: "(?:", | ||
c: { | ||
b: "(?:\\(", | ||
group: "(.+?)", // 1 | ||
e: "\\))", | ||
}, | ||
e: "\\s*)?" | ||
}, | ||
vType: { | ||
b: "(?:", | ||
c: { | ||
b: "(?:\\{", | ||
type: "(.+?)", // 2 | ||
e: "\\})", | ||
}, | ||
e: "\\s*)?" | ||
}, | ||
vField: { | ||
b: "(\\[?", // 3 | ||
fieldname: "(\\S[a-zA-Z0-9\/._\\-]*)", // 4 | ||
vDefaultValue: { | ||
b: "(?:=", | ||
withQuote: { | ||
b: "(?:['|\"]", | ||
defaultValue: "(.+?(?=['|\"|\\]]))", // 5 | ||
e: "['|\"]?)" | ||
}, | ||
withoutQuote: { | ||
b: "(?!['|\"]", | ||
defaultValue: "(.+?(?=[\\]|\\s|$]))", // 5 | ||
e: "[\\]|\\s|$])" | ||
}, | ||
e: ")?" | ||
}, | ||
e: "\\]?)" | ||
}, | ||
vSize: { | ||
oWhitespace: "\\s*", | ||
b: "(?:\\{", | ||
size: "(.+?)", // 6 | ||
e: "\\})?" | ||
}, | ||
oWhitespace: "\\s*", | ||
vDescription: "(.*)?", // 7 | ||
e: "(^@|$)" // 8 ??? | ||
}; | ||
var parseRegExp = new RegExp(_objectValuesToString(regExp)); | ||
var matches = parseRegExp.exec(content); | ||
|
||
if( ! matches) return null; | ||
|
||
// Reverse Unicode Linebreaks | ||
if(matches[7]) matches[7] = matches[7].replace(/\uffff/g, "\n"); | ||
|
||
group = matches[1] || defaultGroup || "Parameter"; | ||
|
||
//if (content.substr(0, 16) === "{Number} param35") { | ||
console.log(matches); | ||
process.exit(1); | ||
|
||
return { | ||
group: group, | ||
type: matches[2], | ||
field: matches[4], | ||
defaultValue: matches[5], | ||
optional: (matches[3] && matches[3][0] === "[") ? true : false, | ||
size: matches[6], | ||
description: matches[7] || "" | ||
}; | ||
// Trim | ||
content = content.replace(/^\s+|\s+$/g, ""); | ||
|
||
// Replace Linebreak with Unicode | ||
content = content.replace(/\n/g, "\uffff"); | ||
|
||
var matches = parseRegExp.exec(content); | ||
|
||
if( ! matches) return null; | ||
|
||
var allowedValues = matches[4]; | ||
if(allowedValues) { | ||
var regExp; | ||
if (allowedValues[0] === "\"") | ||
regExp = allowedValuesWithDoubleQuoteRegExp; | ||
else if (allowedValues[0] === "'") | ||
regExp = allowedValuesWithQuoteRegExp; | ||
else | ||
regExp = allowedValuesRegExp; | ||
|
||
var allowedValuesMatch; | ||
var list = []; | ||
while (allowedValuesMatch = regExp.exec(allowedValues)) { | ||
if (typeof allowedValuesMatch == 'string') | ||
list.push(allowedValuesMatch); | ||
else | ||
list.push(allowedValuesMatch[0]); | ||
} | ||
allowedValues = list; | ||
} | ||
|
||
// Replace Unicode Linebreaks in description | ||
if(matches[10]) | ||
matches[10] = matches[10].replace(/\uffff/g, "\n"); | ||
|
||
return { | ||
group : matches[1] || defaultGroup || "Parameter", | ||
type : matches[2], | ||
size : matches[3], | ||
allowedValues: allowedValues, | ||
optional : (matches[5] && matches[5][0] === "[") ? true : false, | ||
field : matches[6], | ||
defaultValue : matches[7] || matches[8] || matches[9], | ||
description : matches[10] || "" | ||
}; | ||
} // parse | ||
|
||
function pushTo() | ||
{ | ||
return "local.parameter.fields." + getGroup(); | ||
function pushTo() { | ||
return "local.parameter.fields." + getGroup(); | ||
} | ||
|
||
function getGroup() | ||
{ | ||
return group; | ||
function getGroup() { | ||
return group; | ||
} | ||
|
||
/** | ||
* Exports. | ||
*/ | ||
module.exports = { | ||
parse: parse, | ||
pushTo: pushTo, | ||
getGroup: getGroup, | ||
markdownFields: [ "description" ] | ||
}; | ||
parse: parse, | ||
pushTo: pushTo, | ||
getGroup: getGroup, | ||
markdownFields: [ "description" ] | ||
}; |