officeparser 5.2.2 → 6.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ /**
2
+ * RTF (Rich Text Format) Parser
3
+ *
4
+ * **RTF Format Overview:**
5
+ * RTF is a proprietary document format developed by Microsoft in 1987.
6
+ * Unlike OOXML formats (DOCX), RTF is a plain text format using control words and groups.
7
+ *
8
+ * **Basic RTF Structure:**
9
+ * ```rtf
10
+ * {\rtf1\ansi
11
+ * {\fonttbl{\f0 Arial;}{\f1 Times;}}
12
+ * {\colortbl;\red255\green0\blue0;\red0\green0\blue255;}
13
+ * \f0\fs24 This is \b bold\b0 and \i italic\i0 text.\par
14
+ * }
15
+ * ```
16
+ *
17
+ * **RTF Elements:**
18
+ * 1. **Control Words**: Backslash followed by letters and optional parameter (e.g., `\fs24`, `\b`, `\par`)
19
+ * 2. **Control Symbols**: Backslash followed by single special char (e.g., `\'xx` for hex char, `\{`, `\}`, `\\`)
20
+ * 3. **Groups**: Content enclosed in `{...}` - creates formatting scope
21
+ * 4. **Text**: Plain text characters
22
+ *
23
+ * **Key Control Words:**
24
+ * - `\rtf1` - RTF version 1
25
+ * - `\fs24` - Font size in half-points (24 = 12pt)
26
+ * - `\b`,`\i`,`\ul`,`\strike` - Bold, italic, underline, strikethrough
27
+ * - `\par` - Paragraph break
28
+ * - `\f0` - Switch to font 0 from font table
29
+ * - `\cf1` - Text color from color table
30
+ * - `\cb2` - Background color from color table
31
+ * - `\sub`,`\super` - Subscript, superscript
32
+ *
33
+ * **Parser Architecture:**
34
+ * This module uses a two-phase approach:
35
+ * 1. **Lexical Phase** (`SimpleRtfParser`): Tokenizes RTF into groups, control words, and text
36
+ * 2. **Semantic Phase** (`parseRtf`): Traverses the token tree to build the AST
37
+ *
38
+ * @module RtfParser
39
+ * @see https://www.biblioscape.com/rtf15_spec.htm RTF 1.5 Specification
40
+ * @see https://latex2rtf.sourceforge.net/RTF-Spec-1.2.pdf RTF 1.2 Specification
41
+ */
42
+ /// <reference types="node" />
43
+ import { OfficeParserAST, OfficeParserConfig } from '../types';
44
+ /**
45
+ * Represents an RTF group (content enclosed in braces).
46
+ * Groups create formatting scopes and can contain other groups, control words, or text.
47
+ *
48
+ * @example
49
+ * // RTF: {\b bold text}
50
+ * // Parsed as group containing control word 'b' and text 'bold text'
51
+ */
52
+ export interface RtfGroup {
53
+ /** Node type identifier */
54
+ type: 'group';
55
+ /** Contents of this group (can be nested groups, control words, or text) */
56
+ content: (RtfGroup | RtfText | RtfControl)[];
57
+ /**
58
+ * The destination control word for this group (if any).
59
+ * Destinations specify what the group contains (e.g., 'fonttbl' for font table).
60
+ * Common destinations: 'fonttbl', 'colortbl', 'footer', 'header', 'pict', 'footnote'
61
+ * @example "fonttbl", "colortbl", "footnote"
62
+ */
63
+ destination?: string;
64
+ }
65
+ /**
66
+ * Represents plain text content in RTF.
67
+ * Text nodes contain the actual displayable characters.
68
+ */
69
+ export interface RtfText {
70
+ /** Node type identifier */
71
+ type: 'text';
72
+ /** The text content (may be a single character or multiple characters) */
73
+ value: string;
74
+ }
75
+ /**
76
+ * Represents an RTF control word or control symbol.
77
+ * Control words modify formatting, specify special characters, or provide document structure.
78
+ *
79
+ * @example
80
+ * // \fs24 - Control word "fs" with parameter 24
81
+ * { type: 'control', value: 'fs', param: 24 }
82
+ *
83
+ * // \b - Control word "b" with no parameter (defaults to "on")
84
+ * { type: 'control', value: 'b', param: undefined }
85
+ *
86
+ * // \u1234 - Unicode character U+1234
87
+ * { type: 'control', value: 'u', param: 1234 }
88
+ */
89
+ export interface RtfControl {
90
+ /** Node type identifier */
91
+ type: 'control';
92
+ /**
93
+ * The control word name (without backslash).
94
+ * @example "fs" for font size, "b" for bold, "par" for paragraph
95
+ */
96
+ value: string;
97
+ /**
98
+ * Optional numeric parameter.
99
+ * - For `\fs24`: param = 24 (12pt font)
100
+ * - For `\b`: param = undefined (defaults to "on")
101
+ * - For `\b0`: param = 0 (explicitly "off")
102
+ * - For `\u1234`: param = 1234 (Unicode code point)
103
+ */
104
+ param?: number;
105
+ }
106
+ /**
107
+ * Union type representing any RTF parse tree node.
108
+ */
109
+ export type RtfNode = RtfGroup | RtfText | RtfControl;
110
+ /**
111
+ * Low-level RTF tokenizer that parses RTF syntax into a tree structure.
112
+ *
113
+ * This class performs lexical analysis on RTF content, breaking it down into:
114
+ * - Groups (enclosed in braces)
115
+ * - Control words (e.g., `\fs24`, `\b`)
116
+ * - Control symbols (e.g., `\'xx`, `\{`)
117
+ * - Plain text
118
+ *
119
+ * The parser uses a byte-level approach and maintains a stack to track nested groups.
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * const buffer = Buffer.from('{\\rtf1 Hello \\b world\\b0}');
124
+ * const parser = new SimpleRtfParser(buffer);
125
+ * const tree = parser.parse();
126
+ * // tree.content contains parsed RTF nodes
127
+ * ```
128
+ */
129
+ export declare class SimpleRtfParser {
130
+ /** Current position in the buffer */
131
+ private index;
132
+ /** The RTF content as a Buffer */
133
+ private buffer;
134
+ /** Total length of the buffer */
135
+ private length;
136
+ /**
137
+ * Creates a new RTF parser.
138
+ * @param buffer - The RTF file content as a Buffer
139
+ */
140
+ constructor(buffer: Buffer);
141
+ parse(): RtfGroup;
142
+ private parseControl;
143
+ private parseText;
144
+ }
145
+ /**
146
+ * Parses an RTF file and returns the AST.
147
+ *
148
+ * **RTF Format Limitations:**
149
+ * The following features are NOT supported due to RTF format constraints:
150
+ *
151
+ * 1. **Images/Attachments**: RTF `\pict` contains device-dependent metafile data
152
+ * (WMF/EMF/DIB). Extracting as portable images requires complex metafile parsing.
153
+ *
154
+ * 2. **StyleMap**: RTF uses inline formatting rather than named style definitions.
155
+ * There is no direct equivalent to DOCX's style.xml.
156
+ *
157
+ * 3. **Endnotes**: RTF uses `\footnote` for both footnotes and endnotes.
158
+ * No separate endnote support exists in the format.
159
+ *
160
+ * @param buffer The file buffer.
161
+ * @param config The parser configuration.
162
+ * @returns The parsed AST.
163
+ */
164
+ export declare const parseRtf: (buffer: Buffer, config: OfficeParserConfig) => Promise<OfficeParserAST>;