forked from small-tou/js2image
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (111 loc) · 3.85 KB
/
Copy pathindex.js
File metadata and controls
123 lines (111 loc) · 3.85 KB
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
var fs = require("fs");
var ImageToStruct = require("./utils/image-to-struct.js");
var JSMin = require("./utils/js-min.js");
var KeepLine = require("./utils/keep-line.js");
var Promise = require("bluebird");
var Js2Image = function(jsCode,imagePath,options,callback){
ImageToStruct(imagePath,options).then(function(struct){
JSMin(jsCode).then(function(code){
//fs.writeFileSync(jsPath.replace(".js",".min.js"),code,'utf-8')
//提取不换行的字符串
var hold_code = KeepLine.pickFromCode(code);
//分离代码,以可分割单位拆分成数组。
var lines = hold_code.replace(/([^a-zA-Z_0-9=!|&$])/g,"\n$1\n").split("\n");
//把分离后的代码里的占位符替换回代码。
KeepLine.findBack(lines);
var result = "";
while(lines.length>0){
//循环往struct里填充代码
struct.forEach(function(s){
var chars_arr = s.replace(/ +/g," ");//一行有多组分离的*****
var r = s;
chars_arr.split(/ +/).forEach(function(chars){
if(chars.length == 0){
return;
}
var char_count = chars.length;
//从lines里取出char_count数量的代码来填充,不一定精准,要确保断行正确
var l = pickFromLines(lines,char_count);
r = r.replace(chars,function(){
return l;
})
})
result += r+"\n"
})
}
//result就是最终的代码
callback(null,result);
}).catch(function(e){
callback(e);
});
})
}
module.exports = {
/**
* 从源文件读取,然后写到目标文件里。
* @param jsPath
* @param imagePath
* @param outputPath
* @param options
* @param callback
*/
writeToFile:function(jsPath,imagePath,outputPath,options){
var jsCode = fs.readFileSync(jsPath);
return new Promise(function(resolve,reject){
Js2Image(jsCode.toString(),imagePath,options,function(e,code){
code = "/* generated by https://github.com/xinyu198736/js2image */ \n\n"+code;
fs.writeFileSync(outputPath,code,'utf-8');
if(e){
reject(e);
}else{
resolve(code);
}
})
})
},
/**
* 从源代码字符串开始,直接返回一个处理后的code
* @param jsCode
* @param imagePath
* @param options
* @returns {bluebird|exports|module.exports}
*/
getCode:function(jsCode,imagePath,options){
return new Promise(function(resolve,reject){
Js2Image(jsCode,imagePath,options,function(e,code){
if(e){
reject(e);
}else{
resolve(code);
}
})
})
}
};
/**
* 从lines数组里顺序取字符来填充到指定的长度,不一定精确。
* @param count
* @returns {string}
*/
function pickFromLines(lines,count){
var t = "";
var has_pick_count = 0; //取了多少次
var now_length = 0; //当前填充的长度
for(var i =0;i<lines.length;i++){
var l = lines[i];
now_length += l.length;
has_pick_count ++;
if(now_length>count){
if(now_length - count >3&&has_pick_count>1){
//如果长度超长太多,去掉最后一个填充进来的字符串。
has_pick_count--;
}
break;
}
}
for(var i =0;i<has_pick_count;i++){
var s = lines.shift();
t += s;
}
return t;
}