-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
367 lines (352 loc) · 14.3 KB
/
main.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
const latex = require('node-latex-pdf');
const rs = require('randomstring');
const mdpdf = require('mdpdf');
const fsx = require('fs-extra');
const fh = require("filehound");
const path = require('path');
const fs = require('fs');
const os = require('os');
const latex_engine = require('./lib/docogen-latex-engine');
const md_engine = require('./lib/docogen-md-engine');
const gviz = require('./lib/docogen-graphviz');
const utils = require('./lib/docogen-util');
const docogen = {};
docogen.generate_latexpdf = function(src,dest,options,cb){
// find our file extension name
const files = fh.create().paths(src).ext('docogen').find((err,files) => {
if(err)
cb(1,"error");
else{
// strip all test file in node_modules
for(var index=0;;index++){
if(files[index] == undefined) break;
if(files[index].indexOf('node_modules') != -1){
files.splice(index,1);
index--;
}
}
// doing the work without merging
if(files.length <= 1){
// get file name
let fname = '';
if(os.type() == "Windows_NT"){
fname = files[0].split('\\').splice(-1)[0].split('.')[0];
}
else{
// Linux
fname = files[0].split('/').splice(-1)[0].split('.')[0];
}
// using defined output filename or not
let output = `${options.output}.tex` || `${rs.generate(5)}-${fname}.tex`;
fs.writeFileSync(`${os.tmpdir()}/${output}`,latex_engine.trans2latex(JSON.parse(fs.readFileSync(files[0],'utf-8'))));
// convert to latex pdf
latex(`${os.tmpdir()}/${output}`,dest,(err,pdfPath) => {
if(err)
cb(1,"latex error");
else
cb(0,`[Docogen] Job done. ${src} successfully convert into latex pdf format in ${pdfPath}`);
})
}
else{
// multiple docogen files merging
// using defined output filename or not
let output = `${options.output}.tex` || `${rs.generate(5)}-merginglatex.tex`;
// merging
fs.writeFileSync(`${os.tmpdir()}/${output}`,latex_engine.trans2latex(this.merge_docogen(files)));
// convert to latex pdf
latex(`${os.tmpdir()}/${output}`,dest,(err,pdfPath) => {
if(err)
cb(1,"latex error");
else
cb(0,`[Docogen - Merging process] Job done. ${src} successfully convert into latex pdf format in ${pdfPath}`);
})
}
}
});
}
docogen.generate_latexpdf_raw = function(md_path,options,cb){
let output = `${options.output}.tex` || `${rs.generate(5)}-${fname}.tex`,
dest = `${options.dest}` || __dirname;
// Standardlize the path with \ or /
md_path = this.path_standardlize(md_path)
let rawjsonObj = this.md2docogen(md_path);
rawjsonObj = this.resolve_rel(rawjsonObj,md_path);
fs.writeFileSync(`${os.tmpdir()}/${output}`,latex_engine.trans2latex(rawjsonObj))
// convert to latex pdf
latex(`${os.tmpdir}/${output}`,dest,(err,pdfPath) => {
if(err)
cb(1,"latex error");
else
cb(0,`[Docogen - Merging process from rawjsonObj] Job done. JSONObj successfully convert into latex pdf format in ${pdfPath}`);
})
}
docogen.generate_mdpdf = function(src,dest,options,cb){
// find our file extension
const files = fh.create().paths(src).ext('docogen').find((err,files) => {
if(err)
cb(1,"error");
else{
// strip all test file in node_modules
for(var index in files){
if(files[index].indexOf('node_modules') != -1){
files.splice(index,1);
}
}
if(files.length <= 1){
// get file name
let fname = '';
if(os.type() == "Windows_NT"){
fname = files[0].split('\\').splice(-1)[0].split('.')[0];
}
else{
// Linux
fname = files[0].split('/').splice(-1)[0].split('.')[0];
}
// using defined output filename or not
let output = `${options.output}.pdf` || `${rs.generate(5)}-${fname}.pdf`;
fs.writeFileSync(os.tmpdir()+'/'+output+'.md',md_engine.trans2md(JSON.parse(fs.readFileSync(files[0],'utf-8'))));
let opt = {
source: os.tmpdir()+'/'+output+'.md',
destination: path.join(dest,output),
pdf: {
format: 'A4',
header: {
height: 0
}
}
};
// converting
mdpdf.convert(opt).then((pdfPath) => {
cb(0,`[Docogen] Job done. ${src} successfully convert into markdown pdf format in ${pdfPath}`);
}).catch((err) => {
cb(1,`[Docogen] Job failed.`);
});
}
else{
// multiple docogen files merging
// using defined output filename or not
let output = `${options.output}.pdf` || `${rs.generate(5)}-mergingmd.pdf`;
fs.writeFileSync(os.tmpdir()+'/'+output+'.md',md_engine.trans2md(this.merge_docogen(files)));
let opt = {
source: os.tmpdir()+'/'+output+'.md',
destination: path.join(dest,output),
pdf: {
format: 'A4',
header: {
height: 0
}
}
};
// converting
mdpdf.convert(opt).then((pdfPath) => {
cb(0,`[Docogen - Merging process] Job done. ${src} successfully convert into markdown pdf format in ${pdfPath}`);
}).catch((err) => {
cb(1,`[Docogen] Job failed.`);
});
}
}
});
}
docogen.merge_docogen = function(src_arr,options){
// here comes the files list
let jsobj = {};
console.log("Have " + src_arr.length + " files.");
let opt = options || false;
if(opt.detail){
console.dir(src_arr); // print out all the files name
}
// Support "article","reference" part merging
for(var index in src_arr){
var tmp = JSON.parse(fs.readFileSync(src_arr[index],'utf-8'));
// ============ set title (only one time) ============
if(tmp.title != undefined && jsobj.title == undefined){
jsobj.title = tmp.title;
}
// ============ set options (only one time) ============
if(tmp.options != undefined && jsobj.options == undefined){
jsobj.options = tmp.options;
}
// ============ set author (only one time) ============
if(tmp.author != undefined && jsobj.author == undefined){
jsobj.author = tmp.author;
}
// ============ set abstract (only one time) ============
if(tmp.abstract != undefined && jsobj.abstract == undefined){
jsobj.abstract = tmp.abstract;
}
// ============ merge article ============
if(tmp.article != undefined && jsobj.article == undefined){
src_arr[index] = this.path_standardlize(src_arr[index])
// Resolving the path
tmp = this.resolve_rel(tmp,src_arr[index])
// first time setting
jsobj.article = tmp.article;
}
else if( tmp.article != undefined && jsobj.article.length >= 1 ){
src_arr[index] = this.path_standardlize(src_arr[index])
// Resolving the path
tmp = this.resolve_rel(tmp,src_arr[index])
// concat then sort , by priority
jsobj.article = jsobj.article.concat(tmp.article);
// sort by prority
jsobj.article.sort(function(a,b){
return (a.priority - b.priority);
});
}
// ============ merge reference ============
if( tmp.reference != undefined ){
if( jsobj.reference == undefined ) jsobj.reference = tmp.reference;
else {
// just concat
jsobj.reference = jsobj.reference.concat(tmp.reference);
}
}
}
// return result jsObj
return jsobj;
}
// Promise version of merge_docogen
docogen.merge_docogen_promise = function(src_arr,options){
return new Promise( (resolve,reject) => {
// here comes the files list
let jsobj = {};
console.log("Have " + src_arr.length + " files.");
let opt = options || false;
if(opt.detail){
console.dir(src_arr); // print out all the files name
}
// Support "article","reference" part merging
for(var index in src_arr){
// that file script with working space is src_arr[index] -> here is the key to trans relative to absolutive
var tmp = JSON.parse(fs.readFileSync(src_arr[index],'utf-8'));
// ============ set title (only one time) ============
if(tmp.title != undefined && jsobj.title == undefined){
jsobj.title = tmp.title;
}
// ============ set options (only one time) ============
if(tmp.options != undefined && jsobj.options == undefined){
jsobj.options = tmp.options;
}
// ============ set author (only one time) ============
if(tmp.author != undefined && jsobj.author == undefined){
jsobj.author = tmp.author;
}
// ============ set abstract (only one time) ============
if(tmp.abstract != undefined && jsobj.abstract == undefined){
jsobj.abstract = tmp.abstract;
}
// ============ merge article ============
if(tmp.article != undefined && jsobj.article == undefined){
// find figure & get translate to absolute
src_arr[index] = this.path_standardlize(src_arr[index])
// Resolving the path
tmp = this.resolve_rel(tmp,src_arr[index])
// first time setting
jsobj.article = tmp.article;
}
else if( tmp.article != undefined && jsobj.article.length >= 1 ){
// standardize
src_arr[index] = this.path_standardlize(src_arr[index])
// Resolving the path
tmp = this.resolve_rel(tmp,src_arr[index])
// concat then sort , by priority
jsobj.article = jsobj.article.concat(tmp.article);
// sort by prority
jsobj.article.sort(function(a,b){
return (a.priority - b.priority);
});
}
// ============ merge reference ============
if( tmp.reference != undefined ){
if( jsobj.reference == undefined ) jsobj.reference = tmp.reference;
else {
// just concat
jsobj.reference = jsobj.reference.concat(tmp.reference);
}
}
}
// return result jsObj
return resolve({
msg: "success",
obj: jsobj
});
})
}
docogen.resolve_rel = function(jsObj,dirname){
console.log("Resolving Raw path:" + dirname)
// resolving the path from rel to abs
if(os.type() == "Windows_NT"){
jsObj.article = utils.resolve(jsObj.article,dirname.substring(0,dirname.lastIndexOf('\\')));
}
else if(os.type() == "Linux"){
// Linux
jsObj.article = utils.resolve(jsObj.article,dirname.substring(0,dirname.lastIndexOf('/')));
}
else{
// FIXME: Other platform, currently use linux
jsObj.article = utils.resolve(jsObj.article,dirname.substring(0,dirname.lastIndexOf('/')));
}
return jsObj
}
//
docogen.path_standardlize = function(dirname){
// resolving the path from rel to abs
if(os.type() == "Windows_NT"){
dirname = dirname.replace(/\//g,'\\')
}
else if(os.type() == "Linux"){
// Linux (if you use Window path, and then change into Linux form)
dirname = dirname.replace(/\\/g,'/')
}
else{
// FIXME: Other platform, currently use linux
dirname = dirname.replace(/\\/g,'/')
}
return dirname
}
docogen.merge_docogen_ex = function(src_path,options,cb){
// passing the src project dir path (do the same thing as merg_docogen)
const files = fh.create().paths(src_path).ext('docogen').find((err,files) => {
if(err)
cb(1,"error");
else{
// strip all test file in node_modules
for(var index in files){
if(files[index].indexOf('node_modules') != -1){
files.splice(index,1);
}
}
// and then pass to merge_docogen, return an docogen format json object
cb(0,this.merge_docogen(files,options));
}
});
}
// Promise version of merge_docogen
docogen.merge_docogen_ex_promise = function(src_path,options){
return new Promise((resolve,reject) => {
const files = fh.create().paths(src_path).ext('docogen').find((err,files) => {
if(err)
reject("[Merging Process] Searching files error");
else{
// strip all test file in node_modules
for(var index in files){
if(files[index].indexOf('node_modules') != -1){
files.splice(index,1);
}
}
// and then pass to merge_docogen, return an docogen format json object
resolve({
msg: "[Merging Process] Merging success",
obj: this.merge_docogen(files,options)
})
}
});
})
}
// Utils exports
docogen.utils = utils;
// Graphviz exports
docogen.gviz = gviz;
// Export for markdown engine
docogen.md2docogen = md_engine.md2docogen;
module.exports = docogen;