This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 877
/
Copy pathregionExtractor.js
281 lines (253 loc) · 7.31 KB
/
regionExtractor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* NOT a dgeni service because we need to be able to use it externally
* as well as from dgeni.
* @description
*
*/
var _ = require('lodash');
var nullLine = '###';
var nullLinePattern = new RegExp(nullLine + '\n', 'g');
module.exports = {
buildRegionDocs: buildRegionDocs,
getRegionDoc: getRegionDoc,
removeDocTags: removeDocTags
};
// split out each fragment in {content} into a separate doc
// a fragment is a section of text surrounded by
// 1) In front: a comment marker followed by '#docregion' followed by an optional region name. For example:
// <-- #docregion foo --> for html
// or // #docregion foo for js/ts
// 2) In back: a comment marker followed by '#enddocregion'
// Regions can be nested and any regions not 'closed' are automatically closed at the end of the doc.
// empty enddocregion always closes last region started.
// enddocregions with names that do no match start region tags get ignored.
function buildRegionDocs(content, extn) {
var commentInfo = getCommentInfo(extn);
if (!commentInfo) {
return [ { content: content } ];
}
var lines = result = content.split(/\r?\n/);
var docStack = []; // items will be both popped and removed from the middle
var docMap = {};
var docPlaster = '. . .';
var doc;
var regionNames;
lines.forEach(function(line, ix) {
if (isCommentLine(line, commentInfo.prefix)) {
if (hasRegionTag(line)) {
lines[ix] = nullLine;
regionNames = getRegionNames(line);
regionNames.forEach(function(rn) {
doc = docMap[rn];
if (!doc) {
// regionName may be ''
doc = {regionName: rn, ranges: [ { startIx: ix} ] };
docMap[rn] = doc;
} else {
// only add a new range if prev range is closed
var lastRange = doc.ranges[doc.ranges.length-1];
if (lastRange.endIx) {
doc.ranges.push({startIx: ix});
}
}
docStack.push(doc);
});
} else if (hasEndRegionTag(line)) {
lines[ix] = nullLine;
regionNames = getEndRegionNames(line);
regionNames.forEach(function(rn) {
// handle endregions with no name specially.
// They operate on the last region created.
if (rn.length == 0) {
if (docStack.length) {
// update last item on the stack
doc = docStack.pop();
doc.ranges[doc.ranges.length - 1].endIx = ix;
}
} else {
doc = docMap[rn];
// ignore endregion if name is specified but not found.
if (doc) {
doc.ranges[doc.ranges.length - 1].endIx = ix;
// remove doc from stack
_.remove(docStack, function (item) {
return item.regionName === rn;
});
}
}
});
} else if (hasDocPlasterTag(line)) {
line[ix] = nullLine;
docPlaster = getDocPlaster(line);
}
}
});
var docs = _.values(docMap);
var plasterComment = docPlaster && commentInfo.plasterPattern.replace('{tag}', docPlaster);
docs = reattachDocs(docs, lines, plasterComment);
return docs;
}
function getRegionDoc(content, extn, regionName) {
var docs = buildRegionDocs(content, extn);
var doc = _.find(docs, function (doc) {
return doc.regionName === regionName;
});
return doc && doc.content;
}
function removeDocTags(content, extn) {
var commentInfo = getCommentInfo(extn);
if (commentInfo == null) {
return content;
}
var lines = result = content.split(/\r?\n/);
lines.forEach(function(line, ix) {
if (isCommentLine(line, commentInfo.prefix)) {
if (hasDocTag(line)) {
lines[ix] = nullLine;
}
}
});
var result = joinLines(lines);
return result;
}
function reattachDocs(docs, lines, plasterComment) {
docs.forEach(function(doc) {
var content;
var fragLines = [];
doc.ranges.forEach(function (range) {
var subLines;
if (range.endIx) {
subLines = lines.slice(range.startIx + 1, range.endIx);
} else {
subLines = lines.slice(range.startIx + 1);
}
if (plasterComment && fragLines.length) {
// pad is the padding on the previous line
var pad = fragLines[fragLines.length - 1].match(/(\s*)/)[0];
fragLines.push(pad + plasterComment);
}
fragLines = fragLines.concat(subLines);
});
fragLines = trimLeftIndent(fragLines);
doc.content = joinLines(fragLines);
});
return docs;
}
function getCommentInfo(extension) {
var commentInfo;
switch (extension) {
case 'ts':
case 'js':
case 'es6':
case 'dart':
commentInfo = {
prefix: '//',
plasterPattern: '/* {tag} */'
};
break;
case 'html':
commentInfo = {
prefix: '<!--',
plasterPattern: '<!-- {tag} -->'
};
break;
case 'css':
commentInfo = {
prefix: '/*',
plasterPattern: '/* {tag} */'
};
break;
case 'json':
return null;
case 'yaml':
commentInfo = {
prefix: '#',
plasterPattern: '# {tag} '
};
break;
case 'jade':
commentInfo = {
prefix: '//',
plasterPattern: '// {tag} '
};
break;
default:
return null;
}
return commentInfo;
}
function isCommentLine(line, commentPrefix) {
return line.trim().indexOf(commentPrefix) == 0;
}
function hasDocTag(line) {
return hasRegionTag(line) || hasEndRegionTag(line) || hasDocPlasterTag(line);
}
function hasRegionTag(line) {
return line.indexOf("#docregion") >= 0;
}
function hasEndRegionTag(line) {
return line.indexOf("#enddocregion") >= 0;
}
function hasDocPlasterTag(line) {
return line.indexOf("#docplaster") >= 0;
}
function getRegionNames(line) {
return extractRegionNames(line, /#docregion\s*(\S.*)/);
}
function getEndRegionNames(line) {
return extractRegionNames(line, /#enddocregion\s*(\S.*)/);
}
function getDocPlaster(line) {
var rx = /#docplaster\s*(\S.*)/;
try {
var plaster = line.match(rx)[1];
plaster = plaster.replace("-->","").replace('\*\/',"");
return plaster.trim();
} catch (e) {
return null;
}
}
function extractRegionNames(line, rx) {
try {
var names = line.match(rx)[1];
names = names.replace(/\s*/g,'');
// Hack for html regions that look like <!-- #docregion --> or */
names = names.replace("-->","").replace('\*\/',"");
names = names.split(',');
return names;
} catch (e) {
return [''];
}
}
function trimLeftIndent(lines) {
var minIx = 100;
var ok = lines.every(function(line) {
// var ix = line.search(/\S/);
var ix = line.search(/[^ ]/);
if (ix === 0) return false;
if (ix === -1) return true;
if (ix > 0) {
minIx = Math.min(minIx, ix);
}
return true;
});
if ( (!ok) || minIx === 100) return lines;
var result = lines.map(function(line) {
if (line.length > minIx) {
return line.substr(minIx);
} else {
// this can happen if line is all blanks and shorter than mixIx
return line;
}
});
return result;
}
function joinLines(lines) {
var content = lines.join('\n');
// eliminate all #docregion lines
content = content.replace(nullLinePattern, '');
if (content.substr(-3) === nullLine) {
content = content.substr(0, content.length - 3);
}
return content;
}