rtf-stream-parser
Version:
Stream Transform class to tokenize RTF, and another to de-encapsulate text or HTML
60 lines (59 loc) • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleControlsAndDestinations = void 0;
const words_1 = require("../words");
function addDestination(state, destination) {
state.destDepth = (state.destDepth || 0) + 1;
state.destGroupDepth = state.groupDepth;
if (!state.allDestinations) {
state.allDestinations = {};
state.allDestinations[destination] = true;
}
else if (!state.allDestinations[destination]) {
state.allDestinations = Object.create(state.allDestinations);
state.allDestinations[destination] = true;
}
}
const destinationControlHandlers = {
[2]: (global, token) => {
var _a;
if (token.word === '*' && ((_a = global._lastToken) === null || _a === void 0 ? void 0 : _a.type) !== 0) {
global._options.warn('Got control symbol "\\*" but not immediately after "{"');
}
const wordType = words_1.words[token.word] || 0;
if (wordType === 5) {
if (global._lastToken && global._lastToken.type === 0) {
global._state.destination = token.word;
global._state.destIgnorableImmediate = false;
addDestination(global._state, token.word);
}
else if (global._lastToken && global._lastLastToken
&& global._lastToken.type === 2 && global._lastToken.word === '*'
&& global._lastLastToken.type === 0) {
global._state.destination = token.word;
global._state.destIgnorableImmediate = global._state.destIgnorable = true;
addDestination(global._state, token.word);
}
else {
global._options.warn('Got destination control word but not immediately after "{" or "{\\*": ' + token.word);
}
}
else if (wordType === 0) {
if (global._lastToken && global._lastLastToken
&& global._lastToken.type === 2 && global._lastToken.word === '*'
&& global._lastLastToken.type === 0) {
global._state.destination = token.word;
global._state.destIgnorableImmediate = global._state.destIgnorable = true;
addDestination(global._state, token.word);
}
}
},
};
exports.handleControlsAndDestinations = {
tokenHandlers: destinationControlHandlers,
allTokenHandler: (global, token) => {
global._lastLastToken = global._lastToken;
global._lastToken = global._currToken;
global._currToken = token;
}
};