UNPKG

rtf-stream-parser

Version:

Stream Transform class to tokenize RTF, and another to de-encapsulate text or HTML

51 lines (50 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleOutput = void 0; const utils_1 = require("../utils"); function flushBuffers(global) { if (global._bufferedUnicodeOutput) { const str = global._bufferedUnicodeOutput.join(''); global._pushOutput(str); delete global._bufferedUnicodeOutput; } if (global._bufferedBinaryOutput) { global._pushOutput(Buffer.concat(global._bufferedBinaryOutput)); delete global._bufferedBinaryOutput; } } const allTokenHandler = (global, token) => { if (global._bufferedUnicodeOutput && (token.type !== 2 || (token.word !== 'uc' && token.word !== 'u'))) { flushBuffers(global); } else if (global._bufferedBinaryOutput && (token.type !== 2 || token.word !== "'")) { flushBuffers(global); } }; const tokenHandlers = { [3]: (globals, token) => { flushBuffers(globals); globals._pushOutput(token.data); }, }; const unicodeControlHandlers = { u: (global, token) => { if (!utils_1.isNum(token.param)) { throw new Error('Unicode control word with no param'); } const newCodeUnit = token.param < 0 ? String.fromCodePoint(token.param + 0x10000) : String.fromCodePoint(token.param); global._bufferedUnicodeOutput = global._bufferedUnicodeOutput || []; global._bufferedUnicodeOutput.push(newCodeUnit); }, "'": (global, token) => { global._bufferedBinaryOutput = global._bufferedBinaryOutput || []; global._bufferedBinaryOutput.push(token.data); } }; exports.handleOutput = { allTokenHandler: allTokenHandler, tokenHandlers: tokenHandlers, controlHandlers: unicodeControlHandlers, };