1 | var trim = require('../utils/trim');
|
2 | var unindent = require('../utils/unindent');
|
3 |
|
4 | var group = '';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var regExp = {
|
16 | b: '^',
|
17 | oGroup: {
|
18 | b: '\\s*(?:\\(\\s*',
|
19 | group: '(.+?)',
|
20 | e: '\\s*\\)\\s*)?'
|
21 | },
|
22 | oType: {
|
23 | b: '\\s*(?:\\{\\s*',
|
24 | type: '([a-zA-Z0-9\(\)#:\\.\\/\\\\\\[\\]_-]+)',
|
25 | oSize: {
|
26 | b: '\\s*(?:\\{\\s*',
|
27 | size: '(.+?)',
|
28 | e: '\\s*\\}\\s*)?'
|
29 | },
|
30 | oAllowedValues: {
|
31 | b: '\\s*(?:=\\s*',
|
32 | possibleValues: '(.+?)',
|
33 | e: '(?=\\s*\\}\\s*))?'
|
34 | },
|
35 | e: '\\s*\\}\\s*)?'
|
36 | },
|
37 | wName: {
|
38 | b: '(\\[?\\s*',
|
39 | name: '([a-zA-Z0-9\\:\\.\\/\\\\_-]+',
|
40 | withArray: '(?:\\[[a-zA-Z0-9\\.\\/\\\\_-]*\\])?)',
|
41 | oDefaultValue: {
|
42 | b: '(?:\\s*=\\s*(?:',
|
43 | withDoubleQuote: '"([^"]*)"',
|
44 | withQuote: '|\'([^\']*)\'',
|
45 | withoutQuote: '|(.*?)(?:\\s|\\]|$)',
|
46 | e: '))?'
|
47 | },
|
48 | e: '\\s*\\]?\\s*)'
|
49 | },
|
50 | description: '(.*)?',
|
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 |
|
75 | content = content.replace(/\n/g, '\uffff');
|
76 |
|
77 | var matches = parseRegExp.exec(content);
|
78 |
|
79 | if ( ! matches)
|
80 | return null;
|
81 |
|
82 |
|
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 |
|
109 | group = matches[1] || defaultGroup || 'Parameter';
|
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.parameter.fields.' + getGroup();
|
125 | }
|
126 |
|
127 | function getGroup() {
|
128 | return group;
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | module.exports = {
|
135 | parse : parse,
|
136 | path : path,
|
137 | method : 'push',
|
138 | getGroup : getGroup,
|
139 | markdownFields: [ 'description', 'type' ],
|
140 | markdownRemovePTags: [ 'type' ]
|
141 | };
|