-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathformat.js
executable file
·203 lines (156 loc) · 4.21 KB
/
format.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
/*!
* blog text formatter
* @rank
* @Date: 2014-04-03
*/
'use strict';
var fs = require('fs'),
path = require('path');
var fmt = module.exports = {};
fmt.dirRoot = null;
/**
* extFilter
* filter file with the extension name
* if result is TRUE, that will be formatted.
* @return {Boolean}
*/
fmt.extFilter = function(fp) {
return (path.extname(fp).toLowerCase() === '.md' ||
fs.statSync(fp).isDirectory());
}
/**
* set dir root
* @return {String}
*/
fmt.setDirRoot = function(dir) {
this.dirRoot = path.dirname(root);
if (this.dirRoot === '.') this.dirRoot = '';
return this.dirRoot;
}
/**
* this is formatter rules object
* the methods will be invoked by format function
*/
fmt.rules = {
mixspace: function(stream, fp) {
//匹配英文+非ASCII码/标点情况
var leftExp = /([a-z0-9\+\-]+)([^\x00-\xff,「」!。、【】『』《》?:()]+)/ig,
rightExp = /([^\x00-\xff,「」!。、【】『』《》?:()]+)([a-z0-9\+\-]+)/ig,
newstream = '',
formatter = function(all, p1, p2) {
return p1 +' '+ p2;
};
if (leftExp.test(stream) || rightExp.test(stream)) {
console.log(fp +' has been mixspace.');
newstream = stream.replace(leftExp, formatter)
.replace(rightExp, formatter);
}
return newstream;
},
quotes: function(stream, fp) {
if (!/“|”/.test(stream)) {
return stream;
}
console.log(fp +' has been replace “”.');
return stream.replace(/“/g, '「').replace(/”/g, '」');
}
}
/**
* check the dest available
* @root {String} the destination dir
* @return {Boolean}
*/
fmt._chkdest = function(dest) {
if (!fs.existsSync(dest)) {
console.log('[ERROR] dest "' +dest+ '" is not exists.');
return false;
}
if (!fs.statSync(dest).isDirectory()) {
console.log('[ERROR] dest "' +dest+ '" is not a dir.');
return false;
}
return true;
}
/**
* the formatter main function
* @root {String} the root dirname or filename
* @dest {String} optional. the output directory
* @return {void}
*/
fmt.format = function(root, dest) {
if (!fs.existsSync(root)) {
console.log('[Error] "' +root+ '" is not exists.');
return;
}
if (dest && !this._chkdest(dest)) return;
console.log('[DIRROOT] '+ this.setDirRoot(root));
var stat = fs.statSync(root);
if (stat.isFile()) {
console.log(root +' is a [File].');
this.applyRules(root, dest);
} else if (stat.isDirectory()) {
console.log('"' +root+ '" is a [Directory].');
var counter = 0;
this.walk(root, function(fp) {
counter++;
return fmt.applyRules(fp, dest);
}, this.extFilter);
console.log('[COUNTER] totally files counter: '+counter );
} else {
console.log('dir stat [Error].');
}
},
/**
* recursive dir and execute callback when scan a file.
* @dir {String} the root dirname or filename
* @cb {Function} optional. excute callback function
* @filter {Function} optional. filter filename
* @return {void}
*/
fmt.walk = function(dir, cb, filter) {
var obj = fmt,
retval = false,
files = fs.readdirSync(dir)
.filter(function(filename) {
return filter.call(this, dir +'/'+ filename);
});
files.forEach(function(fp) {
var tmp = dir +'/'+ fp,
stat = fs.statSync(tmp);
if (stat.isDirectory()) {
console.log('[RECURSIVE] '+ tmp);
obj.walk.call(this, tmp, cb, filter);
} else {
if (cb) retval = cb(tmp);
if (!retval) process.exit(1);
}
});
}
/**
* apply all formmater rules in fmt.rules
* @dir {String} file path
* @cb {Function} optional. destination dir
* @return {Boolean}
*/
fmt.applyRules = function(fp, dest) {
var newstream = '',
dir = "";
if (dest && fmt._chkdest(dest)) {
dest = dest+ '/'+ fp.replace(fmt.dirRoot, '').replace(/\/\//i, '\/');
dir = path.dirname(dest);
(!fs.existsSync(dir)) && fs.mkdir(dir);
} else {
dest = fp;
}
console.log('[SCAN] ' +fp);
console.log('[DEST] ' +dest);
newstream = fs.readFileSync(fp).toString();
for (var method in fmt.rules) {
newstream = fmt.rules[method].call(this, newstream, fp);
}
fs.writeFileSync(dest, newstream);
return true;
}
var args = process.argv.splice(2);
if (args[0]) fmt.format(args[0], args[1]);
else console.log('No input dir/file. \nUsage: node ' +path.basename(__filename)+ ' dir/file [dest].');