1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 | const util = require('util');
|
6 | const jdfUtils = require('jdf-utils');
|
7 | const logger = require('jdf-log');
|
8 | const $ = jdfUtils.base;
|
9 | const f = jdfUtils.file;
|
10 | const jdf = require("./jdf");
|
11 | const velocity = require('velocityjs');
|
12 | const VFS = require('./VFS/VirtualFileSystem');
|
13 | const vm = module.exports;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | vm.render = function(vmSource, options) {
|
28 | let dataObj = options.dataObj;
|
29 | let dirname = options.dirname;
|
30 | let existMap = options.existMap;
|
31 |
|
32 | if (!(vmSource && dataObj)) {
|
33 | return '';
|
34 | }
|
35 |
|
36 | let macros = {
|
37 | parse: function (name) {
|
38 | let content = `<!-- begin embed widget ${name} -->\n`;
|
39 | let vmpath = path.resolve(dirname, name);
|
40 | let vmVfile = VFS.queryFile(vmpath);
|
41 | if (!vmVfile) {
|
42 | logger.error(`'#parse(${name})' in ${dirname}'s vm, path not exist or not a file`);
|
43 | return name;
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | let vmName = path.basename(vmpath).replace(path.extname(vmpath), '');
|
50 | if (!existMap.cssMap[vmName]) {
|
51 |
|
52 | let csspath = vmpath.replace(path.extname(vmpath), '.css');
|
53 | csspath = path.relative(VFS.originDir, csspath);
|
54 | csspath = path.join(VFS.targetDir, csspath);
|
55 | let cssVfile = VFS.queryFile(csspath, 'target');
|
56 | if (cssVfile) {
|
57 | existMap.cssMap.set(vmName, true);
|
58 | }
|
59 | }
|
60 | if (!existMap.jsMap[vmName]) {
|
61 | let jspath = vmpath.replace(path.extname(vmpath), '.js');
|
62 | jspath = path.relative(VFS.originDir, jspath);
|
63 | jspath = path.join(VFS.targetDir, jspath);
|
64 | let jsVfile = VFS.queryFile(jspath, 'target');
|
65 | if (jsVfile) {
|
66 | existMap.jsMap.set(vmName, true);
|
67 | }
|
68 | }
|
69 |
|
70 | content += vm.render(vmVfile.targetContent, {
|
71 | dataObj: dataObj,
|
72 | dirname: path.dirname(vmpath),
|
73 | existMap: existMap
|
74 | });
|
75 |
|
76 | content += `<!-- end embed widget ${name} -->`;
|
77 |
|
78 | return content;
|
79 | }
|
80 | }
|
81 |
|
82 | return velocity.render(vmSource, dataObj, macros);
|
83 | }
|