-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.js
124 lines (101 loc) · 2.81 KB
/
convert.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
var webPage = require('webpage');
var webserver = require('webserver');
var fs = require('fs');
var args = require('system').args;
var jqueryFile = 'lib\\jquery.min.js';
var sieFile = 'lib\\sie18-ef.1.js';
var vmlTempl = 'lib\\convert.html';
var fileIndex = 0;
var path = args[1];
var list = fs.list(path);
var scriptPath = args[0];
if (args.length === 1) {
console.log("Please pass a path");
} else {
main();
}
if(scriptPath.indexOf('\\') > -1){
scriptPath = scriptPath.substr(0, scriptPath.lastIndexOf('\\') + 1);
}
else{
scriptPath = "";
}
jqueryFile = scriptPath + jqueryFile;
sieFile = scriptPath + sieFile;
vmlTempl = scriptPath + vmlTempl;
//console.log("---- base path : " + scriptPath + " ----")
function main() {
var server = webserver.create();
server.listen(9527, function (req, res) {
res.statusCode = 200;
var url = req.url;
var query = url.split('/');
switch (query[1]) {
case "jquery":
res.write(fs.read(jqueryFile));
break;
case "sie":
res.write(fs.read(sieFile));
break;
case "svg":
var urlSvg = url.replace('/svg/', '/');
res.write(
fs.read(path + "\\" + urlSvg.substring(urlSvg.indexOf('/') + 1))
.replace(/<\?xml[^>]*>/g, "")
.replace(/<!--[^>]*-->/g, "")
.replace(/<!DOCTYPE[^>]*>/g, "")
.replace(/id=\S*/g, "")
);
break;
case "vml":
res.write(fs.read(vmlTempl));
break;
default:
res.write(fs.read(vmlTempl));
break;
}
res.close();
});
list = list.filter(function(filePath){
return filePath.indexOf(".svg") > -1
});
parseSvg(list[fileIndex]);
}
function parseSvg(fileNameAndPath) {
var svgPage = webPage.create();
var svgContent;
var fileName = fileNameAndPath.substring(fileNameAndPath.lastIndexOf('\\') + 1);
svgPage.open('http://localhost:9527/svg/' + fileName, function () {
svgPage.includeJs('http://localhost:9527/jquery', function () {
svgContent = svgPage.evaluate(function () {
var $ = window.$;
return $('svg').wrap('<div></div>').parent().html();
});
svgPage.close();
convertToVml(encodeURIComponent(svgContent), fileName);
});
});
}
function convertToVml(svgContent, fileName) {
var vmlPage = webPage.create();
vmlPage.open('http://localhost:9527/vml', function () {
var vmlContent = vmlPage.evaluate(function (svgContent) {
var $ = window.$;
var $result = $('.result');
var content = decodeURIComponent(svgContent);
$result.html('<script type="image/svg+xml">' + content + '</script>');
window.NAIBU._main();
return $result.html();
}, svgContent);
vmlPage.close();
fs.write(path + "\\" + fileName.replace(".svg", ".vml"), vmlContent);
console.log("-> " + fileName);
fileIndex++;
if (fileIndex === list.length) {
console.log("----> All of SVG converting completed");
phantom.exit();
} else {
parseSvg(list[fileIndex]);
}
});
}