-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
46 lines (40 loc) · 1010 Bytes
/
cli.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
'use strict';
var fs = require('fs');
var rtf = require('./rtf');
var inconv = require('iconv-lite');
var data = fs.readFileSync('file.rtf');
var root = rtf.parse(data);
var cr, idx = 0, text;
var cp = 'latin1';
if (root.meta.ansicpg) {
cp = 'windows' + root.meta.ansicpg;
}
var st = [],
pc = {idx: 0, ob: root};
var out = '';
while ((pc.idx < pc.ob.s.length) || (pc = st.pop())) {
cr = pc.ob.s[pc.idx++];
if (cr === undefined) {
continue;
}
if (Buffer.isBuffer(cr.text)) {
text = inconv.decode(cr.text, cp);
out += text;
} else if ((typeof cr.utext) === 'string') {
out += cr.utext;
} else if (cr.s) {
st.push(pc);
pc = {idx:0, ob: cr};
} else if (cr.tag) {
if (cr.tag === 'fonttbl' || cr.tag === 'colortbl') {
pc = st.pop();
}
if (cr.tag === 'par') {
out += '\n';
}
if (cr.tag === 'page') {
out += '\n\n';
}
}
}
console.log(out);