-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
165 lines (143 loc) · 4.36 KB
/
import.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
'use strict';
let outputDir = 'output'; // default
const path = require('path');
const xslt4node = require('xslt4node');
const fs = require('fs-extra');
const DOMParser = require('xmldom').DOMParser;
const XMLSerializer = require('xmldom').XMLSerializer;
const _ = require('lodash');
// const mv = require('mv');
// const md = require('html-md');
let config = {};
/**
* Constructor
* @param {object} options - config options
* @returns {Convert}
* @constructor
*/
function Convert(options) {
if (!(this instanceof Convert)) {
return new Convert(options);
}
config = options;
}
/**
* Set up object: read in XSL for transforms
*/
Convert.prototype.init = function (xslfile) {
const p = new Promise((resolve, reject) => {
fs.readFile(process.cwd() + "/" + xslfile + ".xsl", (err, data) => {
if (err) {
return console.error(err);
}
this.XML2HTML = new DOMParser().parseFromString(data.toString());
if (!this.XML2HTML) {
console.log('Unable to process XSL file');
process.exit();
reject();
}
this.inXML = null;
console.log('Initialized.');
resolve();
});
});
return p;
};
Convert.prototype.importFolder = function (inDir, outDir) {
const queue = [];
fs.readdirSync(inDir).forEach(file => {
if (/\.xml$/.test(file)) {
queue.push(() => this.importFile(inDir, outDir, file));
}
});
queue.reduce((p, f) => p.then(f), Promise.resolve())
.then(() => {
console.log('Finished');
return process.exit(0);
});
};
Convert.prototype.importFile = function (inDir, outDir, file) {
const p = new Promise((resolve, reject) => {
if (outDir) {
outputDir = outDir;
}
this.inXML = null;
fs.readFile(inDir + '/' + file, (err, data) => {
if (err) {
if ('ENOENT' !== err.code) {
console.error("addXML:", err);
}
if ('ENOENT' === err.code) {
console.log('File not found in add XML:', inDir, file);
}
return;
// return callback();
}
let xml = new DOMParser().parseFromString(data.toString());
this.inXML = xml;
if (!this.XML2HTML) {
console.log('Bad XSL file');
reject();
process.exit();
}
const xslt = new XMLSerializer().serializeToString(this.XML2HTML);
xml = new XMLSerializer().serializeToString(this.inXML);
const config = {
xslt: xslt,
source: xml,
result: String,
props: {
indent: 'yes'
}
};
xslt4node.transform(config, (err, result) => {
if (err) {
console.log('Error', err);
reject();
}
file = file.substring(0, file.lastIndexOf('.'));
console.log('Writing file: ', file);
_writeXML(result, outDir + '/' + file + '.leo')
.then(() => resolve())
});
});
let _writeXML = (xml, filePath) => {
return new Promise((resolve, reject) => {
// let xmlString = new XMLSerializer().serializeToString(xml);
fs.writeFile(filePath, xml, (err) => {
if (err) {
console.log(err);
return reject(err);
}
console.log('Written to: ', filePath);
return resolve();
});
})
};
});
return p;
};
function domListToJson(list, el) {
if (el.tagName === 'li') {
return list.data.push(el.textContent);
}
let children = el.childNodes;
if (!children) {
return;
}
let childList = {};
childList.type = el.tagName;
childList.data = [];
if (list) {
list.list = childList;
} else {
list = childList;
}
_.forEach(children, (child) => {
if (child.nodeType !== 3) {
domListToJson(childList, child);
}
});
return list;
}
module.exports = Convert;