forked from iarna/rtf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (33 loc) · 877 Bytes
/
index.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
'use strict'
const RTFParser = require('./rtf-parser.js')
const RTFDocument = require('./rtf-document.js')
const RTFInterpreter = require('./rtf-interpreter.js')
module.exports = parse
parse.string = parseString
parse.stream = parseStream
function parseString (string, cb) {
parse(cb).end(string)
}
function parseStream (stream, cb) {
stream.pipe(parse(cb))
}
function parse (cb) {
let errored = false
const errorHandler = err => {
if (errored) return
errored = true
parser.unpipe(interpreter)
interpreter.end()
cb(err)
}
const document = new RTFDocument()
const parser = new RTFParser()
parser.once('error', errorHandler)
const interpreter = new RTFInterpreter(document)
interpreter.on('error', errorHandler)
interpreter.on('finish', () => {
if (!errored) cb(null, document)
})
parser.pipe(interpreter)
return parser
}