|
| 1 | +var trim = require('../utils/trim'); |
| 2 | +var unindent = require('../utils/unindent'); |
| 3 | + |
| 4 | +var group = ''; |
| 5 | + |
| 6 | +// Search: group, type, optional, fieldname, defaultValue, size, description |
| 7 | +// Example: {String{1..4}} [user.name='John Doe'] Users fullname. |
| 8 | +// |
| 9 | +// Naming convention: |
| 10 | +// b -> begin |
| 11 | +// e -> end |
| 12 | +// name -> the field value |
| 13 | +// oName -> wrapper for optional field |
| 14 | +// wName -> wrapper for field |
| 15 | +var regExp = { |
| 16 | + b: '^', // start |
| 17 | + oGroup: { // optional group: (404) |
| 18 | + b: '\\s*(?:\\(\\s*', // starting with '(', optional surrounding spaces |
| 19 | + group: '(.+?)', // 1 |
| 20 | + e: '\\s*\\)\\s*)?' // ending with ')', optional surrounding spaces |
| 21 | + }, |
| 22 | + oType: { // optional type: {string} |
| 23 | + b: '\\s*(?:\\{\\s*', // starting with '{', optional surrounding spaces |
| 24 | + type: '([a-zA-Z0-9\(\)#:\\.\\/\\\\\\[\\]_\|-]+)', // 2 |
| 25 | + oSize: { // optional size within type: {string{1..4}} |
| 26 | + b: '\\s*(?:\\{\\s*', // starting with '{', optional surrounding spaces |
| 27 | + size: '(.+?)', // 3 |
| 28 | + e: '\\s*\\}\\s*)?' // ending with '}', optional surrounding spaces |
| 29 | + }, |
| 30 | + oAllowedValues: { // optional allowed values within type: {string='abc','def'} |
| 31 | + b: '\\s*(?:=\\s*', // starting with '=', optional surrounding spaces |
| 32 | + possibleValues: '(.+?)', // 4 |
| 33 | + e: '(?=\\s*\\}\\s*))?' // ending with '}', optional surrounding spaces |
| 34 | + }, |
| 35 | + e: '\\s*\\}\\s*)?' // ending with '}', optional surrounding spaces |
| 36 | + }, |
| 37 | + wName: { |
| 38 | + b: '(\\[?\\s*', // 5 optional optional-marker |
| 39 | + name: '([a-zA-Z0-9\\$\\:\\.\\/\\\\_-]+', // 6 |
| 40 | + withArray: '(?:\\[[a-zA-Z0-9\\.\\/\\\\_-]*\\])?)', // https://github.com/apidoc/apidoc-core/pull/4 |
| 41 | + oDefaultValue: { // optional defaultValue |
| 42 | + b: '(?:\\s*=\\s*(?:', // starting with '=', optional surrounding spaces |
| 43 | + withDoubleQuote: '"([^"]*)"', // 7 |
| 44 | + withQuote: '|\'([^\']*)\'', // 8 |
| 45 | + withoutQuote: '|(.*?)(?:\\s|\\]|$)', // 9 |
| 46 | + e: '))?' |
| 47 | + }, |
| 48 | + e: '\\s*\\]?\\s*)' |
| 49 | + }, |
| 50 | + description: '(.*)?', // 10 |
| 51 | + e: '$|@' |
| 52 | +}; |
| 53 | + |
| 54 | +function _objectValuesToString(obj) { |
| 55 | + var str = ''; |
| 56 | + for (var el in obj) { |
| 57 | + if (typeof obj[el] === 'string') |
| 58 | + str += obj[el]; |
| 59 | + else |
| 60 | + str += _objectValuesToString(obj[el]); |
| 61 | + } |
| 62 | + return str; |
| 63 | +} |
| 64 | + |
| 65 | +var parseRegExp = new RegExp(_objectValuesToString(regExp)); |
| 66 | + |
| 67 | +var allowedValuesWithDoubleQuoteRegExp = new RegExp(/\"[^\"]*[^\"]\"/g); |
| 68 | +var allowedValuesWithQuoteRegExp = new RegExp(/\'[^\']*[^\']\'/g); |
| 69 | +var allowedValuesRegExp = new RegExp(/[^,\s]+/g); |
| 70 | + |
| 71 | +function parse(content, source, defaultGroup) { |
| 72 | + content = trim(content); |
| 73 | + |
| 74 | + // replace Linebreak with Unicode |
| 75 | + content = content.replace(/\n/g, '\uffff'); |
| 76 | + |
| 77 | + var matches = parseRegExp.exec(content); |
| 78 | + |
| 79 | + if ( ! matches) |
| 80 | + return null; |
| 81 | + |
| 82 | + // reverse Unicode Linebreaks |
| 83 | + matches.forEach(function (val, index, array) { |
| 84 | + if (val) { |
| 85 | + array[index] = val.replace(/\uffff/g, '\n'); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + var allowedValues = matches[4]; |
| 90 | + if (allowedValues) { |
| 91 | + var regExp; |
| 92 | + if (allowedValues.charAt(0) === '"') |
| 93 | + regExp = allowedValuesWithDoubleQuoteRegExp; |
| 94 | + else if (allowedValues.charAt(0) === '\'') |
| 95 | + regExp = allowedValuesWithQuoteRegExp; |
| 96 | + else |
| 97 | + regExp = allowedValuesRegExp; |
| 98 | + |
| 99 | + var allowedValuesMatch; |
| 100 | + var list = []; |
| 101 | + |
| 102 | + while ( (allowedValuesMatch = regExp.exec(allowedValues)) ) { |
| 103 | + list.push(allowedValuesMatch[0]); |
| 104 | + } |
| 105 | + allowedValues = list; |
| 106 | + } |
| 107 | + |
| 108 | + // Set global group variable |
| 109 | + group = matches[1] || defaultGroup || 'Query'; |
| 110 | + |
| 111 | + return { |
| 112 | + group : group, |
| 113 | + type : matches[2], |
| 114 | + size : matches[3], |
| 115 | + allowedValues: allowedValues, |
| 116 | + optional : (matches[5] && matches[5][0] === '[') ? true : false, |
| 117 | + field : matches[6], |
| 118 | + defaultValue : matches[7] || matches[8] || matches[9], |
| 119 | + description : unindent(matches[10] || '') |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +function path() { |
| 124 | + return 'local.query.fields.' + getGroup(); |
| 125 | +} |
| 126 | + |
| 127 | +function getGroup() { |
| 128 | + return group; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Exports |
| 133 | + */ |
| 134 | +module.exports = { |
| 135 | + parse : parse, |
| 136 | + path : path, |
| 137 | + method : 'push', |
| 138 | + getGroup : getGroup, |
| 139 | + markdownFields: [ 'description', 'type' ], |
| 140 | + markdownRemovePTags: [ 'type' ] |
| 141 | +}; |
0 commit comments