1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const _ = require('underscore');
|
10 | const fs = require('graceful-fs');
|
11 | const path = require('path');
|
12 | const minimatch = require('minimatch');
|
13 | const HTML_CHARS = {
|
14 | '&': '&',
|
15 | '<': '<',
|
16 | '>': '>',
|
17 | '"': '"',
|
18 | "'": ''',
|
19 | '/': '/',
|
20 | '`': '`'
|
21 | };
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function fmtProcess (process) {
|
30 | return process.split(',').map(safetrim);
|
31 | }
|
32 | exports.fmtProcess = fmtProcess;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function getNamespace (target) {
|
44 | var nssource = [target.module, target.clazz, target.name];
|
45 | return nssource.filter(function (item) {
|
46 | return !!item;
|
47 | }).join('.');
|
48 | }
|
49 | exports.getNamespace = getNamespace;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function escapeHTML (html) {
|
59 | return html.replace(/[&<>"'\/`]/g, function (m) {
|
60 | return HTML_CHARS[m];
|
61 | });
|
62 | }
|
63 | exports.escapeHTML = escapeHTML;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function safetrim (str) {
|
73 | if (str && _.isFunction(str.trim)) {
|
74 | return str.trim();
|
75 | } else {
|
76 | return String(str || '').trim();
|
77 | }
|
78 | }
|
79 | exports.safetrim = safetrim;
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function unindent (contents) {
|
92 | var indent = contents.match(/^(\s+)/);
|
93 | if (indent) {
|
94 | contents = contents.replace(new RegExp('^' + indent[1], 'gm'), '');
|
95 | }
|
96 | return contents;
|
97 | }
|
98 | exports.unindent = unindent;
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function filterFileName (f) {
|
111 | return (f || '').replace(/[\/\\]/g, '_');
|
112 | }
|
113 | exports.filterFileName = filterFileName;
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function getFoundAt (obj, options) {
|
125 | var ext = options.markdown ? '.md' : '.html';
|
126 | var ret = '';
|
127 | if (obj.file && obj.line && !options.nocode) {
|
128 | if (obj.path) {
|
129 | ret = obj.path + '#l' + obj.line;
|
130 | } else {
|
131 | ret = '../files/' + filterFileName(obj.file) + ext + '#l' + obj.line;
|
132 | }
|
133 | }
|
134 | return ret;
|
135 | }
|
136 | exports.getFoundAt = getFoundAt;
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function getLayouts (dir, useMarkdown) {
|
147 | if (!_.isString(dir)) return {};
|
148 | return getPages(path.join(dir, 'layouts'), useMarkdown);
|
149 | }
|
150 | exports.getLayouts = getLayouts;
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | var cache = {};
|
162 | function getPages (dir, useMarkdown) {
|
163 | if (cache[dir]) {
|
164 | return cache[dir];
|
165 | }
|
166 | var pages = {};
|
167 | var stat = fs.statSync(dir);
|
168 | if (!stat.isDirectory()) {
|
169 | return pages;
|
170 | }
|
171 |
|
172 | _.each(
|
173 | fs.readdirSync(dir),
|
174 | function (name) {
|
175 | var p = path.join(dir, name);
|
176 | var ext = useMarkdown ? '.mdt' : '.handlebars';
|
177 | if (path.extname(name) === ext && fs.statSync(p).isFile()) {
|
178 | var name = path.basename(name, ext);
|
179 | var text = fs.readFileSync(p, 'utf8');
|
180 | Object.defineProperty(pages, name, {
|
181 | enumerable: true,
|
182 | get: function () {
|
183 | return text
|
184 | },
|
185 | set: function () {}
|
186 | });
|
187 | }
|
188 | }
|
189 | );
|
190 | cache[dir] = pages;
|
191 | return pages;
|
192 | }
|
193 | exports.getPages = getPages;
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | function getPartials (dir, useMarkdown) {
|
204 | if (!_.isString(dir)) return {};
|
205 | return getPages(path.join(dir, 'partials'), useMarkdown);
|
206 | }
|
207 | exports.getPartials = getPartials;
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | function prepare (inDirs, options, callback) {
|
220 | var layouts, partials, type = 'project';
|
221 | var defaults = {
|
222 | 'meta': {
|
223 | 'project': options.project,
|
224 | 'component': {}
|
225 | },
|
226 | 'pages': {},
|
227 | 'layouts': {},
|
228 | 'partials': {},
|
229 | 'viewClass': require('./docview').DocView
|
230 | };
|
231 |
|
232 | if (options && options.skipLoad) {
|
233 |
|
234 |
|
235 | options = _.extend(defaults, options);
|
236 | } else {
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | if (inDirs[0] === inDirs[1]) {
|
254 | layouts = getLayouts(inDirs[0], options.markdown);
|
255 | partials = getPartials(inDirs[0], options.markdown);
|
256 | } else {
|
257 | layouts = _.extend(
|
258 | getLayouts(inDirs[0], options.markdown),
|
259 | getLayouts(inDirs[1], options.markdown)
|
260 | );
|
261 | partials = _.extend(
|
262 | getPartials(inDirs[0], options.markdown),
|
263 | getPartials(inDirs[1], options.markdown)
|
264 | );
|
265 | }
|
266 | options = _.extend(defaults, options);
|
267 | options = _.extend(options, {
|
268 | 'layouts': layouts,
|
269 | 'partials': partials,
|
270 | });
|
271 | }
|
272 |
|
273 |
|
274 | if (!options.meta.component.assets && options.component) {
|
275 | options.meta.component.assets = '../assets/' + options.meta.name;
|
276 | }
|
277 | if (_.isUndefined(options.meta.layout)) {
|
278 | options.meta.layout = options.layouts[type] ? type : 'main';
|
279 | }
|
280 | if (_.isFunction(callback)) {
|
281 | callback(null, options);
|
282 | }
|
283 | return options;
|
284 | }
|
285 | exports.prepare = prepare;
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | function fixType (t) {
|
296 | t = safetrim(t);
|
297 | if (t && t.indexOf('.') === -1) {
|
298 | t = t.replace(/{/g, '').replace(/}/g, '');
|
299 | var firstChar = t.charAt(0),
|
300 | upperFirstChar = firstChar.toUpperCase();
|
301 |
|
302 | if (firstChar !== upperFirstChar) {
|
303 | return upperFirstChar + t.substring(1);
|
304 | }
|
305 | }
|
306 | return t;
|
307 | };
|
308 | exports.fixType = fixType;
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | function webpath (url) {
|
321 | var args = [].concat.apply([], arguments),
|
322 | parts = path.join.apply(path, args).split(/[\\\/]/);
|
323 | return parts.join('/');
|
324 | }
|
325 | exports.webpath = webpath;
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | function localize (str, lang) {
|
336 | var splitedStrArr = (str || '').split('!#');
|
337 | var supportedLang = ['en', 'zh'];
|
338 | return splitedStrArr.map(function(block) {
|
339 | var langFlag = block.slice(0, 2);
|
340 | var selectedLang = supportedLang.indexOf(langFlag);
|
341 | if (selectedLang === -1) {
|
342 |
|
343 | return {
|
344 | raw: block,
|
345 | all: true
|
346 | };
|
347 | } else {
|
348 | return {
|
349 | raw: block.slice(2),
|
350 | lang: supportedLang[selectedLang]
|
351 | };
|
352 | }
|
353 | }).filter(function(block) {
|
354 | if (!block || !block.raw) {
|
355 | return false;
|
356 | }
|
357 | if (block.all) {
|
358 | return true;
|
359 | }
|
360 | return block.lang === (lang || 'en');
|
361 | }).map(function(block) {
|
362 | return block.raw.trim();
|
363 | }).join('\n');
|
364 | }
|
365 | exports.localize = localize;
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | function markdownLink (str) {
|
375 | return str
|
376 | .replace(/[:,]/g, '-')
|
377 | .replace(/[\s\(\)\[\]=]/g, '')
|
378 | .toLowerCase();
|
379 | }
|
380 | exports.markdownLink = markdownLink;
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | function buildFileTree (files) {
|
390 | var tree = {};
|
391 | _.each(files, function (v) {
|
392 | var p = v.name.split('/');
|
393 | var par;
|
394 | p.forEach(function (i, k) {
|
395 | if (!par) {
|
396 | if (!tree[i]) {
|
397 | tree[i] = {};
|
398 | }
|
399 | par = tree[i];
|
400 | } else {
|
401 | if (!par[i]) {
|
402 | par[i] = {};
|
403 | }
|
404 | if (k + 1 === p.length) {
|
405 | par[i] = {
|
406 | path: v.name,
|
407 | name: filterFileName(v.name)
|
408 | };
|
409 | }
|
410 | par = par[i];
|
411 | }
|
412 | });
|
413 | });
|
414 | return tree;
|
415 | }
|
416 | exports.buildFileTree = buildFileTree;
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | function stringlog(data) {
|
428 | var line, file;
|
429 | if (data.file && data.line) {
|
430 | file = data.file;
|
431 | line = data.line;
|
432 | } else {
|
433 | data.forEach(function (d) {
|
434 | if (d.tag === 'file') {
|
435 | file = d.value;
|
436 | }
|
437 | if (d.tag === 'line') {
|
438 | line = d.value;
|
439 | }
|
440 | });
|
441 | }
|
442 | return ' ' + file + ':' + line;
|
443 | }
|
444 | exports.stringlog = stringlog;
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 | function defineReadonly (obj, name, val) {
|
451 | Object.defineProperty(obj, name, {
|
452 | get: function () {
|
453 | return val;
|
454 | }
|
455 | });
|
456 | }
|
457 | exports.defineReadonly = defineReadonly;
|
458 |
|